OSDN Git Service

Revert "Fix PR debug/49047"
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-dom.c
1 /* SSA Dominator optimizations for trees
2    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Diego Novillo <dnovillo@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "tm_p.h"
29 #include "basic-block.h"
30 #include "cfgloop.h"
31 #include "output.h"
32 #include "function.h"
33 #include "tree-pretty-print.h"
34 #include "gimple-pretty-print.h"
35 #include "timevar.h"
36 #include "tree-dump.h"
37 #include "tree-flow.h"
38 #include "domwalk.h"
39 #include "tree-pass.h"
40 #include "tree-ssa-propagate.h"
41 #include "langhooks.h"
42 #include "params.h"
43
44 /* This file implements optimizations on the dominator tree.  */
45
46 /* Representation of a "naked" right-hand-side expression, to be used
47    in recording available expressions in the expression hash table.  */
48
49 enum expr_kind
50 {
51   EXPR_SINGLE,
52   EXPR_UNARY,
53   EXPR_BINARY,
54   EXPR_TERNARY,
55   EXPR_CALL
56 };
57
58 struct hashable_expr
59 {
60   tree type;
61   enum expr_kind kind;
62   union {
63     struct { tree rhs; } single;
64     struct { enum tree_code op;  tree opnd; } unary;
65     struct { enum tree_code op;  tree opnd0, opnd1; } binary;
66     struct { enum tree_code op;  tree opnd0, opnd1, opnd2; } ternary;
67     struct { gimple fn_from; bool pure; size_t nargs; tree *args; } call;
68   } ops;
69 };
70
71 /* Structure for recording known values of a conditional expression
72    at the exits from its block.  */
73
74 typedef struct cond_equivalence_s
75 {
76   struct hashable_expr cond;
77   tree value;
78 } cond_equivalence;
79
80 DEF_VEC_O(cond_equivalence);
81 DEF_VEC_ALLOC_O(cond_equivalence,heap);
82
83 /* Structure for recording edge equivalences as well as any pending
84    edge redirections during the dominator optimizer.
85
86    Computing and storing the edge equivalences instead of creating
87    them on-demand can save significant amounts of time, particularly
88    for pathological cases involving switch statements.
89
90    These structures live for a single iteration of the dominator
91    optimizer in the edge's AUX field.  At the end of an iteration we
92    free each of these structures and update the AUX field to point
93    to any requested redirection target (the code for updating the
94    CFG and SSA graph for edge redirection expects redirection edge
95    targets to be in the AUX field for each edge.  */
96
97 struct edge_info
98 {
99   /* If this edge creates a simple equivalence, the LHS and RHS of
100      the equivalence will be stored here.  */
101   tree lhs;
102   tree rhs;
103
104   /* Traversing an edge may also indicate one or more particular conditions
105      are true or false.  */
106   VEC(cond_equivalence, heap) *cond_equivalences;
107 };
108
109 /* Hash table with expressions made available during the renaming process.
110    When an assignment of the form X_i = EXPR is found, the statement is
111    stored in this table.  If the same expression EXPR is later found on the
112    RHS of another statement, it is replaced with X_i (thus performing
113    global redundancy elimination).  Similarly as we pass through conditionals
114    we record the conditional itself as having either a true or false value
115    in this table.  */
116 static htab_t avail_exprs;
117
118 /* Stack of available expressions in AVAIL_EXPRs.  Each block pushes any
119    expressions it enters into the hash table along with a marker entry
120    (null).  When we finish processing the block, we pop off entries and
121    remove the expressions from the global hash table until we hit the
122    marker.  */
123 typedef struct expr_hash_elt * expr_hash_elt_t;
124 DEF_VEC_P(expr_hash_elt_t);
125 DEF_VEC_ALLOC_P(expr_hash_elt_t,heap);
126
127 static VEC(expr_hash_elt_t,heap) *avail_exprs_stack;
128
129 /* Structure for entries in the expression hash table.  */
130
131 struct expr_hash_elt
132 {
133   /* The value (lhs) of this expression.  */
134   tree lhs;
135
136   /* The expression (rhs) we want to record.  */
137   struct hashable_expr expr;
138
139   /* The stmt pointer if this element corresponds to a statement.  */
140   gimple stmt;
141
142   /* The hash value for RHS.  */
143   hashval_t hash;
144
145   /* A unique stamp, typically the address of the hash
146      element itself, used in removing entries from the table.  */
147   struct expr_hash_elt *stamp;
148 };
149
150 /* Stack of dest,src pairs that need to be restored during finalization.
151
152    A NULL entry is used to mark the end of pairs which need to be
153    restored during finalization of this block.  */
154 static VEC(tree,heap) *const_and_copies_stack;
155
156 /* Track whether or not we have changed the control flow graph.  */
157 static bool cfg_altered;
158
159 /* Bitmap of blocks that have had EH statements cleaned.  We should
160    remove their dead edges eventually.  */
161 static bitmap need_eh_cleanup;
162
163 /* Statistics for dominator optimizations.  */
164 struct opt_stats_d
165 {
166   long num_stmts;
167   long num_exprs_considered;
168   long num_re;
169   long num_const_prop;
170   long num_copy_prop;
171 };
172
173 static struct opt_stats_d opt_stats;
174
175 /* Local functions.  */
176 static void optimize_stmt (basic_block, gimple_stmt_iterator);
177 static tree lookup_avail_expr (gimple, bool);
178 static hashval_t avail_expr_hash (const void *);
179 static hashval_t real_avail_expr_hash (const void *);
180 static int avail_expr_eq (const void *, const void *);
181 static void htab_statistics (FILE *, htab_t);
182 static void record_cond (cond_equivalence *);
183 static void record_const_or_copy (tree, tree);
184 static void record_equality (tree, tree);
185 static void record_equivalences_from_phis (basic_block);
186 static void record_equivalences_from_incoming_edge (basic_block);
187 static void eliminate_redundant_computations (gimple_stmt_iterator *);
188 static void record_equivalences_from_stmt (gimple, int);
189 static void dom_thread_across_edge (struct dom_walk_data *, edge);
190 static void dom_opt_leave_block (struct dom_walk_data *, basic_block);
191 static void dom_opt_enter_block (struct dom_walk_data *, basic_block);
192 static void remove_local_expressions_from_table (void);
193 static void restore_vars_to_original_value (void);
194 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
195
196
197 /* Given a statement STMT, initialize the hash table element pointed to
198    by ELEMENT.  */
199
200 static void
201 initialize_hash_element (gimple stmt, tree lhs,
202                          struct expr_hash_elt *element)
203 {
204   enum gimple_code code = gimple_code (stmt);
205   struct hashable_expr *expr = &element->expr;
206
207   if (code == GIMPLE_ASSIGN)
208     {
209       enum tree_code subcode = gimple_assign_rhs_code (stmt);
210
211       expr->type = NULL_TREE;
212
213       switch (get_gimple_rhs_class (subcode))
214         {
215         case GIMPLE_SINGLE_RHS:
216           expr->kind = EXPR_SINGLE;
217           expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
218           break;
219         case GIMPLE_UNARY_RHS:
220           expr->kind = EXPR_UNARY;
221           expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
222           expr->ops.unary.op = subcode;
223           expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
224           break;
225         case GIMPLE_BINARY_RHS:
226           expr->kind = EXPR_BINARY;
227           expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
228           expr->ops.binary.op = subcode;
229           expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
230           expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
231           break;
232         case GIMPLE_TERNARY_RHS:
233           expr->kind = EXPR_TERNARY;
234           expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
235           expr->ops.ternary.op = subcode;
236           expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt);
237           expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt);
238           expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt);
239           break;
240         default:
241           gcc_unreachable ();
242         }
243     }
244   else if (code == GIMPLE_COND)
245     {
246       expr->type = boolean_type_node;
247       expr->kind = EXPR_BINARY;
248       expr->ops.binary.op = gimple_cond_code (stmt);
249       expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
250       expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
251     }
252   else if (code == GIMPLE_CALL)
253     {
254       size_t nargs = gimple_call_num_args (stmt);
255       size_t i;
256
257       gcc_assert (gimple_call_lhs (stmt));
258
259       expr->type = TREE_TYPE (gimple_call_lhs (stmt));
260       expr->kind = EXPR_CALL;
261       expr->ops.call.fn_from = stmt;
262
263       if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
264         expr->ops.call.pure = true;
265       else
266         expr->ops.call.pure = false;
267
268       expr->ops.call.nargs = nargs;
269       expr->ops.call.args = (tree *) xcalloc (nargs, sizeof (tree));
270       for (i = 0; i < nargs; i++)
271         expr->ops.call.args[i] = gimple_call_arg (stmt, i);
272     }
273   else if (code == GIMPLE_SWITCH)
274     {
275       expr->type = TREE_TYPE (gimple_switch_index (stmt));
276       expr->kind = EXPR_SINGLE;
277       expr->ops.single.rhs = gimple_switch_index (stmt);
278     }
279   else if (code == GIMPLE_GOTO)
280     {
281       expr->type = TREE_TYPE (gimple_goto_dest (stmt));
282       expr->kind = EXPR_SINGLE;
283       expr->ops.single.rhs = gimple_goto_dest (stmt);
284     }
285   else
286     gcc_unreachable ();
287
288   element->lhs = lhs;
289   element->stmt = stmt;
290   element->hash = avail_expr_hash (element);
291   element->stamp = element;
292 }
293
294 /* Given a conditional expression COND as a tree, initialize
295    a hashable_expr expression EXPR.  The conditional must be a
296    comparison or logical negation.  A constant or a variable is
297    not permitted.  */
298
299 static void
300 initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
301 {
302   expr->type = boolean_type_node;
303
304   if (COMPARISON_CLASS_P (cond))
305     {
306       expr->kind = EXPR_BINARY;
307       expr->ops.binary.op = TREE_CODE (cond);
308       expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
309       expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
310     }
311   else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
312     {
313       expr->kind = EXPR_UNARY;
314       expr->ops.unary.op = TRUTH_NOT_EXPR;
315       expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
316     }
317   else
318     gcc_unreachable ();
319 }
320
321 /* Given a hashable_expr expression EXPR and an LHS,
322    initialize the hash table element pointed to by ELEMENT.  */
323
324 static void
325 initialize_hash_element_from_expr (struct hashable_expr *expr,
326                                    tree lhs,
327                                    struct expr_hash_elt *element)
328 {
329   element->expr = *expr;
330   element->lhs = lhs;
331   element->stmt = NULL;
332   element->hash = avail_expr_hash (element);
333   element->stamp = element;
334 }
335
336 /* Compare two hashable_expr structures for equivalence.
337    They are considered equivalent when the the expressions
338    they denote must necessarily be equal.  The logic is intended
339    to follow that of operand_equal_p in fold-const.c  */
340
341 static bool
342 hashable_expr_equal_p (const struct hashable_expr *expr0,
343                         const struct hashable_expr *expr1)
344 {
345   tree type0 = expr0->type;
346   tree type1 = expr1->type;
347
348   /* If either type is NULL, there is nothing to check.  */
349   if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
350     return false;
351
352   /* If both types don't have the same signedness, precision, and mode,
353      then we can't consider  them equal.  */
354   if (type0 != type1
355       && (TREE_CODE (type0) == ERROR_MARK
356           || TREE_CODE (type1) == ERROR_MARK
357           || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
358           || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
359           || TYPE_MODE (type0) != TYPE_MODE (type1)))
360     return false;
361
362   if (expr0->kind != expr1->kind)
363     return false;
364
365   switch (expr0->kind)
366     {
367     case EXPR_SINGLE:
368       return operand_equal_p (expr0->ops.single.rhs,
369                               expr1->ops.single.rhs, 0);
370
371     case EXPR_UNARY:
372       if (expr0->ops.unary.op != expr1->ops.unary.op)
373         return false;
374
375       if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
376            || expr0->ops.unary.op == NON_LVALUE_EXPR)
377           && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
378         return false;
379
380       return operand_equal_p (expr0->ops.unary.opnd,
381                               expr1->ops.unary.opnd, 0);
382
383     case EXPR_BINARY:
384       if (expr0->ops.binary.op != expr1->ops.binary.op)
385         return false;
386
387       if (operand_equal_p (expr0->ops.binary.opnd0,
388                            expr1->ops.binary.opnd0, 0)
389           && operand_equal_p (expr0->ops.binary.opnd1,
390                               expr1->ops.binary.opnd1, 0))
391         return true;
392
393       /* For commutative ops, allow the other order.  */
394       return (commutative_tree_code (expr0->ops.binary.op)
395               && operand_equal_p (expr0->ops.binary.opnd0,
396                                   expr1->ops.binary.opnd1, 0)
397               && operand_equal_p (expr0->ops.binary.opnd1,
398                                   expr1->ops.binary.opnd0, 0));
399
400     case EXPR_TERNARY:
401       if (expr0->ops.ternary.op != expr1->ops.ternary.op
402           || !operand_equal_p (expr0->ops.ternary.opnd2,
403                                expr1->ops.ternary.opnd2, 0))
404         return false;
405
406       if (operand_equal_p (expr0->ops.ternary.opnd0,
407                            expr1->ops.ternary.opnd0, 0)
408           && operand_equal_p (expr0->ops.ternary.opnd1,
409                               expr1->ops.ternary.opnd1, 0))
410         return true;
411
412       /* For commutative ops, allow the other order.  */
413       return (commutative_ternary_tree_code (expr0->ops.ternary.op)
414               && operand_equal_p (expr0->ops.ternary.opnd0,
415                                   expr1->ops.ternary.opnd1, 0)
416               && operand_equal_p (expr0->ops.ternary.opnd1,
417                                   expr1->ops.ternary.opnd0, 0));
418
419     case EXPR_CALL:
420       {
421         size_t i;
422
423         /* If the calls are to different functions, then they
424            clearly cannot be equal.  */
425         if (!gimple_call_same_target_p (expr0->ops.call.fn_from,
426                                         expr1->ops.call.fn_from))
427           return false;
428
429         if (! expr0->ops.call.pure)
430           return false;
431
432         if (expr0->ops.call.nargs !=  expr1->ops.call.nargs)
433           return false;
434
435         for (i = 0; i < expr0->ops.call.nargs; i++)
436           if (! operand_equal_p (expr0->ops.call.args[i],
437                                  expr1->ops.call.args[i], 0))
438             return false;
439
440         return true;
441       }
442
443     default:
444       gcc_unreachable ();
445     }
446 }
447
448 /* Compute a hash value for a hashable_expr value EXPR and a
449    previously accumulated hash value VAL.  If two hashable_expr
450    values compare equal with hashable_expr_equal_p, they must
451    hash to the same value, given an identical value of VAL.
452    The logic is intended to follow iterative_hash_expr in tree.c.  */
453
454 static hashval_t
455 iterative_hash_hashable_expr (const struct hashable_expr *expr, hashval_t val)
456 {
457   switch (expr->kind)
458     {
459     case EXPR_SINGLE:
460       val = iterative_hash_expr (expr->ops.single.rhs, val);
461       break;
462
463     case EXPR_UNARY:
464       val = iterative_hash_object (expr->ops.unary.op, val);
465
466       /* Make sure to include signedness in the hash computation.
467          Don't hash the type, that can lead to having nodes which
468          compare equal according to operand_equal_p, but which
469          have different hash codes.  */
470       if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
471           || expr->ops.unary.op == NON_LVALUE_EXPR)
472         val += TYPE_UNSIGNED (expr->type);
473
474       val = iterative_hash_expr (expr->ops.unary.opnd, val);
475       break;
476
477     case EXPR_BINARY:
478       val = iterative_hash_object (expr->ops.binary.op, val);
479       if (commutative_tree_code (expr->ops.binary.op))
480         val = iterative_hash_exprs_commutative (expr->ops.binary.opnd0,
481                                                 expr->ops.binary.opnd1, val);
482       else
483         {
484           val = iterative_hash_expr (expr->ops.binary.opnd0, val);
485           val = iterative_hash_expr (expr->ops.binary.opnd1, val);
486         }
487       break;
488
489     case EXPR_TERNARY:
490       val = iterative_hash_object (expr->ops.ternary.op, val);
491       if (commutative_ternary_tree_code (expr->ops.ternary.op))
492         val = iterative_hash_exprs_commutative (expr->ops.ternary.opnd0,
493                                                 expr->ops.ternary.opnd1, val);
494       else
495         {
496           val = iterative_hash_expr (expr->ops.ternary.opnd0, val);
497           val = iterative_hash_expr (expr->ops.ternary.opnd1, val);
498         }
499       val = iterative_hash_expr (expr->ops.ternary.opnd2, val);
500       break;
501
502     case EXPR_CALL:
503       {
504         size_t i;
505         enum tree_code code = CALL_EXPR;
506         gimple fn_from;
507
508         val = iterative_hash_object (code, val);
509         fn_from = expr->ops.call.fn_from;
510         if (gimple_call_internal_p (fn_from))
511           val = iterative_hash_hashval_t
512             ((hashval_t) gimple_call_internal_fn (fn_from), val);
513         else
514           val = iterative_hash_expr (gimple_call_fn (fn_from), val);
515         for (i = 0; i < expr->ops.call.nargs; i++)
516           val = iterative_hash_expr (expr->ops.call.args[i], val);
517       }
518       break;
519
520     default:
521       gcc_unreachable ();
522     }
523
524   return val;
525 }
526
527 /* Print a diagnostic dump of an expression hash table entry.  */
528
529 static void
530 print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
531 {
532   if (element->stmt)
533     fprintf (stream, "STMT ");
534   else
535     fprintf (stream, "COND ");
536
537   if (element->lhs)
538     {
539       print_generic_expr (stream, element->lhs, 0);
540       fprintf (stream, " = ");
541     }
542
543   switch (element->expr.kind)
544     {
545       case EXPR_SINGLE:
546         print_generic_expr (stream, element->expr.ops.single.rhs, 0);
547         break;
548
549       case EXPR_UNARY:
550         fprintf (stream, "%s ", tree_code_name[element->expr.ops.unary.op]);
551         print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
552         break;
553
554       case EXPR_BINARY:
555         print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
556         fprintf (stream, " %s ", tree_code_name[element->expr.ops.binary.op]);
557         print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
558         break;
559
560       case EXPR_TERNARY:
561         fprintf (stream, " %s <", tree_code_name[element->expr.ops.ternary.op]);
562         print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0);
563         fputs (", ", stream);
564         print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0);
565         fputs (", ", stream);
566         print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0);
567         fputs (">", stream);
568         break;
569
570       case EXPR_CALL:
571         {
572           size_t i;
573           size_t nargs = element->expr.ops.call.nargs;
574           gimple fn_from;
575
576           fn_from = element->expr.ops.call.fn_from;
577           if (gimple_call_internal_p (fn_from))
578             fputs (internal_fn_name (gimple_call_internal_fn (fn_from)),
579                    stream);
580           else
581             print_generic_expr (stream, gimple_call_fn (fn_from), 0);
582           fprintf (stream, " (");
583           for (i = 0; i < nargs; i++)
584             {
585               print_generic_expr (stream, element->expr.ops.call.args[i], 0);
586               if (i + 1 < nargs)
587                 fprintf (stream, ", ");
588             }
589           fprintf (stream, ")");
590         }
591         break;
592     }
593   fprintf (stream, "\n");
594
595   if (element->stmt)
596     {
597       fprintf (stream, "          ");
598       print_gimple_stmt (stream, element->stmt, 0, 0);
599     }
600 }
601
602 /* Delete an expr_hash_elt and reclaim its storage.  */
603
604 static void
605 free_expr_hash_elt (void *elt)
606 {
607   struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
608
609   if (element->expr.kind == EXPR_CALL)
610     free (element->expr.ops.call.args);
611
612   free (element);
613 }
614
615 /* Allocate an EDGE_INFO for edge E and attach it to E.
616    Return the new EDGE_INFO structure.  */
617
618 static struct edge_info *
619 allocate_edge_info (edge e)
620 {
621   struct edge_info *edge_info;
622
623   edge_info = XCNEW (struct edge_info);
624
625   e->aux = edge_info;
626   return edge_info;
627 }
628
629 /* Free all EDGE_INFO structures associated with edges in the CFG.
630    If a particular edge can be threaded, copy the redirection
631    target from the EDGE_INFO structure into the edge's AUX field
632    as required by code to update the CFG and SSA graph for
633    jump threading.  */
634
635 static void
636 free_all_edge_infos (void)
637 {
638   basic_block bb;
639   edge_iterator ei;
640   edge e;
641
642   FOR_EACH_BB (bb)
643     {
644       FOR_EACH_EDGE (e, ei, bb->preds)
645         {
646          struct edge_info *edge_info = (struct edge_info *) e->aux;
647
648           if (edge_info)
649             {
650               if (edge_info->cond_equivalences)
651                 VEC_free (cond_equivalence, heap, edge_info->cond_equivalences);
652               free (edge_info);
653               e->aux = NULL;
654             }
655         }
656     }
657 }
658
659 /* Jump threading, redundancy elimination and const/copy propagation.
660
661    This pass may expose new symbols that need to be renamed into SSA.  For
662    every new symbol exposed, its corresponding bit will be set in
663    VARS_TO_RENAME.  */
664
665 static unsigned int
666 tree_ssa_dominator_optimize (void)
667 {
668   struct dom_walk_data walk_data;
669
670   memset (&opt_stats, 0, sizeof (opt_stats));
671
672   /* Create our hash tables.  */
673   avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free_expr_hash_elt);
674   avail_exprs_stack = VEC_alloc (expr_hash_elt_t, heap, 20);
675   const_and_copies_stack = VEC_alloc (tree, heap, 20);
676   need_eh_cleanup = BITMAP_ALLOC (NULL);
677
678   /* Setup callbacks for the generic dominator tree walker.  */
679   walk_data.dom_direction = CDI_DOMINATORS;
680   walk_data.initialize_block_local_data = NULL;
681   walk_data.before_dom_children = dom_opt_enter_block;
682   walk_data.after_dom_children = dom_opt_leave_block;
683   /* Right now we only attach a dummy COND_EXPR to the global data pointer.
684      When we attach more stuff we'll need to fill this out with a real
685      structure.  */
686   walk_data.global_data = NULL;
687   walk_data.block_local_data_size = 0;
688
689   /* Now initialize the dominator walker.  */
690   init_walk_dominator_tree (&walk_data);
691
692   calculate_dominance_info (CDI_DOMINATORS);
693   cfg_altered = false;
694
695   /* We need to know loop structures in order to avoid destroying them
696      in jump threading.  Note that we still can e.g. thread through loop
697      headers to an exit edge, or through loop header to the loop body, assuming
698      that we update the loop info.  */
699   loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES);
700
701   /* Initialize the value-handle array.  */
702   threadedge_initialize_values ();
703
704   /* We need accurate information regarding back edges in the CFG
705      for jump threading; this may include back edges that are not part of
706      a single loop.  */
707   mark_dfs_back_edges ();
708
709   /* Recursively walk the dominator tree optimizing statements.  */
710   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
711
712   {
713     gimple_stmt_iterator gsi;
714     basic_block bb;
715     FOR_EACH_BB (bb)
716       {
717         for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
718           update_stmt_if_modified (gsi_stmt (gsi));
719       }
720   }
721
722   /* If we exposed any new variables, go ahead and put them into
723      SSA form now, before we handle jump threading.  This simplifies
724      interactions between rewriting of _DECL nodes into SSA form
725      and rewriting SSA_NAME nodes into SSA form after block
726      duplication and CFG manipulation.  */
727   update_ssa (TODO_update_ssa);
728
729   free_all_edge_infos ();
730
731   /* Thread jumps, creating duplicate blocks as needed.  */
732   cfg_altered |= thread_through_all_blocks (first_pass_instance);
733
734   if (cfg_altered)
735     free_dominance_info (CDI_DOMINATORS);
736
737   /* Removal of statements may make some EH edges dead.  Purge
738      such edges from the CFG as needed.  */
739   if (!bitmap_empty_p (need_eh_cleanup))
740     {
741       unsigned i;
742       bitmap_iterator bi;
743
744       /* Jump threading may have created forwarder blocks from blocks
745          needing EH cleanup; the new successor of these blocks, which
746          has inherited from the original block, needs the cleanup.  */
747       EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
748         {
749           basic_block bb = BASIC_BLOCK (i);
750           if (bb
751               && single_succ_p (bb)
752               && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
753             {
754               bitmap_clear_bit (need_eh_cleanup, i);
755               bitmap_set_bit (need_eh_cleanup, single_succ (bb)->index);
756             }
757         }
758
759       gimple_purge_all_dead_eh_edges (need_eh_cleanup);
760       bitmap_zero (need_eh_cleanup);
761     }
762
763   statistics_counter_event (cfun, "Redundant expressions eliminated",
764                             opt_stats.num_re);
765   statistics_counter_event (cfun, "Constants propagated",
766                             opt_stats.num_const_prop);
767   statistics_counter_event (cfun, "Copies propagated",
768                             opt_stats.num_copy_prop);
769
770   /* Debugging dumps.  */
771   if (dump_file && (dump_flags & TDF_STATS))
772     dump_dominator_optimization_stats (dump_file);
773
774   loop_optimizer_finalize ();
775
776   /* Delete our main hashtable.  */
777   htab_delete (avail_exprs);
778
779   /* And finalize the dominator walker.  */
780   fini_walk_dominator_tree (&walk_data);
781
782   /* Free asserted bitmaps and stacks.  */
783   BITMAP_FREE (need_eh_cleanup);
784
785   VEC_free (expr_hash_elt_t, heap, avail_exprs_stack);
786   VEC_free (tree, heap, const_and_copies_stack);
787
788   /* Free the value-handle array.  */
789   threadedge_finalize_values ();
790   ssa_name_values = NULL;
791
792   return 0;
793 }
794
795 static bool
796 gate_dominator (void)
797 {
798   return flag_tree_dom != 0;
799 }
800
801 struct gimple_opt_pass pass_dominator =
802 {
803  {
804   GIMPLE_PASS,
805   "dom",                                /* name */
806   gate_dominator,                       /* gate */
807   tree_ssa_dominator_optimize,          /* execute */
808   NULL,                                 /* sub */
809   NULL,                                 /* next */
810   0,                                    /* static_pass_number */
811   TV_TREE_SSA_DOMINATOR_OPTS,           /* tv_id */
812   PROP_cfg | PROP_ssa,                  /* properties_required */
813   0,                                    /* properties_provided */
814   0,                                    /* properties_destroyed */
815   0,                                    /* todo_flags_start */
816   TODO_cleanup_cfg
817     | TODO_update_ssa
818     | TODO_verify_ssa
819     | TODO_verify_flow
820     | TODO_dump_func                    /* todo_flags_finish */
821  }
822 };
823
824
825 /* Given a conditional statement CONDSTMT, convert the
826    condition to a canonical form.  */
827
828 static void
829 canonicalize_comparison (gimple condstmt)
830 {
831   tree op0;
832   tree op1;
833   enum tree_code code;
834
835   gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
836
837   op0 = gimple_cond_lhs (condstmt);
838   op1 = gimple_cond_rhs (condstmt);
839
840   code = gimple_cond_code (condstmt);
841
842   /* If it would be profitable to swap the operands, then do so to
843      canonicalize the statement, enabling better optimization.
844
845      By placing canonicalization of such expressions here we
846      transparently keep statements in canonical form, even
847      when the statement is modified.  */
848   if (tree_swap_operands_p (op0, op1, false))
849     {
850       /* For relationals we need to swap the operands
851          and change the code.  */
852       if (code == LT_EXPR
853           || code == GT_EXPR
854           || code == LE_EXPR
855           || code == GE_EXPR)
856         {
857           code = swap_tree_comparison (code);
858
859           gimple_cond_set_code (condstmt, code);
860           gimple_cond_set_lhs (condstmt, op1);
861           gimple_cond_set_rhs (condstmt, op0);
862
863           update_stmt (condstmt);
864         }
865     }
866 }
867
868 /* Initialize local stacks for this optimizer and record equivalences
869    upon entry to BB.  Equivalences can come from the edge traversed to
870    reach BB or they may come from PHI nodes at the start of BB.  */
871
872 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
873    LIMIT entries left in LOCALs.  */
874
875 static void
876 remove_local_expressions_from_table (void)
877 {
878   /* Remove all the expressions made available in this block.  */
879   while (VEC_length (expr_hash_elt_t, avail_exprs_stack) > 0)
880     {
881       expr_hash_elt_t victim = VEC_pop (expr_hash_elt_t, avail_exprs_stack);
882       void **slot;
883
884       if (victim == NULL)
885         break;
886
887       /* This must precede the actual removal from the hash table,
888          as ELEMENT and the table entry may share a call argument
889          vector which will be freed during removal.  */
890       if (dump_file && (dump_flags & TDF_DETAILS))
891         {
892           fprintf (dump_file, "<<<< ");
893           print_expr_hash_elt (dump_file, victim);
894         }
895
896       slot = htab_find_slot_with_hash (avail_exprs,
897                                        victim, victim->hash, NO_INSERT);
898       gcc_assert (slot && *slot == (void *) victim);
899       htab_clear_slot (avail_exprs, slot);
900     }
901 }
902
903 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
904    CONST_AND_COPIES to its original state, stopping when we hit a
905    NULL marker.  */
906
907 static void
908 restore_vars_to_original_value (void)
909 {
910   while (VEC_length (tree, const_and_copies_stack) > 0)
911     {
912       tree prev_value, dest;
913
914       dest = VEC_pop (tree, const_and_copies_stack);
915
916       if (dest == NULL)
917         break;
918
919       if (dump_file && (dump_flags & TDF_DETAILS))
920         {
921           fprintf (dump_file, "<<<< COPY ");
922           print_generic_expr (dump_file, dest, 0);
923           fprintf (dump_file, " = ");
924           print_generic_expr (dump_file, SSA_NAME_VALUE (dest), 0);
925           fprintf (dump_file, "\n");
926         }
927
928       prev_value = VEC_pop (tree, const_and_copies_stack);
929       set_ssa_name_value (dest, prev_value);
930     }
931 }
932
933 /* A trivial wrapper so that we can present the generic jump
934    threading code with a simple API for simplifying statements.  */
935 static tree
936 simplify_stmt_for_jump_threading (gimple stmt,
937                                   gimple within_stmt ATTRIBUTE_UNUSED)
938 {
939   return lookup_avail_expr (stmt, false);
940 }
941
942 /* Wrapper for common code to attempt to thread an edge.  For example,
943    it handles lazily building the dummy condition and the bookkeeping
944    when jump threading is successful.  */
945
946 static void
947 dom_thread_across_edge (struct dom_walk_data *walk_data, edge e)
948 {
949   if (! walk_data->global_data)
950   {
951     gimple dummy_cond =
952         gimple_build_cond (NE_EXPR,
953                            integer_zero_node, integer_zero_node,
954                            NULL, NULL);
955     walk_data->global_data = dummy_cond;
956   }
957
958   thread_across_edge ((gimple) walk_data->global_data, e, false,
959                       &const_and_copies_stack,
960                       simplify_stmt_for_jump_threading);
961 }
962
963 /* PHI nodes can create equivalences too.
964
965    Ignoring any alternatives which are the same as the result, if
966    all the alternatives are equal, then the PHI node creates an
967    equivalence.  */
968
969 static void
970 record_equivalences_from_phis (basic_block bb)
971 {
972   gimple_stmt_iterator gsi;
973
974   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
975     {
976       gimple phi = gsi_stmt (gsi);
977
978       tree lhs = gimple_phi_result (phi);
979       tree rhs = NULL;
980       size_t i;
981
982       for (i = 0; i < gimple_phi_num_args (phi); i++)
983         {
984           tree t = gimple_phi_arg_def (phi, i);
985
986           /* Ignore alternatives which are the same as our LHS.  Since
987              LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
988              can simply compare pointers.  */
989           if (lhs == t)
990             continue;
991
992           /* If we have not processed an alternative yet, then set
993              RHS to this alternative.  */
994           if (rhs == NULL)
995             rhs = t;
996           /* If we have processed an alternative (stored in RHS), then
997              see if it is equal to this one.  If it isn't, then stop
998              the search.  */
999           else if (! operand_equal_for_phi_arg_p (rhs, t))
1000             break;
1001         }
1002
1003       /* If we had no interesting alternatives, then all the RHS alternatives
1004          must have been the same as LHS.  */
1005       if (!rhs)
1006         rhs = lhs;
1007
1008       /* If we managed to iterate through each PHI alternative without
1009          breaking out of the loop, then we have a PHI which may create
1010          a useful equivalence.  We do not need to record unwind data for
1011          this, since this is a true assignment and not an equivalence
1012          inferred from a comparison.  All uses of this ssa name are dominated
1013          by this assignment, so unwinding just costs time and space.  */
1014       if (i == gimple_phi_num_args (phi) && may_propagate_copy (lhs, rhs))
1015         set_ssa_name_value (lhs, rhs);
1016     }
1017 }
1018
1019 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1020    return that edge.  Otherwise return NULL.  */
1021 static edge
1022 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1023 {
1024   edge retval = NULL;
1025   edge e;
1026   edge_iterator ei;
1027
1028   FOR_EACH_EDGE (e, ei, bb->preds)
1029     {
1030       /* A loop back edge can be identified by the destination of
1031          the edge dominating the source of the edge.  */
1032       if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1033         continue;
1034
1035       /* If we have already seen a non-loop edge, then we must have
1036          multiple incoming non-loop edges and thus we return NULL.  */
1037       if (retval)
1038         return NULL;
1039
1040       /* This is the first non-loop incoming edge we have found.  Record
1041          it.  */
1042       retval = e;
1043     }
1044
1045   return retval;
1046 }
1047
1048 /* Record any equivalences created by the incoming edge to BB.  If BB
1049    has more than one incoming edge, then no equivalence is created.  */
1050
1051 static void
1052 record_equivalences_from_incoming_edge (basic_block bb)
1053 {
1054   edge e;
1055   basic_block parent;
1056   struct edge_info *edge_info;
1057
1058   /* If our parent block ended with a control statement, then we may be
1059      able to record some equivalences based on which outgoing edge from
1060      the parent was followed.  */
1061   parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1062
1063   e = single_incoming_edge_ignoring_loop_edges (bb);
1064
1065   /* If we had a single incoming edge from our parent block, then enter
1066      any data associated with the edge into our tables.  */
1067   if (e && e->src == parent)
1068     {
1069       unsigned int i;
1070
1071       edge_info = (struct edge_info *) e->aux;
1072
1073       if (edge_info)
1074         {
1075           tree lhs = edge_info->lhs;
1076           tree rhs = edge_info->rhs;
1077           cond_equivalence *eq;
1078
1079           if (lhs)
1080             record_equality (lhs, rhs);
1081
1082           for (i = 0; VEC_iterate (cond_equivalence,
1083                                    edge_info->cond_equivalences, i, eq); ++i)
1084             record_cond (eq);
1085         }
1086     }
1087 }
1088
1089 /* Dump SSA statistics on FILE.  */
1090
1091 void
1092 dump_dominator_optimization_stats (FILE *file)
1093 {
1094   fprintf (file, "Total number of statements:                   %6ld\n\n",
1095            opt_stats.num_stmts);
1096   fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1097            opt_stats.num_exprs_considered);
1098
1099   fprintf (file, "\nHash table statistics:\n");
1100
1101   fprintf (file, "    avail_exprs: ");
1102   htab_statistics (file, avail_exprs);
1103 }
1104
1105
1106 /* Dump SSA statistics on stderr.  */
1107
1108 DEBUG_FUNCTION void
1109 debug_dominator_optimization_stats (void)
1110 {
1111   dump_dominator_optimization_stats (stderr);
1112 }
1113
1114
1115 /* Dump statistics for the hash table HTAB.  */
1116
1117 static void
1118 htab_statistics (FILE *file, htab_t htab)
1119 {
1120   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1121            (long) htab_size (htab),
1122            (long) htab_elements (htab),
1123            htab_collisions (htab));
1124 }
1125
1126
1127 /* Enter condition equivalence into the expression hash table.
1128    This indicates that a conditional expression has a known
1129    boolean value.  */
1130
1131 static void
1132 record_cond (cond_equivalence *p)
1133 {
1134   struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
1135   void **slot;
1136
1137   initialize_hash_element_from_expr (&p->cond, p->value, element);
1138
1139   slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1140                                    element->hash, INSERT);
1141   if (*slot == NULL)
1142     {
1143       *slot = (void *) element;
1144
1145       if (dump_file && (dump_flags & TDF_DETAILS))
1146         {
1147           fprintf (dump_file, "1>>> ");
1148           print_expr_hash_elt (dump_file, element);
1149         }
1150
1151       VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element);
1152     }
1153   else
1154     free (element);
1155 }
1156
1157 /* Build a cond_equivalence record indicating that the comparison
1158    CODE holds between operands OP0 and OP1 and push it to **P.  */
1159
1160 static void
1161 build_and_record_new_cond (enum tree_code code,
1162                            tree op0, tree op1,
1163                            VEC(cond_equivalence, heap) **p)
1164 {
1165   cond_equivalence c;
1166   struct hashable_expr *cond = &c.cond;
1167
1168   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1169
1170   cond->type = boolean_type_node;
1171   cond->kind = EXPR_BINARY;
1172   cond->ops.binary.op = code;
1173   cond->ops.binary.opnd0 = op0;
1174   cond->ops.binary.opnd1 = op1;
1175
1176   c.value = boolean_true_node;
1177   VEC_safe_push (cond_equivalence, heap, *p, &c);
1178 }
1179
1180 /* Record that COND is true and INVERTED is false into the edge information
1181    structure.  Also record that any conditions dominated by COND are true
1182    as well.
1183
1184    For example, if a < b is true, then a <= b must also be true.  */
1185
1186 static void
1187 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1188 {
1189   tree op0, op1;
1190   cond_equivalence c;
1191
1192   if (!COMPARISON_CLASS_P (cond))
1193     return;
1194
1195   op0 = TREE_OPERAND (cond, 0);
1196   op1 = TREE_OPERAND (cond, 1);
1197
1198   switch (TREE_CODE (cond))
1199     {
1200     case LT_EXPR:
1201     case GT_EXPR:
1202       if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1203         {
1204           build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1205                                      &edge_info->cond_equivalences);
1206           build_and_record_new_cond (LTGT_EXPR, op0, op1,
1207                                      &edge_info->cond_equivalences);
1208         }
1209
1210       build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1211                                   ? LE_EXPR : GE_EXPR),
1212                                  op0, op1, &edge_info->cond_equivalences);
1213       build_and_record_new_cond (NE_EXPR, op0, op1,
1214                                  &edge_info->cond_equivalences);
1215       break;
1216
1217     case GE_EXPR:
1218     case LE_EXPR:
1219       if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1220         {
1221           build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1222                                      &edge_info->cond_equivalences);
1223         }
1224       break;
1225
1226     case EQ_EXPR:
1227       if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1228         {
1229           build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1230                                      &edge_info->cond_equivalences);
1231         }
1232       build_and_record_new_cond (LE_EXPR, op0, op1,
1233                                  &edge_info->cond_equivalences);
1234       build_and_record_new_cond (GE_EXPR, op0, op1,
1235                                  &edge_info->cond_equivalences);
1236       break;
1237
1238     case UNORDERED_EXPR:
1239       build_and_record_new_cond (NE_EXPR, op0, op1,
1240                                  &edge_info->cond_equivalences);
1241       build_and_record_new_cond (UNLE_EXPR, op0, op1,
1242                                  &edge_info->cond_equivalences);
1243       build_and_record_new_cond (UNGE_EXPR, op0, op1,
1244                                  &edge_info->cond_equivalences);
1245       build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1246                                  &edge_info->cond_equivalences);
1247       build_and_record_new_cond (UNLT_EXPR, op0, op1,
1248                                  &edge_info->cond_equivalences);
1249       build_and_record_new_cond (UNGT_EXPR, op0, op1,
1250                                  &edge_info->cond_equivalences);
1251       break;
1252
1253     case UNLT_EXPR:
1254     case UNGT_EXPR:
1255       build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1256                                   ? UNLE_EXPR : UNGE_EXPR),
1257                                  op0, op1, &edge_info->cond_equivalences);
1258       build_and_record_new_cond (NE_EXPR, op0, op1,
1259                                  &edge_info->cond_equivalences);
1260       break;
1261
1262     case UNEQ_EXPR:
1263       build_and_record_new_cond (UNLE_EXPR, op0, op1,
1264                                  &edge_info->cond_equivalences);
1265       build_and_record_new_cond (UNGE_EXPR, op0, op1,
1266                                  &edge_info->cond_equivalences);
1267       break;
1268
1269     case LTGT_EXPR:
1270       build_and_record_new_cond (NE_EXPR, op0, op1,
1271                                  &edge_info->cond_equivalences);
1272       build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1273                                  &edge_info->cond_equivalences);
1274       break;
1275
1276     default:
1277       break;
1278     }
1279
1280   /* Now store the original true and false conditions into the first
1281      two slots.  */
1282   initialize_expr_from_cond (cond, &c.cond);
1283   c.value = boolean_true_node;
1284   VEC_safe_push (cond_equivalence, heap, edge_info->cond_equivalences, &c);
1285
1286   /* It is possible for INVERTED to be the negation of a comparison,
1287      and not a valid RHS or GIMPLE_COND condition.  This happens because
1288      invert_truthvalue may return such an expression when asked to invert
1289      a floating-point comparison.  These comparisons are not assumed to
1290      obey the trichotomy law.  */
1291   initialize_expr_from_cond (inverted, &c.cond);
1292   c.value = boolean_false_node;
1293   VEC_safe_push (cond_equivalence, heap, edge_info->cond_equivalences, &c);
1294 }
1295
1296 /* A helper function for record_const_or_copy and record_equality.
1297    Do the work of recording the value and undo info.  */
1298
1299 static void
1300 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1301 {
1302   set_ssa_name_value (x, y);
1303
1304   if (dump_file && (dump_flags & TDF_DETAILS))
1305     {
1306       fprintf (dump_file, "0>>> COPY ");
1307       print_generic_expr (dump_file, x, 0);
1308       fprintf (dump_file, " = ");
1309       print_generic_expr (dump_file, y, 0);
1310       fprintf (dump_file, "\n");
1311     }
1312
1313   VEC_reserve (tree, heap, const_and_copies_stack, 2);
1314   VEC_quick_push (tree, const_and_copies_stack, prev_x);
1315   VEC_quick_push (tree, const_and_copies_stack, x);
1316 }
1317
1318 /* Return the loop depth of the basic block of the defining statement of X.
1319    This number should not be treated as absolutely correct because the loop
1320    information may not be completely up-to-date when dom runs.  However, it
1321    will be relatively correct, and as more passes are taught to keep loop info
1322    up to date, the result will become more and more accurate.  */
1323
1324 int
1325 loop_depth_of_name (tree x)
1326 {
1327   gimple defstmt;
1328   basic_block defbb;
1329
1330   /* If it's not an SSA_NAME, we have no clue where the definition is.  */
1331   if (TREE_CODE (x) != SSA_NAME)
1332     return 0;
1333
1334   /* Otherwise return the loop depth of the defining statement's bb.
1335      Note that there may not actually be a bb for this statement, if the
1336      ssa_name is live on entry.  */
1337   defstmt = SSA_NAME_DEF_STMT (x);
1338   defbb = gimple_bb (defstmt);
1339   if (!defbb)
1340     return 0;
1341
1342   return defbb->loop_depth;
1343 }
1344
1345 /* Record that X is equal to Y in const_and_copies.  Record undo
1346    information in the block-local vector.  */
1347
1348 static void
1349 record_const_or_copy (tree x, tree y)
1350 {
1351   tree prev_x = SSA_NAME_VALUE (x);
1352
1353   gcc_assert (TREE_CODE (x) == SSA_NAME);
1354
1355   if (TREE_CODE (y) == SSA_NAME)
1356     {
1357       tree tmp = SSA_NAME_VALUE (y);
1358       if (tmp)
1359         y = tmp;
1360     }
1361
1362   record_const_or_copy_1 (x, y, prev_x);
1363 }
1364
1365 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1366    This constrains the cases in which we may treat this as assignment.  */
1367
1368 static void
1369 record_equality (tree x, tree y)
1370 {
1371   tree prev_x = NULL, prev_y = NULL;
1372
1373   if (TREE_CODE (x) == SSA_NAME)
1374     prev_x = SSA_NAME_VALUE (x);
1375   if (TREE_CODE (y) == SSA_NAME)
1376     prev_y = SSA_NAME_VALUE (y);
1377
1378   /* If one of the previous values is invariant, or invariant in more loops
1379      (by depth), then use that.
1380      Otherwise it doesn't matter which value we choose, just so
1381      long as we canonicalize on one value.  */
1382   if (is_gimple_min_invariant (y))
1383     ;
1384   else if (is_gimple_min_invariant (x)
1385            || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1386     prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1387   else if (prev_x && is_gimple_min_invariant (prev_x))
1388     x = y, y = prev_x, prev_x = prev_y;
1389   else if (prev_y)
1390     y = prev_y;
1391
1392   /* After the swapping, we must have one SSA_NAME.  */
1393   if (TREE_CODE (x) != SSA_NAME)
1394     return;
1395
1396   /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1397      variable compared against zero.  If we're honoring signed zeros,
1398      then we cannot record this value unless we know that the value is
1399      nonzero.  */
1400   if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1401       && (TREE_CODE (y) != REAL_CST
1402           || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1403     return;
1404
1405   record_const_or_copy_1 (x, y, prev_x);
1406 }
1407
1408 /* Returns true when STMT is a simple iv increment.  It detects the
1409    following situation:
1410
1411    i_1 = phi (..., i_2)
1412    i_2 = i_1 +/- ...  */
1413
1414 static bool
1415 simple_iv_increment_p (gimple stmt)
1416 {
1417   tree lhs, preinc;
1418   gimple phi;
1419   size_t i;
1420
1421   if (gimple_code (stmt) != GIMPLE_ASSIGN)
1422     return false;
1423
1424   lhs = gimple_assign_lhs (stmt);
1425   if (TREE_CODE (lhs) != SSA_NAME)
1426     return false;
1427
1428   if (gimple_assign_rhs_code (stmt) != PLUS_EXPR
1429       && gimple_assign_rhs_code (stmt) != MINUS_EXPR)
1430     return false;
1431
1432   preinc = gimple_assign_rhs1 (stmt);
1433
1434   if (TREE_CODE (preinc) != SSA_NAME)
1435     return false;
1436
1437   phi = SSA_NAME_DEF_STMT (preinc);
1438   if (gimple_code (phi) != GIMPLE_PHI)
1439     return false;
1440
1441   for (i = 0; i < gimple_phi_num_args (phi); i++)
1442     if (gimple_phi_arg_def (phi, i) == lhs)
1443       return true;
1444
1445   return false;
1446 }
1447
1448 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1449    known value for that SSA_NAME (or NULL if no value is known).
1450
1451    Propagate values from CONST_AND_COPIES into the PHI nodes of the
1452    successors of BB.  */
1453
1454 static void
1455 cprop_into_successor_phis (basic_block bb)
1456 {
1457   edge e;
1458   edge_iterator ei;
1459
1460   FOR_EACH_EDGE (e, ei, bb->succs)
1461     {
1462       int indx;
1463       gimple_stmt_iterator gsi;
1464
1465       /* If this is an abnormal edge, then we do not want to copy propagate
1466          into the PHI alternative associated with this edge.  */
1467       if (e->flags & EDGE_ABNORMAL)
1468         continue;
1469
1470       gsi = gsi_start_phis (e->dest);
1471       if (gsi_end_p (gsi))
1472         continue;
1473
1474       indx = e->dest_idx;
1475       for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1476         {
1477           tree new_val;
1478           use_operand_p orig_p;
1479           tree orig_val;
1480           gimple phi = gsi_stmt (gsi);
1481
1482           /* The alternative may be associated with a constant, so verify
1483              it is an SSA_NAME before doing anything with it.  */
1484           orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1485           orig_val = get_use_from_ptr (orig_p);
1486           if (TREE_CODE (orig_val) != SSA_NAME)
1487             continue;
1488
1489           /* If we have *ORIG_P in our constant/copy table, then replace
1490              ORIG_P with its value in our constant/copy table.  */
1491           new_val = SSA_NAME_VALUE (orig_val);
1492           if (new_val
1493               && new_val != orig_val
1494               && (TREE_CODE (new_val) == SSA_NAME
1495                   || is_gimple_min_invariant (new_val))
1496               && may_propagate_copy (orig_val, new_val))
1497             propagate_value (orig_p, new_val);
1498         }
1499     }
1500 }
1501
1502 /* We have finished optimizing BB, record any information implied by
1503    taking a specific outgoing edge from BB.  */
1504
1505 static void
1506 record_edge_info (basic_block bb)
1507 {
1508   gimple_stmt_iterator gsi = gsi_last_bb (bb);
1509   struct edge_info *edge_info;
1510
1511   if (! gsi_end_p (gsi))
1512     {
1513       gimple stmt = gsi_stmt (gsi);
1514       location_t loc = gimple_location (stmt);
1515
1516       if (gimple_code (stmt) == GIMPLE_SWITCH)
1517         {
1518           tree index = gimple_switch_index (stmt);
1519
1520           if (TREE_CODE (index) == SSA_NAME)
1521             {
1522               int i;
1523               int n_labels = gimple_switch_num_labels (stmt);
1524               tree *info = XCNEWVEC (tree, last_basic_block);
1525               edge e;
1526               edge_iterator ei;
1527
1528               for (i = 0; i < n_labels; i++)
1529                 {
1530                   tree label = gimple_switch_label (stmt, i);
1531                   basic_block target_bb = label_to_block (CASE_LABEL (label));
1532                   if (CASE_HIGH (label)
1533                       || !CASE_LOW (label)
1534                       || info[target_bb->index])
1535                     info[target_bb->index] = error_mark_node;
1536                   else
1537                     info[target_bb->index] = label;
1538                 }
1539
1540               FOR_EACH_EDGE (e, ei, bb->succs)
1541                 {
1542                   basic_block target_bb = e->dest;
1543                   tree label = info[target_bb->index];
1544
1545                   if (label != NULL && label != error_mark_node)
1546                     {
1547                       tree x = fold_convert_loc (loc, TREE_TYPE (index),
1548                                                  CASE_LOW (label));
1549                       edge_info = allocate_edge_info (e);
1550                       edge_info->lhs = index;
1551                       edge_info->rhs = x;
1552                     }
1553                 }
1554               free (info);
1555             }
1556         }
1557
1558       /* A COND_EXPR may create equivalences too.  */
1559       if (gimple_code (stmt) == GIMPLE_COND)
1560         {
1561           edge true_edge;
1562           edge false_edge;
1563
1564           tree op0 = gimple_cond_lhs (stmt);
1565           tree op1 = gimple_cond_rhs (stmt);
1566           enum tree_code code = gimple_cond_code (stmt);
1567
1568           extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1569
1570           /* Special case comparing booleans against a constant as we
1571              know the value of OP0 on both arms of the branch.  i.e., we
1572              can record an equivalence for OP0 rather than COND.  */
1573           if ((code == EQ_EXPR || code == NE_EXPR)
1574               && TREE_CODE (op0) == SSA_NAME
1575               && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1576               && is_gimple_min_invariant (op1))
1577             {
1578               if (code == EQ_EXPR)
1579                 {
1580                   edge_info = allocate_edge_info (true_edge);
1581                   edge_info->lhs = op0;
1582                   edge_info->rhs = (integer_zerop (op1)
1583                                     ? boolean_false_node
1584                                     : boolean_true_node);
1585
1586                   edge_info = allocate_edge_info (false_edge);
1587                   edge_info->lhs = op0;
1588                   edge_info->rhs = (integer_zerop (op1)
1589                                     ? boolean_true_node
1590                                     : boolean_false_node);
1591                 }
1592               else
1593                 {
1594                   edge_info = allocate_edge_info (true_edge);
1595                   edge_info->lhs = op0;
1596                   edge_info->rhs = (integer_zerop (op1)
1597                                     ? boolean_true_node
1598                                     : boolean_false_node);
1599
1600                   edge_info = allocate_edge_info (false_edge);
1601                   edge_info->lhs = op0;
1602                   edge_info->rhs = (integer_zerop (op1)
1603                                     ? boolean_false_node
1604                                     : boolean_true_node);
1605                 }
1606             }
1607           else if (is_gimple_min_invariant (op0)
1608                    && (TREE_CODE (op1) == SSA_NAME
1609                        || is_gimple_min_invariant (op1)))
1610             {
1611               tree cond = build2 (code, boolean_type_node, op0, op1);
1612               tree inverted = invert_truthvalue_loc (loc, cond);
1613               struct edge_info *edge_info;
1614
1615               edge_info = allocate_edge_info (true_edge);
1616               record_conditions (edge_info, cond, inverted);
1617
1618               if (code == EQ_EXPR)
1619                 {
1620                   edge_info->lhs = op1;
1621                   edge_info->rhs = op0;
1622                 }
1623
1624               edge_info = allocate_edge_info (false_edge);
1625               record_conditions (edge_info, inverted, cond);
1626
1627               if (TREE_CODE (inverted) == EQ_EXPR)
1628                 {
1629                   edge_info->lhs = op1;
1630                   edge_info->rhs = op0;
1631                 }
1632             }
1633
1634           else if (TREE_CODE (op0) == SSA_NAME
1635                    && (is_gimple_min_invariant (op1)
1636                        || TREE_CODE (op1) == SSA_NAME))
1637             {
1638               tree cond = build2 (code, boolean_type_node, op0, op1);
1639               tree inverted = invert_truthvalue_loc (loc, cond);
1640               struct edge_info *edge_info;
1641
1642               edge_info = allocate_edge_info (true_edge);
1643               record_conditions (edge_info, cond, inverted);
1644
1645               if (code == EQ_EXPR)
1646                 {
1647                   edge_info->lhs = op0;
1648                   edge_info->rhs = op1;
1649                 }
1650
1651               edge_info = allocate_edge_info (false_edge);
1652               record_conditions (edge_info, inverted, cond);
1653
1654               if (TREE_CODE (inverted) == EQ_EXPR)
1655                 {
1656                   edge_info->lhs = op0;
1657                   edge_info->rhs = op1;
1658                 }
1659             }
1660         }
1661
1662       /* ??? TRUTH_NOT_EXPR can create an equivalence too.  */
1663     }
1664 }
1665
1666 static void
1667 dom_opt_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1668                      basic_block bb)
1669 {
1670   gimple_stmt_iterator gsi;
1671
1672   if (dump_file && (dump_flags & TDF_DETAILS))
1673     fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1674
1675   /* Push a marker on the stacks of local information so that we know how
1676      far to unwind when we finalize this block.  */
1677   VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL);
1678   VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1679
1680   record_equivalences_from_incoming_edge (bb);
1681
1682   /* PHI nodes can create equivalences too.  */
1683   record_equivalences_from_phis (bb);
1684
1685   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1686     optimize_stmt (bb, gsi);
1687
1688   /* Now prepare to process dominated blocks.  */
1689   record_edge_info (bb);
1690   cprop_into_successor_phis (bb);
1691 }
1692
1693 /* We have finished processing the dominator children of BB, perform
1694    any finalization actions in preparation for leaving this node in
1695    the dominator tree.  */
1696
1697 static void
1698 dom_opt_leave_block (struct dom_walk_data *walk_data, basic_block bb)
1699 {
1700   gimple last;
1701
1702   /* If we have an outgoing edge to a block with multiple incoming and
1703      outgoing edges, then we may be able to thread the edge, i.e., we
1704      may be able to statically determine which of the outgoing edges
1705      will be traversed when the incoming edge from BB is traversed.  */
1706   if (single_succ_p (bb)
1707       && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1708       && potentially_threadable_block (single_succ (bb)))
1709     {
1710       dom_thread_across_edge (walk_data, single_succ_edge (bb));
1711     }
1712   else if ((last = last_stmt (bb))
1713            && gimple_code (last) == GIMPLE_COND
1714            && EDGE_COUNT (bb->succs) == 2
1715            && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1716            && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1717     {
1718       edge true_edge, false_edge;
1719
1720       extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1721
1722       /* Only try to thread the edge if it reaches a target block with
1723          more than one predecessor and more than one successor.  */
1724       if (potentially_threadable_block (true_edge->dest))
1725         {
1726           struct edge_info *edge_info;
1727           unsigned int i;
1728
1729           /* Push a marker onto the available expression stack so that we
1730              unwind any expressions related to the TRUE arm before processing
1731              the false arm below.  */
1732           VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL);
1733           VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1734
1735           edge_info = (struct edge_info *) true_edge->aux;
1736
1737           /* If we have info associated with this edge, record it into
1738              our equivalence tables.  */
1739           if (edge_info)
1740             {
1741               cond_equivalence *eq;
1742               tree lhs = edge_info->lhs;
1743               tree rhs = edge_info->rhs;
1744
1745               /* If we have a simple NAME = VALUE equivalence, record it.  */
1746               if (lhs && TREE_CODE (lhs) == SSA_NAME)
1747                 record_const_or_copy (lhs, rhs);
1748
1749               /* If we have 0 = COND or 1 = COND equivalences, record them
1750                  into our expression hash tables.  */
1751               for (i = 0; VEC_iterate (cond_equivalence,
1752                                        edge_info->cond_equivalences, i, eq); ++i)
1753                 record_cond (eq);
1754             }
1755
1756           dom_thread_across_edge (walk_data, true_edge);
1757
1758           /* And restore the various tables to their state before
1759              we threaded this edge.  */
1760           remove_local_expressions_from_table ();
1761         }
1762
1763       /* Similarly for the ELSE arm.  */
1764       if (potentially_threadable_block (false_edge->dest))
1765         {
1766           struct edge_info *edge_info;
1767           unsigned int i;
1768
1769           VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1770           edge_info = (struct edge_info *) false_edge->aux;
1771
1772           /* If we have info associated with this edge, record it into
1773              our equivalence tables.  */
1774           if (edge_info)
1775             {
1776               cond_equivalence *eq;
1777               tree lhs = edge_info->lhs;
1778               tree rhs = edge_info->rhs;
1779
1780               /* If we have a simple NAME = VALUE equivalence, record it.  */
1781               if (lhs && TREE_CODE (lhs) == SSA_NAME)
1782                 record_const_or_copy (lhs, rhs);
1783
1784               /* If we have 0 = COND or 1 = COND equivalences, record them
1785                  into our expression hash tables.  */
1786               for (i = 0; VEC_iterate (cond_equivalence,
1787                                        edge_info->cond_equivalences, i, eq); ++i)
1788                 record_cond (eq);
1789             }
1790
1791           /* Now thread the edge.  */
1792           dom_thread_across_edge (walk_data, false_edge);
1793
1794           /* No need to remove local expressions from our tables
1795              or restore vars to their original value as that will
1796              be done immediately below.  */
1797         }
1798     }
1799
1800   remove_local_expressions_from_table ();
1801   restore_vars_to_original_value ();
1802 }
1803
1804 /* Search for redundant computations in STMT.  If any are found, then
1805    replace them with the variable holding the result of the computation.
1806
1807    If safe, record this expression into the available expression hash
1808    table.  */
1809
1810 static void
1811 eliminate_redundant_computations (gimple_stmt_iterator* gsi)
1812 {
1813   tree expr_type;
1814   tree cached_lhs;
1815   bool insert = true;
1816   bool assigns_var_p = false;
1817
1818   gimple stmt = gsi_stmt (*gsi);
1819
1820   tree def = gimple_get_lhs (stmt);
1821
1822   /* Certain expressions on the RHS can be optimized away, but can not
1823      themselves be entered into the hash tables.  */
1824   if (! def
1825       || TREE_CODE (def) != SSA_NAME
1826       || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1827       || gimple_vdef (stmt)
1828       /* Do not record equivalences for increments of ivs.  This would create
1829          overlapping live ranges for a very questionable gain.  */
1830       || simple_iv_increment_p (stmt))
1831     insert = false;
1832
1833   /* Check if the expression has been computed before.  */
1834   cached_lhs = lookup_avail_expr (stmt, insert);
1835
1836   opt_stats.num_exprs_considered++;
1837
1838   /* Get the type of the expression we are trying to optimize.  */
1839   if (is_gimple_assign (stmt))
1840     {
1841       expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1842       assigns_var_p = true;
1843     }
1844   else if (gimple_code (stmt) == GIMPLE_COND)
1845     expr_type = boolean_type_node;
1846   else if (is_gimple_call (stmt))
1847     {
1848       gcc_assert (gimple_call_lhs (stmt));
1849       expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1850       assigns_var_p = true;
1851     }
1852   else if (gimple_code (stmt) == GIMPLE_SWITCH)
1853     expr_type = TREE_TYPE (gimple_switch_index (stmt));
1854   else
1855     gcc_unreachable ();
1856
1857   if (!cached_lhs)
1858     return;
1859
1860   /* It is safe to ignore types here since we have already done
1861      type checking in the hashing and equality routines.  In fact
1862      type checking here merely gets in the way of constant
1863      propagation.  Also, make sure that it is safe to propagate
1864      CACHED_LHS into the expression in STMT.  */
1865   if ((TREE_CODE (cached_lhs) != SSA_NAME
1866        && (assigns_var_p
1867            || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1868       || may_propagate_copy_into_stmt (stmt, cached_lhs))
1869   {
1870       gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1871                            || is_gimple_min_invariant (cached_lhs));
1872
1873       if (dump_file && (dump_flags & TDF_DETAILS))
1874         {
1875           fprintf (dump_file, "  Replaced redundant expr '");
1876           print_gimple_expr (dump_file, stmt, 0, dump_flags);
1877           fprintf (dump_file, "' with '");
1878           print_generic_expr (dump_file, cached_lhs, dump_flags);
1879           fprintf (dump_file, "'\n");
1880         }
1881
1882       opt_stats.num_re++;
1883
1884       if (assigns_var_p
1885           && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1886         cached_lhs = fold_convert (expr_type, cached_lhs);
1887
1888       propagate_tree_value_into_stmt (gsi, cached_lhs);
1889
1890       /* Since it is always necessary to mark the result as modified,
1891          perhaps we should move this into propagate_tree_value_into_stmt
1892          itself.  */
1893       gimple_set_modified (gsi_stmt (*gsi), true);
1894   }
1895 }
1896
1897 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1898    the available expressions table or the const_and_copies table.
1899    Detect and record those equivalences.  */
1900 /* We handle only very simple copy equivalences here.  The heavy
1901    lifing is done by eliminate_redundant_computations.  */
1902
1903 static void
1904 record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
1905 {
1906   tree lhs;
1907   enum tree_code lhs_code;
1908
1909   gcc_assert (is_gimple_assign (stmt));
1910
1911   lhs = gimple_assign_lhs (stmt);
1912   lhs_code = TREE_CODE (lhs);
1913
1914   if (lhs_code == SSA_NAME
1915       && gimple_assign_single_p (stmt))
1916     {
1917       tree rhs = gimple_assign_rhs1 (stmt);
1918
1919       /* If the RHS of the assignment is a constant or another variable that
1920          may be propagated, register it in the CONST_AND_COPIES table.  We
1921          do not need to record unwind data for this, since this is a true
1922          assignment and not an equivalence inferred from a comparison.  All
1923          uses of this ssa name are dominated by this assignment, so unwinding
1924          just costs time and space.  */
1925       if (may_optimize_p
1926           && (TREE_CODE (rhs) == SSA_NAME
1927               || is_gimple_min_invariant (rhs)))
1928       {
1929         if (dump_file && (dump_flags & TDF_DETAILS))
1930           {
1931             fprintf (dump_file, "==== ASGN ");
1932             print_generic_expr (dump_file, lhs, 0);
1933             fprintf (dump_file, " = ");
1934             print_generic_expr (dump_file, rhs, 0);
1935             fprintf (dump_file, "\n");
1936           }
1937
1938         set_ssa_name_value (lhs, rhs);
1939       }
1940     }
1941
1942   /* A memory store, even an aliased store, creates a useful
1943      equivalence.  By exchanging the LHS and RHS, creating suitable
1944      vops and recording the result in the available expression table,
1945      we may be able to expose more redundant loads.  */
1946   if (!gimple_has_volatile_ops (stmt)
1947       && gimple_references_memory_p (stmt)
1948       && gimple_assign_single_p (stmt)
1949       && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1950           || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1951       && !is_gimple_reg (lhs))
1952     {
1953       tree rhs = gimple_assign_rhs1 (stmt);
1954       gimple new_stmt;
1955
1956       /* Build a new statement with the RHS and LHS exchanged.  */
1957       if (TREE_CODE (rhs) == SSA_NAME)
1958         {
1959           /* NOTE tuples.  The call to gimple_build_assign below replaced
1960              a call to build_gimple_modify_stmt, which did not set the
1961              SSA_NAME_DEF_STMT on the LHS of the assignment.  Doing so
1962              may cause an SSA validation failure, as the LHS may be a
1963              default-initialized name and should have no definition.  I'm
1964              a bit dubious of this, as the artificial statement that we
1965              generate here may in fact be ill-formed, but it is simply
1966              used as an internal device in this pass, and never becomes
1967              part of the CFG.  */
1968           gimple defstmt = SSA_NAME_DEF_STMT (rhs);
1969           new_stmt = gimple_build_assign (rhs, lhs);
1970           SSA_NAME_DEF_STMT (rhs) = defstmt;
1971         }
1972       else
1973         new_stmt = gimple_build_assign (rhs, lhs);
1974
1975       gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1976
1977       /* Finally enter the statement into the available expression
1978          table.  */
1979       lookup_avail_expr (new_stmt, true);
1980     }
1981 }
1982
1983 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1984    CONST_AND_COPIES.  */
1985
1986 static void
1987 cprop_operand (gimple stmt, use_operand_p op_p)
1988 {
1989   tree val;
1990   tree op = USE_FROM_PTR (op_p);
1991
1992   /* If the operand has a known constant value or it is known to be a
1993      copy of some other variable, use the value or copy stored in
1994      CONST_AND_COPIES.  */
1995   val = SSA_NAME_VALUE (op);
1996   if (val && val != op)
1997     {
1998       /* Do not change the base variable in the virtual operand
1999          tables.  That would make it impossible to reconstruct
2000          the renamed virtual operand if we later modify this
2001          statement.  Also only allow the new value to be an SSA_NAME
2002          for propagation into virtual operands.  */
2003       if (!is_gimple_reg (op)
2004           && (TREE_CODE (val) != SSA_NAME
2005               || is_gimple_reg (val)
2006               || get_virtual_var (val) != get_virtual_var (op)))
2007         return;
2008
2009       /* Do not replace hard register operands in asm statements.  */
2010       if (gimple_code (stmt) == GIMPLE_ASM
2011           && !may_propagate_copy_into_asm (op))
2012         return;
2013
2014       /* Certain operands are not allowed to be copy propagated due
2015          to their interaction with exception handling and some GCC
2016          extensions.  */
2017       if (!may_propagate_copy (op, val))
2018         return;
2019
2020       /* Do not propagate addresses that point to volatiles into memory
2021          stmts without volatile operands.  */
2022       if (POINTER_TYPE_P (TREE_TYPE (val))
2023           && TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (val)))
2024           && gimple_has_mem_ops (stmt)
2025           && !gimple_has_volatile_ops (stmt))
2026         return;
2027
2028       /* Do not propagate copies if the propagated value is at a deeper loop
2029          depth than the propagatee.  Otherwise, this may move loop variant
2030          variables outside of their loops and prevent coalescing
2031          opportunities.  If the value was loop invariant, it will be hoisted
2032          by LICM and exposed for copy propagation.  */
2033       if (loop_depth_of_name (val) > loop_depth_of_name (op))
2034         return;
2035
2036       /* Do not propagate copies into simple IV increment statements.
2037          See PR23821 for how this can disturb IV analysis.  */
2038       if (TREE_CODE (val) != INTEGER_CST
2039           && simple_iv_increment_p (stmt))
2040         return;
2041
2042       /* Dump details.  */
2043       if (dump_file && (dump_flags & TDF_DETAILS))
2044         {
2045           fprintf (dump_file, "  Replaced '");
2046           print_generic_expr (dump_file, op, dump_flags);
2047           fprintf (dump_file, "' with %s '",
2048                    (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2049           print_generic_expr (dump_file, val, dump_flags);
2050           fprintf (dump_file, "'\n");
2051         }
2052
2053       if (TREE_CODE (val) != SSA_NAME)
2054         opt_stats.num_const_prop++;
2055       else
2056         opt_stats.num_copy_prop++;
2057
2058       propagate_value (op_p, val);
2059
2060       /* And note that we modified this statement.  This is now
2061          safe, even if we changed virtual operands since we will
2062          rescan the statement and rewrite its operands again.  */
2063       gimple_set_modified (stmt, true);
2064     }
2065 }
2066
2067 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2068    known value for that SSA_NAME (or NULL if no value is known).
2069
2070    Propagate values from CONST_AND_COPIES into the uses, vuses and
2071    vdef_ops of STMT.  */
2072
2073 static void
2074 cprop_into_stmt (gimple stmt)
2075 {
2076   use_operand_p op_p;
2077   ssa_op_iter iter;
2078
2079   FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
2080     {
2081       if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
2082         cprop_operand (stmt, op_p);
2083     }
2084 }
2085
2086 /* Optimize the statement pointed to by iterator SI.
2087
2088    We try to perform some simplistic global redundancy elimination and
2089    constant propagation:
2090
2091    1- To detect global redundancy, we keep track of expressions that have
2092       been computed in this block and its dominators.  If we find that the
2093       same expression is computed more than once, we eliminate repeated
2094       computations by using the target of the first one.
2095
2096    2- Constant values and copy assignments.  This is used to do very
2097       simplistic constant and copy propagation.  When a constant or copy
2098       assignment is found, we map the value on the RHS of the assignment to
2099       the variable in the LHS in the CONST_AND_COPIES table.  */
2100
2101 static void
2102 optimize_stmt (basic_block bb, gimple_stmt_iterator si)
2103 {
2104   gimple stmt, old_stmt;
2105   bool may_optimize_p;
2106   bool modified_p = false;
2107
2108   old_stmt = stmt = gsi_stmt (si);
2109
2110   if (gimple_code (stmt) == GIMPLE_COND)
2111     canonicalize_comparison (stmt);
2112
2113   update_stmt_if_modified (stmt);
2114   opt_stats.num_stmts++;
2115
2116   if (dump_file && (dump_flags & TDF_DETAILS))
2117     {
2118       fprintf (dump_file, "Optimizing statement ");
2119       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2120     }
2121
2122   /* Const/copy propagate into USES, VUSES and the RHS of VDEFs.  */
2123   cprop_into_stmt (stmt);
2124
2125   /* If the statement has been modified with constant replacements,
2126      fold its RHS before checking for redundant computations.  */
2127   if (gimple_modified_p (stmt))
2128     {
2129       tree rhs = NULL;
2130
2131       /* Try to fold the statement making sure that STMT is kept
2132          up to date.  */
2133       if (fold_stmt (&si))
2134         {
2135           stmt = gsi_stmt (si);
2136           gimple_set_modified (stmt, true);
2137
2138           if (dump_file && (dump_flags & TDF_DETAILS))
2139             {
2140               fprintf (dump_file, "  Folded to: ");
2141               print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2142             }
2143         }
2144
2145       /* We only need to consider cases that can yield a gimple operand.  */
2146       if (gimple_assign_single_p (stmt))
2147         rhs = gimple_assign_rhs1 (stmt);
2148       else if (gimple_code (stmt) == GIMPLE_GOTO)
2149         rhs = gimple_goto_dest (stmt);
2150       else if (gimple_code (stmt) == GIMPLE_SWITCH)
2151         /* This should never be an ADDR_EXPR.  */
2152         rhs = gimple_switch_index (stmt);
2153
2154       if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2155         recompute_tree_invariant_for_addr_expr (rhs);
2156
2157       /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2158          even if fold_stmt updated the stmt already and thus cleared
2159          gimple_modified_p flag on it.  */
2160       modified_p = true;
2161     }
2162
2163   /* Check for redundant computations.  Do this optimization only
2164      for assignments that have no volatile ops and conditionals.  */
2165   may_optimize_p = (!gimple_has_volatile_ops (stmt)
2166                     && ((is_gimple_assign (stmt)
2167                          && !gimple_rhs_has_side_effects (stmt))
2168                         || (is_gimple_call (stmt)
2169                             && gimple_call_lhs (stmt) != NULL_TREE
2170                             && !gimple_rhs_has_side_effects (stmt))
2171                         || gimple_code (stmt) == GIMPLE_COND
2172                         || gimple_code (stmt) == GIMPLE_SWITCH));
2173
2174   if (may_optimize_p)
2175     {
2176       if (gimple_code (stmt) == GIMPLE_CALL)
2177         {
2178           /* Resolve __builtin_constant_p.  If it hasn't been
2179              folded to integer_one_node by now, it's fairly
2180              certain that the value simply isn't constant.  */
2181           tree callee = gimple_call_fndecl (stmt);
2182           if (callee
2183               && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2184               && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2185             {
2186               propagate_tree_value_into_stmt (&si, integer_zero_node);
2187               stmt = gsi_stmt (si);
2188             }
2189         }
2190
2191       update_stmt_if_modified (stmt);
2192       eliminate_redundant_computations (&si);
2193       stmt = gsi_stmt (si);
2194
2195       /* Perform simple redundant store elimination.  */
2196       if (gimple_assign_single_p (stmt)
2197           && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2198         {
2199           tree lhs = gimple_assign_lhs (stmt);
2200           tree rhs = gimple_assign_rhs1 (stmt);
2201           tree cached_lhs;
2202           gimple new_stmt;
2203           if (TREE_CODE (rhs) == SSA_NAME)
2204             {
2205               tree tem = SSA_NAME_VALUE (rhs);
2206               if (tem)
2207                 rhs = tem;
2208             }
2209           /* Build a new statement with the RHS and LHS exchanged.  */
2210           if (TREE_CODE (rhs) == SSA_NAME)
2211             {
2212               gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2213               new_stmt = gimple_build_assign (rhs, lhs);
2214               SSA_NAME_DEF_STMT (rhs) = defstmt;
2215             }
2216           else
2217             new_stmt = gimple_build_assign (rhs, lhs);
2218           gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2219           cached_lhs = lookup_avail_expr (new_stmt, false);
2220           if (cached_lhs
2221               && rhs == cached_lhs)
2222             {
2223               basic_block bb = gimple_bb (stmt);
2224               int lp_nr = lookup_stmt_eh_lp (stmt);
2225               unlink_stmt_vdef (stmt);
2226               gsi_remove (&si, true);
2227               if (lp_nr != 0)
2228                 {
2229                   bitmap_set_bit (need_eh_cleanup, bb->index);
2230                   if (dump_file && (dump_flags & TDF_DETAILS))
2231                     fprintf (dump_file, "  Flagged to clear EH edges.\n");
2232                 }
2233               return;
2234             }
2235         }
2236     }
2237
2238   /* Record any additional equivalences created by this statement.  */
2239   if (is_gimple_assign (stmt))
2240     record_equivalences_from_stmt (stmt, may_optimize_p);
2241
2242   /* If STMT is a COND_EXPR and it was modified, then we may know
2243      where it goes.  If that is the case, then mark the CFG as altered.
2244
2245      This will cause us to later call remove_unreachable_blocks and
2246      cleanup_tree_cfg when it is safe to do so.  It is not safe to
2247      clean things up here since removal of edges and such can trigger
2248      the removal of PHI nodes, which in turn can release SSA_NAMEs to
2249      the manager.
2250
2251      That's all fine and good, except that once SSA_NAMEs are released
2252      to the manager, we must not call create_ssa_name until all references
2253      to released SSA_NAMEs have been eliminated.
2254
2255      All references to the deleted SSA_NAMEs can not be eliminated until
2256      we remove unreachable blocks.
2257
2258      We can not remove unreachable blocks until after we have completed
2259      any queued jump threading.
2260
2261      We can not complete any queued jump threads until we have taken
2262      appropriate variables out of SSA form.  Taking variables out of
2263      SSA form can call create_ssa_name and thus we lose.
2264
2265      Ultimately I suspect we're going to need to change the interface
2266      into the SSA_NAME manager.  */
2267   if (gimple_modified_p (stmt) || modified_p)
2268     {
2269       tree val = NULL;
2270
2271       update_stmt_if_modified (stmt);
2272
2273       if (gimple_code (stmt) == GIMPLE_COND)
2274         val = fold_binary_loc (gimple_location (stmt),
2275                            gimple_cond_code (stmt), boolean_type_node,
2276                            gimple_cond_lhs (stmt),  gimple_cond_rhs (stmt));
2277       else if (gimple_code (stmt) == GIMPLE_SWITCH)
2278         val = gimple_switch_index (stmt);
2279
2280       if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2281         cfg_altered = true;
2282
2283       /* If we simplified a statement in such a way as to be shown that it
2284          cannot trap, update the eh information and the cfg to match.  */
2285       if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2286         {
2287           bitmap_set_bit (need_eh_cleanup, bb->index);
2288           if (dump_file && (dump_flags & TDF_DETAILS))
2289             fprintf (dump_file, "  Flagged to clear EH edges.\n");
2290         }
2291     }
2292 }
2293
2294 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2295    If found, return its LHS. Otherwise insert STMT in the table and
2296    return NULL_TREE.
2297
2298    Also, when an expression is first inserted in the  table, it is also
2299    is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2300    we finish processing this block and its children.  */
2301
2302 static tree
2303 lookup_avail_expr (gimple stmt, bool insert)
2304 {
2305   void **slot;
2306   tree lhs;
2307   tree temp;
2308   struct expr_hash_elt element;
2309
2310   /* Get LHS of assignment or call, else NULL_TREE.  */
2311   lhs = gimple_get_lhs (stmt);
2312
2313   initialize_hash_element (stmt, lhs, &element);
2314
2315   if (dump_file && (dump_flags & TDF_DETAILS))
2316     {
2317       fprintf (dump_file, "LKUP ");
2318       print_expr_hash_elt (dump_file, &element);
2319     }
2320
2321   /* Don't bother remembering constant assignments and copy operations.
2322      Constants and copy operations are handled by the constant/copy propagator
2323      in optimize_stmt.  */
2324   if (element.expr.kind == EXPR_SINGLE
2325       && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2326           || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2327     return NULL_TREE;
2328
2329   /* Finally try to find the expression in the main expression hash table.  */
2330   slot = htab_find_slot_with_hash (avail_exprs, &element, element.hash,
2331                                    (insert ? INSERT : NO_INSERT));
2332   if (slot == NULL)
2333     return NULL_TREE;
2334
2335   if (*slot == NULL)
2336     {
2337       struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2338       *element2 = element;
2339       element2->stamp = element2;
2340       *slot = (void *) element2;
2341
2342       if (dump_file && (dump_flags & TDF_DETAILS))
2343         {
2344           fprintf (dump_file, "2>>> ");
2345           print_expr_hash_elt (dump_file, element2);
2346         }
2347
2348       VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element2);
2349       return NULL_TREE;
2350     }
2351
2352   /* Extract the LHS of the assignment so that it can be used as the current
2353      definition of another variable.  */
2354   lhs = ((struct expr_hash_elt *)*slot)->lhs;
2355
2356   /* See if the LHS appears in the CONST_AND_COPIES table.  If it does, then
2357      use the value from the const_and_copies table.  */
2358   if (TREE_CODE (lhs) == SSA_NAME)
2359     {
2360       temp = SSA_NAME_VALUE (lhs);
2361       if (temp)
2362         lhs = temp;
2363     }
2364
2365   if (dump_file && (dump_flags & TDF_DETAILS))
2366     {
2367       fprintf (dump_file, "FIND: ");
2368       print_generic_expr (dump_file, lhs, 0);
2369       fprintf (dump_file, "\n");
2370     }
2371
2372   return lhs;
2373 }
2374
2375 /* Hashing and equality functions for AVAIL_EXPRS.  We compute a value number
2376    for expressions using the code of the expression and the SSA numbers of
2377    its operands.  */
2378
2379 static hashval_t
2380 avail_expr_hash (const void *p)
2381 {
2382   gimple stmt = ((const struct expr_hash_elt *)p)->stmt;
2383   const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2384   tree vuse;
2385   hashval_t val = 0;
2386
2387   val = iterative_hash_hashable_expr (expr, val);
2388
2389   /* If the hash table entry is not associated with a statement, then we
2390      can just hash the expression and not worry about virtual operands
2391      and such.  */
2392   if (!stmt)
2393     return val;
2394
2395   /* Add the SSA version numbers of the vuse operand.  This is important
2396      because compound variables like arrays are not renamed in the
2397      operands.  Rather, the rename is done on the virtual variable
2398      representing all the elements of the array.  */
2399   if ((vuse = gimple_vuse (stmt)))
2400     val = iterative_hash_expr (vuse, val);
2401
2402   return val;
2403 }
2404
2405 static hashval_t
2406 real_avail_expr_hash (const void *p)
2407 {
2408   return ((const struct expr_hash_elt *)p)->hash;
2409 }
2410
2411 static int
2412 avail_expr_eq (const void *p1, const void *p2)
2413 {
2414   gimple stmt1 = ((const struct expr_hash_elt *)p1)->stmt;
2415   const struct hashable_expr *expr1 = &((const struct expr_hash_elt *)p1)->expr;
2416   const struct expr_hash_elt *stamp1 = ((const struct expr_hash_elt *)p1)->stamp;
2417   gimple stmt2 = ((const struct expr_hash_elt *)p2)->stmt;
2418   const struct hashable_expr *expr2 = &((const struct expr_hash_elt *)p2)->expr;
2419   const struct expr_hash_elt *stamp2 = ((const struct expr_hash_elt *)p2)->stamp;
2420
2421   /* This case should apply only when removing entries from the table.  */
2422   if (stamp1 == stamp2)
2423     return true;
2424
2425   /* FIXME tuples:
2426      We add stmts to a hash table and them modify them. To detect the case
2427      that we modify a stmt and then search for it, we assume that the hash
2428      is always modified by that change.
2429      We have to fully check why this doesn't happen on trunk or rewrite
2430      this in a more  reliable (and easier to understand) way. */
2431   if (((const struct expr_hash_elt *)p1)->hash
2432       != ((const struct expr_hash_elt *)p2)->hash)
2433     return false;
2434
2435   /* In case of a collision, both RHS have to be identical and have the
2436      same VUSE operands.  */
2437   if (hashable_expr_equal_p (expr1, expr2)
2438       && types_compatible_p (expr1->type, expr2->type))
2439     {
2440       /* Note that STMT1 and/or STMT2 may be NULL.  */
2441       return ((stmt1 ? gimple_vuse (stmt1) : NULL_TREE)
2442               == (stmt2 ? gimple_vuse (stmt2) : NULL_TREE));
2443     }
2444
2445   return false;
2446 }
2447
2448 /* PHI-ONLY copy and constant propagation.  This pass is meant to clean
2449    up degenerate PHIs created by or exposed by jump threading.  */
2450
2451 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
2452    NULL.  */
2453
2454 tree
2455 degenerate_phi_result (gimple phi)
2456 {
2457   tree lhs = gimple_phi_result (phi);
2458   tree val = NULL;
2459   size_t i;
2460
2461   /* Ignoring arguments which are the same as LHS, if all the remaining
2462      arguments are the same, then the PHI is a degenerate and has the
2463      value of that common argument.  */
2464   for (i = 0; i < gimple_phi_num_args (phi); i++)
2465     {
2466       tree arg = gimple_phi_arg_def (phi, i);
2467
2468       if (arg == lhs)
2469         continue;
2470       else if (!arg)
2471         break;
2472       else if (!val)
2473         val = arg;
2474       else if (arg == val)
2475         continue;
2476       /* We bring in some of operand_equal_p not only to speed things
2477          up, but also to avoid crashing when dereferencing the type of
2478          a released SSA name.  */
2479       else if (TREE_CODE (val) != TREE_CODE (arg)
2480                || TREE_CODE (val) == SSA_NAME
2481                || !operand_equal_p (arg, val, 0))
2482         break;
2483     }
2484   return (i == gimple_phi_num_args (phi) ? val : NULL);
2485 }
2486
2487 /* Given a statement STMT, which is either a PHI node or an assignment,
2488    remove it from the IL.  */
2489
2490 static void
2491 remove_stmt_or_phi (gimple stmt)
2492 {
2493   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2494
2495   if (gimple_code (stmt) == GIMPLE_PHI)
2496     remove_phi_node (&gsi, true);
2497   else
2498     {
2499       gsi_remove (&gsi, true);
2500       release_defs (stmt);
2501     }
2502 }
2503
2504 /* Given a statement STMT, which is either a PHI node or an assignment,
2505    return the "rhs" of the node, in the case of a non-degenerate
2506    phi, NULL is returned.  */
2507
2508 static tree
2509 get_rhs_or_phi_arg (gimple stmt)
2510 {
2511   if (gimple_code (stmt) == GIMPLE_PHI)
2512     return degenerate_phi_result (stmt);
2513   else if (gimple_assign_single_p (stmt))
2514     return gimple_assign_rhs1 (stmt);
2515   else
2516     gcc_unreachable ();
2517 }
2518
2519
2520 /* Given a statement STMT, which is either a PHI node or an assignment,
2521    return the "lhs" of the node.  */
2522
2523 static tree
2524 get_lhs_or_phi_result (gimple stmt)
2525 {
2526   if (gimple_code (stmt) == GIMPLE_PHI)
2527     return gimple_phi_result (stmt);
2528   else if (is_gimple_assign (stmt))
2529     return gimple_assign_lhs (stmt);
2530   else
2531     gcc_unreachable ();
2532 }
2533
2534 /* Propagate RHS into all uses of LHS (when possible).
2535
2536    RHS and LHS are derived from STMT, which is passed in solely so
2537    that we can remove it if propagation is successful.
2538
2539    When propagating into a PHI node or into a statement which turns
2540    into a trivial copy or constant initialization, set the
2541    appropriate bit in INTERESTING_NAMEs so that we will visit those
2542    nodes as well in an effort to pick up secondary optimization
2543    opportunities.  */
2544
2545 static void
2546 propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
2547 {
2548   /* First verify that propagation is valid and isn't going to move a
2549      loop variant variable outside its loop.  */
2550   if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
2551       && (TREE_CODE (rhs) != SSA_NAME
2552           || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2553       && may_propagate_copy (lhs, rhs)
2554       && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs))
2555     {
2556       use_operand_p use_p;
2557       imm_use_iterator iter;
2558       gimple use_stmt;
2559       bool all = true;
2560
2561       /* Dump details.  */
2562       if (dump_file && (dump_flags & TDF_DETAILS))
2563         {
2564           fprintf (dump_file, "  Replacing '");
2565           print_generic_expr (dump_file, lhs, dump_flags);
2566           fprintf (dump_file, "' with %s '",
2567                    (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2568                    print_generic_expr (dump_file, rhs, dump_flags);
2569           fprintf (dump_file, "'\n");
2570         }
2571
2572       /* Walk over every use of LHS and try to replace the use with RHS.
2573          At this point the only reason why such a propagation would not
2574          be successful would be if the use occurs in an ASM_EXPR.  */
2575       FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2576         {
2577           /* Leave debug stmts alone.  If we succeed in propagating
2578              all non-debug uses, we'll drop the DEF, and propagation
2579              into debug stmts will occur then.  */
2580           if (gimple_debug_bind_p (use_stmt))
2581             continue;
2582
2583           /* It's not always safe to propagate into an ASM_EXPR.  */
2584           if (gimple_code (use_stmt) == GIMPLE_ASM
2585               && ! may_propagate_copy_into_asm (lhs))
2586             {
2587               all = false;
2588               continue;
2589             }
2590
2591           /* It's not ok to propagate into the definition stmt of RHS.
2592                 <bb 9>:
2593                   # prephitmp.12_36 = PHI <g_67.1_6(9)>
2594                   g_67.1_6 = prephitmp.12_36;
2595                   goto <bb 9>;
2596              While this is strictly all dead code we do not want to
2597              deal with this here.  */
2598           if (TREE_CODE (rhs) == SSA_NAME
2599               && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2600             {
2601               all = false;
2602               continue;
2603             }
2604
2605           /* Dump details.  */
2606           if (dump_file && (dump_flags & TDF_DETAILS))
2607             {
2608               fprintf (dump_file, "    Original statement:");
2609               print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2610             }
2611
2612           /* Propagate the RHS into this use of the LHS.  */
2613           FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2614             propagate_value (use_p, rhs);
2615
2616           /* Special cases to avoid useless calls into the folding
2617              routines, operand scanning, etc.
2618
2619              First, propagation into a PHI may cause the PHI to become
2620              a degenerate, so mark the PHI as interesting.  No other
2621              actions are necessary.
2622
2623              Second, if we're propagating a virtual operand and the
2624              propagation does not change the underlying _DECL node for
2625              the virtual operand, then no further actions are necessary.  */
2626           if (gimple_code (use_stmt) == GIMPLE_PHI
2627               || (! is_gimple_reg (lhs)
2628                   && TREE_CODE (rhs) == SSA_NAME
2629                   && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs)))
2630             {
2631               /* Dump details.  */
2632               if (dump_file && (dump_flags & TDF_DETAILS))
2633                 {
2634                   fprintf (dump_file, "    Updated statement:");
2635                   print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2636                 }
2637
2638               /* Propagation into a PHI may expose new degenerate PHIs,
2639                  so mark the result of the PHI as interesting.  */
2640               if (gimple_code (use_stmt) == GIMPLE_PHI)
2641                 {
2642                   tree result = get_lhs_or_phi_result (use_stmt);
2643                   bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2644                 }
2645
2646               continue;
2647             }
2648
2649           /* From this point onward we are propagating into a
2650              real statement.  Folding may (or may not) be possible,
2651              we may expose new operands, expose dead EH edges,
2652              etc.  */
2653           /* NOTE tuples. In the tuples world, fold_stmt_inplace
2654              cannot fold a call that simplifies to a constant,
2655              because the GIMPLE_CALL must be replaced by a
2656              GIMPLE_ASSIGN, and there is no way to effect such a
2657              transformation in-place.  We might want to consider
2658              using the more general fold_stmt here.  */
2659           fold_stmt_inplace (use_stmt);
2660
2661           /* Sometimes propagation can expose new operands to the
2662              renamer.  */
2663           update_stmt (use_stmt);
2664
2665           /* Dump details.  */
2666           if (dump_file && (dump_flags & TDF_DETAILS))
2667             {
2668               fprintf (dump_file, "    Updated statement:");
2669               print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
2670             }
2671
2672           /* If we replaced a variable index with a constant, then
2673              we would need to update the invariant flag for ADDR_EXPRs.  */
2674           if (gimple_assign_single_p (use_stmt)
2675               && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
2676             recompute_tree_invariant_for_addr_expr
2677                 (gimple_assign_rhs1 (use_stmt));
2678
2679           /* If we cleaned up EH information from the statement,
2680              mark its containing block as needing EH cleanups.  */
2681           if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
2682             {
2683               bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
2684               if (dump_file && (dump_flags & TDF_DETAILS))
2685                 fprintf (dump_file, "  Flagged to clear EH edges.\n");
2686             }
2687
2688           /* Propagation may expose new trivial copy/constant propagation
2689              opportunities.  */
2690           if (gimple_assign_single_p (use_stmt)
2691               && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2692               && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2693                   || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2694             {
2695               tree result = get_lhs_or_phi_result (use_stmt);
2696               bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2697             }
2698
2699           /* Propagation into these nodes may make certain edges in
2700              the CFG unexecutable.  We want to identify them as PHI nodes
2701              at the destination of those unexecutable edges may become
2702              degenerates.  */
2703           else if (gimple_code (use_stmt) == GIMPLE_COND
2704                    || gimple_code (use_stmt) == GIMPLE_SWITCH
2705                    || gimple_code (use_stmt) == GIMPLE_GOTO)
2706             {
2707               tree val;
2708
2709               if (gimple_code (use_stmt) == GIMPLE_COND)
2710                 val = fold_binary_loc (gimple_location (use_stmt),
2711                                    gimple_cond_code (use_stmt),
2712                                    boolean_type_node,
2713                                    gimple_cond_lhs (use_stmt),
2714                                    gimple_cond_rhs (use_stmt));
2715               else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
2716                 val = gimple_switch_index (use_stmt);
2717               else
2718                 val = gimple_goto_dest  (use_stmt);
2719
2720               if (val && is_gimple_min_invariant (val))
2721                 {
2722                   basic_block bb = gimple_bb (use_stmt);
2723                   edge te = find_taken_edge (bb, val);
2724                   edge_iterator ei;
2725                   edge e;
2726                   gimple_stmt_iterator gsi, psi;
2727
2728                   /* Remove all outgoing edges except TE.  */
2729                   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2730                     {
2731                       if (e != te)
2732                         {
2733                           /* Mark all the PHI nodes at the destination of
2734                              the unexecutable edge as interesting.  */
2735                           for (psi = gsi_start_phis (e->dest);
2736                                !gsi_end_p (psi);
2737                                gsi_next (&psi))
2738                             {
2739                               gimple phi = gsi_stmt (psi);
2740
2741                               tree result = gimple_phi_result (phi);
2742                               int version = SSA_NAME_VERSION (result);
2743
2744                               bitmap_set_bit (interesting_names, version);
2745                             }
2746
2747                           te->probability += e->probability;
2748
2749                           te->count += e->count;
2750                           remove_edge (e);
2751                           cfg_altered = true;
2752                         }
2753                       else
2754                         ei_next (&ei);
2755                     }
2756
2757                   gsi = gsi_last_bb (gimple_bb (use_stmt));
2758                   gsi_remove (&gsi, true);
2759
2760                   /* And fixup the flags on the single remaining edge.  */
2761                   te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2762                   te->flags &= ~EDGE_ABNORMAL;
2763                   te->flags |= EDGE_FALLTHRU;
2764                   if (te->probability > REG_BR_PROB_BASE)
2765                     te->probability = REG_BR_PROB_BASE;
2766                 }
2767             }
2768         }
2769
2770       /* Ensure there is nothing else to do. */
2771       gcc_assert (!all || has_zero_uses (lhs));
2772
2773       /* If we were able to propagate away all uses of LHS, then
2774          we can remove STMT.  */
2775       if (all)
2776         remove_stmt_or_phi (stmt);
2777     }
2778 }
2779
2780 /* STMT is either a PHI node (potentially a degenerate PHI node) or
2781    a statement that is a trivial copy or constant initialization.
2782
2783    Attempt to eliminate T by propagating its RHS into all uses of
2784    its LHS.  This may in turn set new bits in INTERESTING_NAMES
2785    for nodes we want to revisit later.
2786
2787    All exit paths should clear INTERESTING_NAMES for the result
2788    of STMT.  */
2789
2790 static void
2791 eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
2792 {
2793   tree lhs = get_lhs_or_phi_result (stmt);
2794   tree rhs;
2795   int version = SSA_NAME_VERSION (lhs);
2796
2797   /* If the LHS of this statement or PHI has no uses, then we can
2798      just eliminate it.  This can occur if, for example, the PHI
2799      was created by block duplication due to threading and its only
2800      use was in the conditional at the end of the block which was
2801      deleted.  */
2802   if (has_zero_uses (lhs))
2803     {
2804       bitmap_clear_bit (interesting_names, version);
2805       remove_stmt_or_phi (stmt);
2806       return;
2807     }
2808
2809   /* Get the RHS of the assignment or PHI node if the PHI is a
2810      degenerate.  */
2811   rhs = get_rhs_or_phi_arg (stmt);
2812   if (!rhs)
2813     {
2814       bitmap_clear_bit (interesting_names, version);
2815       return;
2816     }
2817
2818   propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
2819
2820   /* Note that STMT may well have been deleted by now, so do
2821      not access it, instead use the saved version # to clear
2822      T's entry in the worklist.  */
2823   bitmap_clear_bit (interesting_names, version);
2824 }
2825
2826 /* The first phase in degenerate PHI elimination.
2827
2828    Eliminate the degenerate PHIs in BB, then recurse on the
2829    dominator children of BB.  */
2830
2831 static void
2832 eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
2833 {
2834   gimple_stmt_iterator gsi;
2835   basic_block son;
2836
2837   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2838     {
2839       gimple phi = gsi_stmt (gsi);
2840
2841       eliminate_const_or_copy (phi, interesting_names);
2842     }
2843
2844   /* Recurse into the dominator children of BB.  */
2845   for (son = first_dom_son (CDI_DOMINATORS, bb);
2846        son;
2847        son = next_dom_son (CDI_DOMINATORS, son))
2848     eliminate_degenerate_phis_1 (son, interesting_names);
2849 }
2850
2851
2852 /* A very simple pass to eliminate degenerate PHI nodes from the
2853    IL.  This is meant to be fast enough to be able to be run several
2854    times in the optimization pipeline.
2855
2856    Certain optimizations, particularly those which duplicate blocks
2857    or remove edges from the CFG can create or expose PHIs which are
2858    trivial copies or constant initializations.
2859
2860    While we could pick up these optimizations in DOM or with the
2861    combination of copy-prop and CCP, those solutions are far too
2862    heavy-weight for our needs.
2863
2864    This implementation has two phases so that we can efficiently
2865    eliminate the first order degenerate PHIs and second order
2866    degenerate PHIs.
2867
2868    The first phase performs a dominator walk to identify and eliminate
2869    the vast majority of the degenerate PHIs.  When a degenerate PHI
2870    is identified and eliminated any affected statements or PHIs
2871    are put on a worklist.
2872
2873    The second phase eliminates degenerate PHIs and trivial copies
2874    or constant initializations using the worklist.  This is how we
2875    pick up the secondary optimization opportunities with minimal
2876    cost.  */
2877
2878 static unsigned int
2879 eliminate_degenerate_phis (void)
2880 {
2881   bitmap interesting_names;
2882   bitmap interesting_names1;
2883
2884   /* Bitmap of blocks which need EH information updated.  We can not
2885      update it on-the-fly as doing so invalidates the dominator tree.  */
2886   need_eh_cleanup = BITMAP_ALLOC (NULL);
2887
2888   /* INTERESTING_NAMES is effectively our worklist, indexed by
2889      SSA_NAME_VERSION.
2890
2891      A set bit indicates that the statement or PHI node which
2892      defines the SSA_NAME should be (re)examined to determine if
2893      it has become a degenerate PHI or trivial const/copy propagation
2894      opportunity.
2895
2896      Experiments have show we generally get better compilation
2897      time behavior with bitmaps rather than sbitmaps.  */
2898   interesting_names = BITMAP_ALLOC (NULL);
2899   interesting_names1 = BITMAP_ALLOC (NULL);
2900
2901   calculate_dominance_info (CDI_DOMINATORS);
2902   cfg_altered = false;
2903
2904   /* First phase.  Eliminate degenerate PHIs via a dominator
2905      walk of the CFG.
2906
2907      Experiments have indicated that we generally get better
2908      compile-time behavior by visiting blocks in the first
2909      phase in dominator order.  Presumably this is because walking
2910      in dominator order leaves fewer PHIs for later examination
2911      by the worklist phase.  */
2912   eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names);
2913
2914   /* Second phase.  Eliminate second order degenerate PHIs as well
2915      as trivial copies or constant initializations identified by
2916      the first phase or this phase.  Basically we keep iterating
2917      until our set of INTERESTING_NAMEs is empty.   */
2918   while (!bitmap_empty_p (interesting_names))
2919     {
2920       unsigned int i;
2921       bitmap_iterator bi;
2922
2923       /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
2924          changed during the loop.  Copy it to another bitmap and
2925          use that.  */
2926       bitmap_copy (interesting_names1, interesting_names);
2927
2928       EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
2929         {
2930           tree name = ssa_name (i);
2931
2932           /* Ignore SSA_NAMEs that have been released because
2933              their defining statement was deleted (unreachable).  */
2934           if (name)
2935             eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
2936                                      interesting_names);
2937         }
2938     }
2939
2940   if (cfg_altered)
2941     free_dominance_info (CDI_DOMINATORS);
2942
2943   /* Propagation of const and copies may make some EH edges dead.  Purge
2944      such edges from the CFG as needed.  */
2945   if (!bitmap_empty_p (need_eh_cleanup))
2946     {
2947       gimple_purge_all_dead_eh_edges (need_eh_cleanup);
2948       BITMAP_FREE (need_eh_cleanup);
2949     }
2950
2951   BITMAP_FREE (interesting_names);
2952   BITMAP_FREE (interesting_names1);
2953   return 0;
2954 }
2955
2956 struct gimple_opt_pass pass_phi_only_cprop =
2957 {
2958  {
2959   GIMPLE_PASS,
2960   "phicprop",                           /* name */
2961   gate_dominator,                       /* gate */
2962   eliminate_degenerate_phis,            /* execute */
2963   NULL,                                 /* sub */
2964   NULL,                                 /* next */
2965   0,                                    /* static_pass_number */
2966   TV_TREE_PHI_CPROP,                    /* tv_id */
2967   PROP_cfg | PROP_ssa,                  /* properties_required */
2968   0,                                    /* properties_provided */
2969   0,                                    /* properties_destroyed */
2970   0,                                    /* todo_flags_start */
2971   TODO_cleanup_cfg
2972     | TODO_dump_func
2973     | TODO_ggc_collect
2974     | TODO_verify_ssa
2975     | TODO_verify_stmts
2976     | TODO_update_ssa                   /* todo_flags_finish */
2977  }
2978 };