OSDN Git Service

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