OSDN Git Service

* config/m32r/m32r.h (RETURN_ADDR_RTX): Define.
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-dom.c
1 /* SSA Dominator optimizations for trees
2    Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3    Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
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 "output.h"
33 #include "errors.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 "langhooks.h"
44
45 /* This file implements optimizations on the dominator tree.  */
46
47 /* Hash table with expressions made available during the renaming process.
48    When an assignment of the form X_i = EXPR is found, the statement is
49    stored in this table.  If the same expression EXPR is later found on the
50    RHS of another statement, it is replaced with X_i (thus performing
51    global redundancy elimination).  Similarly as we pass through conditionals
52    we record the conditional itself as having either a true or false value
53    in this table.  */
54 static htab_t avail_exprs;
55
56 /* Structure for entries in the expression hash table.
57
58    This requires more memory for the hash table entries, but allows us
59    to avoid creating silly tree nodes and annotations for conditionals,
60    eliminates 2 global hash tables and two block local varrays.
61    
62    It also allows us to reduce the number of hash table lookups we
63    have to perform in lookup_avail_expr and finally it allows us to
64    significantly reduce the number of calls into the hashing routine
65    itself.  */
66 struct expr_hash_elt
67 {
68   /* The value (lhs) of this expression.  */
69   tree lhs;
70
71   /* The expression (rhs) we want to record.  */
72   tree rhs;
73
74   /* The annotation if this element corresponds to a statement.  */
75   stmt_ann_t ann;
76
77   /* The hash value for RHS/ann.  */
78   hashval_t hash;
79 };
80
81 /* Table of constant values and copies indexed by SSA name.  When the
82    renaming pass finds an assignment of a constant (X_i = C) or a copy
83    assignment from another SSA variable (X_i = Y_j), it creates a mapping
84    between X_i and the RHS in this table.  This mapping is used later on,
85    when renaming uses of X_i.  If an assignment to X_i is found in this
86    table, instead of using X_i, we use the RHS of the statement stored in
87    this table (thus performing very simplistic copy and constant
88    propagation).  */
89 static varray_type const_and_copies;
90
91 /* Bitmap of SSA_NAMEs known to have a nonzero value, even if we do not
92    know their exact value.  */
93 static bitmap nonzero_vars;
94
95 /* Track whether or not we have changed the control flow graph.  */
96 static bool cfg_altered;
97
98 /* Statistics for dominator optimizations.  */
99 struct opt_stats_d
100 {
101   long num_stmts;
102   long num_exprs_considered;
103   long num_re;
104 };
105
106 /* Value range propagation record.  Each time we encounter a conditional
107    of the form SSA_NAME COND CONST we create a new vrp_element to record
108    how the condition affects the possible values SSA_NAME may have.
109
110    Each record contains the condition tested (COND), and the the range of
111    values the variable may legitimately have if COND is true.  Note the
112    range of values may be a smaller range than COND specifies if we have
113    recorded other ranges for this variable.  Each record also contains the
114    block in which the range was recorded for invalidation purposes.
115
116    Note that the current known range is computed lazily.  This allows us
117    to avoid the overhead of computing ranges which are never queried.
118
119    When we encounter a conditional, we look for records which constrain
120    the SSA_NAME used in the condition.  In some cases those records allow
121    us to determine the condition's result at compile time.  In other cases
122    they may allow us to simplify the condition.
123
124    We also use value ranges to do things like transform signed div/mod
125    operations into unsigned div/mod or to simplify ABS_EXPRs. 
126
127    Simple experiments have shown these optimizations to not be all that
128    useful on switch statements (much to my surprise).  So switch statement
129    optimizations are not performed.
130
131    Note carefully we do not propagate information through each statement
132    in the block.  ie, if we know variable X has a value defined of
133    [0, 25] and we encounter Y = X + 1, we do not track a value range
134    for Y (which would be [1, 26] if we cared).  Similarly we do not
135    constrain values as we encounter narrowing typecasts, etc.  */
136
137 struct vrp_element
138 {
139   /* The highest and lowest values the variable in COND may contain when
140      COND is true.  Note this may not necessarily be the same values
141      tested by COND if the same variable was used in earlier conditionals. 
142
143      Note this is computed lazily and thus can be NULL indicating that
144      the values have not been computed yet.  */
145   tree low;
146   tree high;
147
148   /* The actual conditional we recorded.  This is needed since we compute
149      ranges lazily.  */
150   tree cond;
151
152   /* The basic block where this record was created.  We use this to determine
153      when to remove records.  */
154   basic_block bb;
155 };
156
157 static struct opt_stats_d opt_stats;
158
159 /* This virtual array holds pairs of edges which describe a scheduled
160    edge redirection from jump threading.
161
162    The first entry in each pair is the edge we are going to redirect.
163
164    The second entry in each pair is the edge leading to our final
165    destination block.  By providing this as an edge rather than the
166    final target block itself we can correctly handle redirections
167    when the target block had PHIs which required edge insertions/splitting
168    to remove the PHIs.  */
169 static GTY(()) varray_type redirection_edges;
170
171 /* A virtual array holding value range records for the variable identified
172    by the index, SSA_VERSION.  */
173 static varray_type vrp_data;
174
175 /* Datastructure for block local data used during the dominator walk.  
176    We maintain a stack of these as we recursively walk down the
177    dominator tree.  */
178
179 struct dom_walk_block_data
180 {
181   /* Array of all the expressions entered into the global expression
182      hash table by this block.  During finalization we use this array to
183      know what expressions to remove from the global expression hash
184      table.  */
185   varray_type avail_exprs;
186
187   /* Array of dest, src pairs that need to be restored during finalization
188      into the global const/copies table during finalization.  */
189   varray_type const_and_copies;
190
191   /* Similarly for the nonzero state of variables that needs to be
192      restored during finalization.  */
193   varray_type nonzero_vars;
194
195   /* Array of statements we need to rescan during finalization for newly
196      exposed variables.  */
197   varray_type stmts_to_rescan;
198
199   /* Array of variables which have their values constrained by operations
200      in this basic block.  We use this during finalization to know
201      which variables need their VRP data updated.  */
202   varray_type vrp_variables;
203
204   /* Array of tree pairs used to restore the global currdefs to its
205      original state after completing optimization of a block and its
206      dominator children.  */
207   varray_type block_defs;
208 };
209
210 struct eq_expr_value
211 {
212   tree src;
213   tree dst;
214 };
215
216 /* Local functions.  */
217 static void optimize_stmt (struct dom_walk_data *, 
218                            basic_block bb,
219                            block_stmt_iterator);
220 static inline tree get_value_for (tree, varray_type table);
221 static inline void set_value_for (tree, tree, varray_type table);
222 static tree lookup_avail_expr (tree, varray_type *, bool);
223 static struct eq_expr_value get_eq_expr_value (tree, int, varray_type *,
224                                                basic_block, varray_type *);
225 static hashval_t avail_expr_hash (const void *);
226 static int avail_expr_eq (const void *, const void *);
227 static void htab_statistics (FILE *, htab_t);
228 static void record_cond (tree, tree, varray_type *);
229 static void record_const_or_copy (tree, tree, varray_type *);
230 static void record_equality (tree, tree, varray_type *);
231 static tree update_rhs_and_lookup_avail_expr (tree, tree, varray_type *,
232                                               stmt_ann_t, bool);
233 static tree simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *,
234                                                 tree, stmt_ann_t, int);
235 static tree simplify_cond_and_lookup_avail_expr (tree, varray_type *,
236                                                  stmt_ann_t, int);
237 static tree simplify_switch_and_lookup_avail_expr (tree, varray_type *,
238                                                    stmt_ann_t, int);
239 static tree find_equivalent_equality_comparison (tree);
240 static void record_range (tree, basic_block, varray_type *);
241 static bool extract_range_from_cond (tree, tree *, tree *, int *);
242 static void record_equivalences_from_phis (struct dom_walk_data *, basic_block);
243 static void record_equivalences_from_incoming_edge (struct dom_walk_data *,
244                                                     basic_block);
245 static bool eliminate_redundant_computations (struct dom_walk_data *,
246                                               tree, stmt_ann_t);
247 static void record_equivalences_from_stmt (tree, varray_type *, varray_type *,
248                                            int, stmt_ann_t);
249 static void thread_across_edge (struct dom_walk_data *, edge);
250 static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
251 static void dom_opt_initialize_block_local_data (struct dom_walk_data *,
252                                                  basic_block, bool);
253 static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
254 static void cprop_into_phis (struct dom_walk_data *, basic_block);
255 static void remove_local_expressions_from_table (varray_type locals,
256                                                  unsigned limit,
257                                                  htab_t table);
258 static void restore_vars_to_original_value (varray_type locals,
259                                             unsigned limit, 
260                                             varray_type table);
261 static void restore_currdefs_to_original_value (varray_type locals,
262                                                 unsigned limit);
263 static void register_definitions_for_stmt (stmt_ann_t, varray_type *);
264 static void redirect_edges_and_update_ssa_graph (varray_type);
265
266 /* Local version of fold that doesn't introduce cruft.  */
267
268 static tree
269 local_fold (tree t)
270 {
271   t = fold (t);
272
273   /* Strip away useless type conversions.  Both the NON_LVALUE_EXPR that
274      may have been added by fold, and "useless" type conversions that might
275      now be apparent due to propagation.  */
276   STRIP_MAIN_TYPE_NOPS (t);
277   STRIP_USELESS_TYPE_CONVERSION (t);
278
279   return t;
280 }
281
282 /* Return the value associated with variable VAR in TABLE.  */
283
284 static inline tree
285 get_value_for (tree var, varray_type table)
286 {
287   return VARRAY_TREE (table, SSA_NAME_VERSION (var));
288 }
289
290 /* Associate VALUE to variable VAR in TABLE.  */
291
292 static inline void
293 set_value_for (tree var, tree value, varray_type table)
294 {
295   VARRAY_TREE (table, SSA_NAME_VERSION (var)) = value;
296 }
297
298 /* REDIRECTION_EDGES contains edge pairs where we want to revector the
299    destination of the first edge to the destination of the second edge.
300
301    These redirections may significantly change the SSA graph since we
302    allow redirection through blocks with PHI nodes and blocks with
303    real instructions in some cases.
304
305    This routine will perform the requested redirections and incrementally
306    update the SSA graph. 
307
308    Note in some cases requested redirections may be ignored as they can
309    not be safely implemented.  */
310
311 static void
312 redirect_edges_and_update_ssa_graph (varray_type redirection_edges)
313 {
314   basic_block tgt, bb;
315   tree phi;
316   unsigned int i;
317   size_t old_num_referenced_vars = num_referenced_vars;
318   bitmap virtuals_to_rename = BITMAP_XMALLOC ();
319
320   /* First note any variables which we are going to have to take
321      out of SSA form as well as any virtuals which need updating.  */
322   for (i = 0; i < VARRAY_ACTIVE_SIZE (redirection_edges); i += 2)
323     {
324       block_stmt_iterator bsi;
325       edge e;
326       basic_block tgt;
327       tree phi;
328
329       e = VARRAY_EDGE (redirection_edges, i);
330       tgt = VARRAY_EDGE (redirection_edges, i + 1)->dest;
331
332       /* All variables referenced in PHI nodes we bypass must be
333          renamed.  */
334       for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
335         {
336           tree result = SSA_NAME_VAR (PHI_RESULT (phi));
337
338           if (is_gimple_reg (PHI_RESULT (phi)))
339             bitmap_set_bit (vars_to_rename, var_ann (result)->uid);
340           else
341             bitmap_set_bit (virtuals_to_rename, var_ann (result)->uid);
342         }
343
344       /* Any variables set by statements at the start of the block we
345          are bypassing must also be taken our of SSA form.  */
346       for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
347         {
348           unsigned int j;
349           def_optype defs;
350           v_may_def_optype v_may_defs;
351           v_must_def_optype v_must_defs;
352           tree stmt = bsi_stmt (bsi);
353           stmt_ann_t ann = stmt_ann (stmt);
354
355           if (TREE_CODE (stmt) == COND_EXPR)
356             break;
357
358           get_stmt_operands (stmt);
359
360           defs = DEF_OPS (ann);
361           for (j = 0; j < NUM_DEFS (defs); j++)
362             {
363               tree op = SSA_NAME_VAR (DEF_OP (defs, j));
364               bitmap_set_bit (vars_to_rename, var_ann (op)->uid);
365             }
366
367           v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
368           for (j = 0; j < NUM_V_MAY_DEFS (v_may_defs); j++)
369             {
370               tree op = V_MAY_DEF_RESULT (v_may_defs, j);
371               bitmap_set_bit (vars_to_rename, var_ann (op)->uid);
372             }
373             
374           v_must_defs = STMT_V_MUST_DEF_OPS (stmt);
375           for (j = 0; j < NUM_V_MUST_DEFS (v_must_defs); j++)
376             {
377               tree op = V_MUST_DEF_OP (v_must_defs, j);
378               bitmap_set_bit (vars_to_rename, var_ann (op)->uid);
379             }
380         }
381
382       /* Finally, any variables in PHI nodes at our final destination
383          must also be taken our of SSA form.  */
384       for (phi = phi_nodes (tgt); phi; phi = TREE_CHAIN (phi))
385         {
386           tree result = SSA_NAME_VAR (PHI_RESULT (phi));
387
388           if (is_gimple_reg (PHI_RESULT (phi)))
389             bitmap_set_bit (vars_to_rename, var_ann (result)->uid);
390           else
391             bitmap_set_bit (virtuals_to_rename, var_ann (result)->uid);
392         }
393     }
394
395   /* Take those selected variables out of SSA form.  This must be
396      done before we start redirecting edges.  */
397   if (bitmap_first_set_bit (vars_to_rename) >= 0)
398     rewrite_vars_out_of_ssa (vars_to_rename);
399
400   /* The out of SSA translation above may split the edge from
401      E->src to E->dest.  This could potentially cause us to lose
402      an assignment leading to invalid warnings about uninitialized
403      variables or incorrect code.
404
405      Luckily, we can detect this by looking at the last statement
406      in E->dest.  If it is not a COND_EXPR or SWITCH_EXPR, then
407      the edge was split and instead of E, we want E->dest->succ.  */
408   for (i = 0; i < VARRAY_ACTIVE_SIZE (redirection_edges); i += 2)
409     {
410       edge e = VARRAY_EDGE (redirection_edges, i);
411       tree last = last_stmt (e->dest);
412
413       if (last
414           && TREE_CODE (last) != COND_EXPR
415           && TREE_CODE (last) != SWITCH_EXPR)
416         {
417           e = e->dest->succ;
418
419 #ifdef ENABLE_CHECKING
420           /* There should only be a single successor if the
421              original edge was split.  */
422           if (e->succ_next)
423             abort ();
424 #endif
425           /* Replace the edge in REDIRECTION_EDGES for the
426              loop below.  */
427           VARRAY_EDGE (redirection_edges, i) = e;
428         }
429     }
430
431   /* If we created any new variables as part of the out-of-ssa
432      translation, then any jump threads must be invalidated if they
433      bypass a block in which we skipped instructions.
434
435      This is necessary as instructions which appeared to be NOPS
436      may be necessary after the out-of-ssa translation.  */
437   if (num_referenced_vars != old_num_referenced_vars)
438     {
439       for (i = 0; i < VARRAY_ACTIVE_SIZE (redirection_edges); i += 2)
440         {
441           block_stmt_iterator bsi;
442           edge e;
443
444           e = VARRAY_EDGE (redirection_edges, i);
445           for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
446             {
447               tree stmt = bsi_stmt (bsi);
448
449               if (IS_EMPTY_STMT (stmt)
450                   || TREE_CODE (stmt) == LABEL_EXPR)
451                 continue;
452
453               if (TREE_CODE (stmt) == COND_EXPR)
454                 break;
455
456               /* Invalidate the jump thread.  */
457               VARRAY_EDGE (redirection_edges, i) = NULL;
458               VARRAY_EDGE (redirection_edges, i + 1) = NULL;
459               break;
460             }
461         }
462     }
463
464   /* Now redirect the edges.  */
465   for (i = 0; i < VARRAY_ACTIVE_SIZE (redirection_edges); i += 2)
466     {
467       basic_block src;
468       edge e;
469
470       e = VARRAY_EDGE (redirection_edges, i);
471       if (!e)
472         continue;
473
474       tgt = VARRAY_EDGE (redirection_edges, i + 1)->dest;
475
476
477       if (dump_file && (dump_flags & TDF_DETAILS))
478         fprintf (dump_file, "  Threaded jump %d --> %d to %d\n",
479                  e->src->index, e->dest->index, tgt->index);
480
481       src = e->src;
482
483       e = redirect_edge_and_branch (e, tgt);
484       PENDING_STMT (e) = NULL_TREE;
485
486       /* Updating the dominance information would be nontrivial.  */
487       free_dominance_info (CDI_DOMINATORS);
488       
489       if ((dump_file && (dump_flags & TDF_DETAILS))
490           && e->src != src)
491         fprintf (dump_file, "    basic block %d created\n",
492                  e->src->index);
493
494       cfg_altered = true;
495     }
496
497   VARRAY_CLEAR (redirection_edges);
498
499   for (i = old_num_referenced_vars; i < num_referenced_vars; i++)
500     {
501       bitmap_set_bit (vars_to_rename, i);
502       var_ann (referenced_var (i))->out_of_ssa_tag = 0;
503     }
504
505   bitmap_a_or_b (vars_to_rename, vars_to_rename, virtuals_to_rename);
506
507   /* We must remove any PHIs for virtual variables that we are going to
508      re-rename.  Hopefully we'll be able to simply update these incrementally
509      soon.  */
510   FOR_EACH_BB (bb)
511     {
512       tree next;
513
514       for (phi = phi_nodes (bb); phi; phi = next)
515         {
516           tree result = PHI_RESULT (phi);
517
518           next = TREE_CHAIN (phi);
519
520           if (bitmap_bit_p (virtuals_to_rename,
521                             var_ann (SSA_NAME_VAR (result))->uid))
522             remove_phi_node (phi, NULL, bb);
523         }
524     }
525   BITMAP_XFREE (virtuals_to_rename);
526 }
527
528 /* Jump threading, redundancy elimination and const/copy propagation. 
529
530    Optimize function FNDECL based on a walk through the dominator tree.
531
532    This pass may expose new symbols that need to be renamed into SSA.  For
533    every new symbol exposed, its corresponding bit will be set in
534    VARS_TO_RENAME.
535
536    PHASE indicates which dump file from the DUMP_FILES array to use when
537    dumping debugging information.  */
538
539 static void
540 tree_ssa_dominator_optimize (void)
541 {
542   basic_block bb;
543   struct dom_walk_data walk_data;
544   unsigned int i;
545
546   for (i = 0; i < num_referenced_vars; i++)
547     var_ann (referenced_var (i))->current_def = NULL;
548
549   /* Mark loop edges so we avoid threading across loop boundaries.
550      This may result in transforming natural loop into irreducible
551      region.  */
552   mark_dfs_back_edges ();
553
554   /* Create our hash tables.  */
555   avail_exprs = htab_create (1024, avail_expr_hash, avail_expr_eq, free);
556   VARRAY_TREE_INIT (const_and_copies, num_ssa_names, "const_and_copies");
557   nonzero_vars = BITMAP_XMALLOC ();
558   VARRAY_EDGE_INIT (redirection_edges, 20, "redirection_edges");
559   VARRAY_GENERIC_PTR_INIT (vrp_data, num_ssa_names, "vrp_data");
560
561   /* Setup callbacks for the generic dominator tree walker.  */
562   walk_data.walk_stmts_backward = false;
563   walk_data.dom_direction = CDI_DOMINATORS;
564   walk_data.initialize_block_local_data = dom_opt_initialize_block_local_data;
565   walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
566   walk_data.before_dom_children_walk_stmts = optimize_stmt;
567   walk_data.before_dom_children_after_stmts = cprop_into_phis;
568   walk_data.after_dom_children_before_stmts = NULL;
569   walk_data.after_dom_children_walk_stmts = NULL;
570   walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
571   /* Right now we only attach a dummy COND_EXPR to the global data pointer.
572      When we attach more stuff we'll need to fill this out with a real
573      structure.  */
574   walk_data.global_data = NULL;
575   walk_data.block_local_data_size = sizeof (struct dom_walk_block_data);
576
577   /* Now initialize the dominator walker.  */
578   init_walk_dominator_tree (&walk_data);
579
580   /* Reset block_forwardable in each block's annotation.  We use that
581      attribute when threading through COND_EXPRs.  */
582   FOR_EACH_BB (bb)
583     bb_ann (bb)->forwardable = 1;
584
585   calculate_dominance_info (CDI_DOMINATORS);
586
587   /* If we prove certain blocks are unreachable, then we want to
588      repeat the dominator optimization process as PHI nodes may
589      have turned into copies which allows better propagation of
590      values.  So we repeat until we do not identify any new unreachable
591      blocks.  */
592   do
593     {
594       /* Optimize the dominator tree.  */
595       cfg_altered = false;
596
597       /* Recursively walk the dominator tree optimizing statements.  */
598       walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
599
600       /* Wipe the hash tables.  */
601
602       if (VARRAY_ACTIVE_SIZE (redirection_edges) > 0)
603         redirect_edges_and_update_ssa_graph (redirection_edges);
604
605       /* We may have made some basic blocks unreachable, remove them.  */
606       cfg_altered |= delete_unreachable_blocks ();
607
608       /* If the CFG was altered, then recompute the dominator tree.  This
609          is not strictly needed if we only removed unreachable blocks, but
610          may produce better results.  If we threaded jumps, then rebuilding
611          the dominator tree is strictly necessary.  */
612       if (cfg_altered)
613         {
614           cleanup_tree_cfg ();
615           calculate_dominance_info (CDI_DOMINATORS);
616         }
617
618       /* If we are going to iterate (CFG_ALTERED is true), then we must
619          perform any queued renaming before the next iteration.  */
620       if (cfg_altered
621           && bitmap_first_set_bit (vars_to_rename) >= 0)
622         {
623           rewrite_into_ssa ();
624           bitmap_clear (vars_to_rename);
625
626           /* The into SSA translation may have created new SSA_NAMES whic
627              affect the size of CONST_AND_COPIES and VRP_DATA.  */
628           VARRAY_GROW (const_and_copies, num_ssa_names);
629           VARRAY_GROW (vrp_data, num_ssa_names);
630         }
631
632       /* Reinitialize the various tables.  */
633       bitmap_clear (nonzero_vars);
634       htab_empty (avail_exprs);
635       VARRAY_CLEAR (const_and_copies);
636       VARRAY_CLEAR (vrp_data);
637
638       for (i = 0; i < num_referenced_vars; i++)
639         var_ann (referenced_var (i))->current_def = NULL;
640     }
641   while (cfg_altered);
642
643   /* Remove any unreachable blocks left behind and linearize the CFG.  */
644   cleanup_tree_cfg ();
645
646   /* Debugging dumps.  */
647   if (dump_file && (dump_flags & TDF_STATS))
648     dump_dominator_optimization_stats (dump_file);
649
650   /* We emptyed the hash table earlier, now delete it completely.  */
651   htab_delete (avail_exprs);
652
653   /* It is not necessary to clear CURRDEFS, REDIRECTION_EDGES, VRP_DATA,
654      CONST_AND_COPIES, and NONZERO_VARS as they all get cleared at the bottom
655      of the do-while loop above.  */
656
657   /* And finalize the dominator walker.  */
658   fini_walk_dominator_tree (&walk_data);
659
660   /* Free nonzero_vars.   */
661   BITMAP_XFREE (nonzero_vars);
662 }
663
664 static bool
665 gate_dominator (void)
666 {
667   return flag_tree_dom != 0;
668 }
669
670 struct tree_opt_pass pass_dominator = 
671 {
672   "dom",                                /* name */
673   gate_dominator,                       /* gate */
674   tree_ssa_dominator_optimize,          /* execute */
675   NULL,                                 /* sub */
676   NULL,                                 /* next */
677   0,                                    /* static_pass_number */
678   TV_TREE_SSA_DOMINATOR_OPTS,           /* tv_id */
679   PROP_cfg | PROP_ssa,                  /* properties_required */
680   0,                                    /* properties_provided */
681   0,                                    /* properties_destroyed */
682   0,                                    /* todo_flags_start */
683   TODO_dump_func | TODO_rename_vars
684     | TODO_verify_ssa                   /* todo_flags_finish */
685 };
686
687
688 /* We are exiting BB, see if the target block begins with a conditional
689    jump which has a known value when reached via BB.  */
690
691 static void
692 thread_across_edge (struct dom_walk_data *walk_data, edge e)
693 {
694   struct dom_walk_block_data *bd
695     = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
696   block_stmt_iterator bsi;
697   tree stmt = NULL;
698   tree phi;
699
700   /* Each PHI creates a temporary equivalence, record them.  */
701   for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
702     {
703       tree src = PHI_ARG_DEF (phi, phi_arg_from_edge (phi, e));
704       tree dst = PHI_RESULT (phi);
705       record_const_or_copy (dst, src, &bd->const_and_copies);
706       register_new_def (dst, &bd->block_defs);
707     }
708
709   for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
710     {
711       tree lhs, cached_lhs;
712
713       stmt = bsi_stmt (bsi);
714
715       /* Ignore empty statements and labels.  */
716       if (IS_EMPTY_STMT (stmt) || TREE_CODE (stmt) == LABEL_EXPR)
717         continue;
718
719       /* If this is not a MODIFY_EXPR which sets an SSA_NAME to a new
720          value, then stop our search here.  Ideally when we stop a
721          search we stop on a COND_EXPR or SWITCH_EXPR.  */
722       if (TREE_CODE (stmt) != MODIFY_EXPR
723           || TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
724         break;
725
726       /* At this point we have a statement which assigns an RHS to an
727          SSA_VAR on the LHS.  We want to prove that the RHS is already
728          available and that its value is held in the current definition
729          of the LHS -- meaning that this assignment is a NOP when
730          reached via edge E.  */
731       if (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME)
732         cached_lhs = TREE_OPERAND (stmt, 1);
733       else
734         cached_lhs = lookup_avail_expr (stmt, NULL, false);
735
736       lhs = TREE_OPERAND (stmt, 0);
737
738       /* This can happen if we thread around to the start of a loop.  */
739       if (lhs == cached_lhs)
740         break;
741
742       /* If we did not find RHS in the hash table, then try again after
743          temporarily const/copy propagating the operands.  */
744       if (!cached_lhs)
745         {
746           /* Copy the operands.  */
747           stmt_ann_t ann = stmt_ann (stmt);
748           use_optype uses = USE_OPS (ann);
749           vuse_optype vuses = VUSE_OPS (ann);
750           tree *uses_copy = xcalloc (NUM_USES (uses),  sizeof (tree));
751           tree *vuses_copy = xcalloc (NUM_VUSES (vuses), sizeof (tree));
752           unsigned int i;
753
754           /* Make a copy of the uses into USES_COPY, then cprop into
755              the use operands.  */
756           for (i = 0; i < NUM_USES (uses); i++)
757             {
758               tree tmp = NULL;
759
760               uses_copy[i] = USE_OP (uses, i);
761               if (TREE_CODE (USE_OP (uses, i)) == SSA_NAME)
762                 tmp = get_value_for (USE_OP (uses, i), const_and_copies);
763               if (tmp)
764                 *USE_OP_PTR (uses, i) = tmp;
765             }
766
767           /* Similarly for virtual uses.  */
768           for (i = 0; i < NUM_VUSES (vuses); i++)
769             {
770               tree tmp = NULL;
771
772               vuses_copy[i] = VUSE_OP (vuses, i);
773               if (TREE_CODE (VUSE_OP (vuses, i)) == SSA_NAME)
774                 tmp = get_value_for (VUSE_OP (vuses, i), const_and_copies);
775               if (tmp)
776                 VUSE_OP (vuses, i) = tmp;
777             }
778
779           /* Try to lookup the new expression.  */
780           cached_lhs = lookup_avail_expr (stmt, NULL, false);
781
782           /* Restore the statement's original uses/defs.  */
783           for (i = 0; i < NUM_USES (uses); i++)
784             *USE_OP_PTR (uses, i) = uses_copy[i];
785
786           for (i = 0; i < NUM_VUSES (vuses); i++)
787             VUSE_OP (vuses, i) = vuses_copy[i];
788
789           free (uses_copy);
790           free (vuses_copy);
791
792           /* If we still did not find the expression in the hash table,
793              then we can not ignore this statement.  */
794           if (! cached_lhs)
795             break;
796         }
797
798       /* If the expression in the hash table was not assigned to an
799          SSA_NAME, then we can not ignore this statement.  */
800       if (TREE_CODE (cached_lhs) != SSA_NAME)
801         break;
802
803       /* If we have different underlying variables, then we can not
804          ignore this statement.  */
805       if (SSA_NAME_VAR (cached_lhs) != SSA_NAME_VAR (lhs))
806         break;
807
808       /* If CACHED_LHS does not represent the current value of the undering
809          variable in CACHED_LHS/LHS, then we can not ignore this statement.  */
810       if (var_ann (SSA_NAME_VAR (lhs))->current_def != cached_lhs)
811         break;
812
813       /* If we got here, then we can ignore this statement and continue
814          walking through the statements in the block looking for a threadable
815          COND_EXPR.
816
817          We want to record an equivalence lhs = cache_lhs so that if
818          the result of this statement is used later we can copy propagate
819          suitably.  */
820       record_const_or_copy (lhs, cached_lhs, &bd->const_and_copies);
821       register_new_def (lhs, &bd->block_defs);
822     }
823
824   /* If we stopped at a COND_EXPR or SWITCH_EXPR, then see if we know which
825      arm will be taken.  */
826   if (stmt
827       && (TREE_CODE (stmt) == COND_EXPR
828           || TREE_CODE (stmt) == SWITCH_EXPR))
829     {
830       tree cond, cached_lhs;
831       edge e1;
832
833       /* Do not forward entry edges into the loop.  In the case loop
834          has multiple entry edges we may end up in constructing irreducible
835          region.  
836          ??? We may consider forwarding the edges in the case all incoming
837          edges forward to the same destination block.  */
838       if (!e->flags & EDGE_DFS_BACK)
839         {
840           for (e1 = e->dest->pred; e; e = e->pred_next)
841             if (e1->flags & EDGE_DFS_BACK)
842               break;
843           if (e1)
844             return;
845         }
846
847       /* Now temporarily cprop the operands and try to find the resulting
848          expression in the hash tables.  */
849       if (TREE_CODE (stmt) == COND_EXPR)
850         cond = COND_EXPR_COND (stmt);
851       else
852         cond = SWITCH_COND (stmt);
853
854       if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<')
855         {
856           tree dummy_cond, op0, op1;
857           enum tree_code cond_code;
858
859           op0 = TREE_OPERAND (cond, 0);
860           op1 = TREE_OPERAND (cond, 1);
861           cond_code = TREE_CODE (cond);
862
863           /* Get the current value of both operands.  */
864           if (TREE_CODE (op0) == SSA_NAME)
865             {
866               tree tmp = get_value_for (op0, const_and_copies);
867               if (tmp)
868                 op0 = tmp;
869             }
870
871           if (TREE_CODE (op1) == SSA_NAME)
872             {
873               tree tmp = get_value_for (op1, const_and_copies);
874               if (tmp)
875                 op1 = tmp;
876             }
877
878           /* Stuff the operator and operands into our dummy conditional
879              expression, creating the dummy conditional if necessary.  */
880           dummy_cond = walk_data->global_data;
881           if (! dummy_cond)
882             {
883               dummy_cond = build (cond_code, boolean_type_node, op0, op1);
884               dummy_cond = build (COND_EXPR, void_type_node,
885                                   dummy_cond, NULL, NULL);
886               walk_data->global_data = dummy_cond;
887             }
888           else
889             {
890               TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), cond_code);
891               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op0;
892               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1) = op1;
893             }
894
895           /* If the conditional folds to an invariant, then we are done,
896              otherwise look it up in the hash tables.  */
897           cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
898           if (! is_gimple_min_invariant (cached_lhs))
899             cached_lhs = lookup_avail_expr (dummy_cond, NULL, false);
900           if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
901             {
902               stmt_ann_t ann = get_stmt_ann (dummy_cond);
903               cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
904                                                                 NULL,
905                                                                 ann,
906                                                                 false);
907             }
908         }
909       /* We can have conditionals which just test the state of a
910          variable rather than use a relational operator.  These are
911          simpler to handle.  */
912       else if (TREE_CODE (cond) == SSA_NAME)
913         {
914           cached_lhs = cond;
915           cached_lhs = get_value_for (cached_lhs, const_and_copies);
916           if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
917             cached_lhs = 0;
918         }
919       else
920         cached_lhs = lookup_avail_expr (stmt, NULL, false);
921
922       if (cached_lhs)
923         {
924           edge taken_edge = find_taken_edge (e->dest, cached_lhs);
925           basic_block dest = (taken_edge ? taken_edge->dest : NULL);
926
927           if (dest == e->src)
928             return;
929
930           /* If we have a known destination for the conditional, then
931              we can perform this optimization, which saves at least one
932              conditional jump each time it applies since we get to
933              bypass the conditional at our original destination. 
934
935              Note that we can either thread through a block with PHIs
936              or to a block with PHIs, but not both.  At this time the
937              bookkeeping to keep the CFG & SSA up-to-date has proven
938              difficult.  */
939           if (dest)
940             {
941               int saved_forwardable = bb_ann (e->src)->forwardable;
942               edge tmp_edge;
943
944               bb_ann (e->src)->forwardable = 0;
945               tmp_edge = tree_block_forwards_to (dest);
946               taken_edge = (tmp_edge ? tmp_edge : taken_edge);
947               bb_ann (e->src)->forwardable = saved_forwardable;
948               VARRAY_PUSH_EDGE (redirection_edges, e);
949               VARRAY_PUSH_EDGE (redirection_edges, taken_edge);
950             }
951         }
952     }
953 }
954
955
956 /* Initialize the local stacks.
957      
958    AVAIL_EXPRS stores all the expressions made available in this block.
959
960    CONST_AND_COPIES stores var/value pairs to restore at the end of this
961    block.
962
963    NONZERO_VARS stores the vars which have a nonzero value made in this
964    block.
965
966    STMTS_TO_RESCAN is a list of statements we will rescan for operands.
967
968    VRP_VARIABLES is the list of variables which have had their values
969    constrained by an operation in this block.
970
971    These stacks are cleared in the finalization routine run for each
972    block.  */
973
974 static void
975 dom_opt_initialize_block_local_data (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
976                                      basic_block bb ATTRIBUTE_UNUSED,
977                                      bool recycled ATTRIBUTE_UNUSED)
978 {
979 #ifdef ENABLE_CHECKING
980   struct dom_walk_block_data *bd
981     = (struct dom_walk_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
982
983   /* We get cleared memory from the allocator, so if the memory is not
984      cleared, then we are re-using a previously allocated entry.  In
985      that case, we can also re-use the underlying virtual arrays.  Just
986      make sure we clear them before using them!  */
987   if (recycled)
988     {
989       if (bd->avail_exprs && VARRAY_ACTIVE_SIZE (bd->avail_exprs) > 0)
990         abort ();
991       if (bd->const_and_copies && VARRAY_ACTIVE_SIZE (bd->const_and_copies) > 0)
992         abort ();
993       if (bd->nonzero_vars && VARRAY_ACTIVE_SIZE (bd->nonzero_vars) > 0)
994         abort ();
995       if (bd->stmts_to_rescan && VARRAY_ACTIVE_SIZE (bd->stmts_to_rescan) > 0)
996         abort ();
997       if (bd->vrp_variables && VARRAY_ACTIVE_SIZE (bd->vrp_variables) > 0)
998         abort ();
999       if (bd->block_defs && VARRAY_ACTIVE_SIZE (bd->block_defs) > 0)
1000         abort ();
1001     }
1002 #endif
1003 }
1004
1005 /* Initialize local stacks for this optimizer and record equivalences
1006    upon entry to BB.  Equivalences can come from the edge traversed to
1007    reach BB or they may come from PHI nodes at the start of BB.  */
1008
1009 static void
1010 dom_opt_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
1011 {
1012   if (dump_file && (dump_flags & TDF_DETAILS))
1013     fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1014
1015   record_equivalences_from_incoming_edge (walk_data, bb);
1016
1017   /* PHI nodes can create equivalences too.  */
1018   record_equivalences_from_phis (walk_data, bb);
1019 }
1020
1021 /* Given an expression EXPR (a relational expression or a statement), 
1022    initialize the hash table element pointed by by ELEMENT.  */
1023
1024 static void
1025 initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
1026 {
1027   /* Hash table elements may be based on conditional expressions or statements.
1028
1029      For the former case, we have no annotation and we want to hash the
1030      conditional expression.  In the latter case we have an annotation and
1031      we want to record the expression the statement evaluates.  */
1032   if (TREE_CODE_CLASS (TREE_CODE (expr)) == '<'
1033       || TREE_CODE (expr) == TRUTH_NOT_EXPR)
1034     {
1035       element->ann = NULL;
1036       element->rhs = expr;
1037     }
1038   else if (TREE_CODE (expr) == COND_EXPR)
1039     {
1040       element->ann = stmt_ann (expr);
1041       element->rhs = COND_EXPR_COND (expr);
1042     }
1043   else if (TREE_CODE (expr) == SWITCH_EXPR)
1044     {
1045       element->ann = stmt_ann (expr);
1046       element->rhs = SWITCH_COND (expr);
1047     }
1048   else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
1049     {
1050       element->ann = stmt_ann (expr);
1051       element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
1052     }
1053   else
1054     {
1055       element->ann = stmt_ann (expr);
1056       element->rhs = TREE_OPERAND (expr, 1);
1057     }
1058
1059   element->lhs = lhs;
1060   element->hash = avail_expr_hash (element);
1061 }
1062
1063 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
1064    LIMIT entries left in LOCALs.  */
1065
1066 static void
1067 remove_local_expressions_from_table (varray_type locals,
1068                                      unsigned limit,
1069                                      htab_t table)
1070 {
1071   if (! locals)
1072     return;
1073
1074   /* Remove all the expressions made available in this block.  */
1075   while (VARRAY_ACTIVE_SIZE (locals) > limit)
1076     {
1077       struct expr_hash_elt element;
1078       tree expr = VARRAY_TOP_TREE (locals);
1079       VARRAY_POP (locals);
1080
1081       initialize_hash_element (expr, NULL, &element);
1082       htab_remove_elt_with_hash (table, &element, element.hash);
1083     }
1084 }
1085
1086 /* Use the SSA_NAMES in LOCALS to restore TABLE to its original
1087    state, stopping when there are LIMIT entries left in LOCALs.  */
1088
1089 static void
1090 restore_nonzero_vars_to_original_value (varray_type locals,
1091                                         unsigned limit,
1092                                         bitmap table)
1093 {
1094   if (!locals)
1095     return;
1096
1097   while (VARRAY_ACTIVE_SIZE (locals) > limit)
1098     {
1099       tree name = VARRAY_TOP_TREE (locals);
1100       VARRAY_POP (locals);
1101       bitmap_clear_bit (table, SSA_NAME_VERSION (name));
1102     }
1103 }
1104
1105 /* Use the source/dest pairs in LOCALS to restore TABLE to its original
1106    state, stopping when there are LIMIT entries left in LOCALs.  */
1107
1108 static void
1109 restore_vars_to_original_value (varray_type locals,
1110                                 unsigned limit,
1111                                 varray_type table)
1112 {
1113   if (! locals)
1114     return;
1115
1116   while (VARRAY_ACTIVE_SIZE (locals) > limit)
1117     {
1118       tree prev_value, dest;
1119
1120       prev_value = VARRAY_TOP_TREE (locals);
1121       VARRAY_POP (locals);
1122       dest = VARRAY_TOP_TREE (locals);
1123       VARRAY_POP (locals);
1124
1125       set_value_for (dest, prev_value, table);
1126     }
1127 }
1128
1129 /* Similar to restore_vars_to_original_value, except that it restores 
1130    CURRDEFS to its original value.  */
1131 static void
1132 restore_currdefs_to_original_value (varray_type locals, unsigned limit)
1133 {
1134   if (!locals)
1135     return;
1136
1137   /* Restore CURRDEFS to its original state.  */
1138   while (VARRAY_ACTIVE_SIZE (locals) > limit)
1139     {
1140       tree tmp = VARRAY_TOP_TREE (locals);
1141       tree saved_def, var;
1142
1143       VARRAY_POP (locals);
1144
1145       /* If we recorded an SSA_NAME, then make the SSA_NAME the current
1146          definition of its underlying variable.  If we recorded anything
1147          else, it must have been an _DECL node and its current reaching
1148          definition must have been NULL.  */
1149       if (TREE_CODE (tmp) == SSA_NAME)
1150         {
1151           saved_def = tmp;
1152           var = SSA_NAME_VAR (saved_def);
1153         }
1154       else
1155         {
1156           saved_def = NULL;
1157           var = tmp;
1158         }
1159                                                                                 
1160       var_ann (var)->current_def = saved_def;
1161     }
1162 }
1163
1164 /* We have finished processing the dominator children of BB, perform
1165    any finalization actions in preparation for leaving this node in
1166    the dominator tree.  */
1167
1168 static void
1169 dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
1170 {
1171   struct dom_walk_block_data *bd
1172     = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
1173   tree last;
1174
1175   /* If we are at a leaf node in the dominator graph, see if we can thread
1176      the edge from BB through its successor.
1177
1178      Do this before we remove entries from our equivalence tables.  */
1179   if (bb->succ
1180       && ! bb->succ->succ_next
1181       && (bb->succ->flags & EDGE_ABNORMAL) == 0
1182       && (get_immediate_dominator (CDI_DOMINATORS, bb->succ->dest) != bb
1183           || phi_nodes (bb->succ->dest)))
1184         
1185     {
1186       thread_across_edge (walk_data, bb->succ);
1187     }
1188   else if ((last = last_stmt (bb))
1189            && TREE_CODE (last) == COND_EXPR
1190            && (TREE_CODE_CLASS (TREE_CODE (COND_EXPR_COND (last))) == '<'
1191                || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
1192            && bb->succ
1193            && (bb->succ->flags & EDGE_ABNORMAL) == 0
1194            && bb->succ->succ_next
1195            && (bb->succ->succ_next->flags & EDGE_ABNORMAL) == 0
1196            && ! bb->succ->succ_next->succ_next)
1197     {
1198       edge true_edge, false_edge;
1199       tree cond, inverted = NULL;
1200       enum tree_code cond_code;
1201
1202       extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1203
1204       cond = COND_EXPR_COND (last);
1205       cond_code = TREE_CODE (cond);
1206
1207       if (TREE_CODE_CLASS (cond_code) == '<')
1208         inverted = invert_truthvalue (cond);
1209
1210       /* If the THEN arm is the end of a dominator tree or has PHI nodes,
1211          then try to thread through its edge.  */
1212       if (get_immediate_dominator (CDI_DOMINATORS, true_edge->dest) != bb
1213           || phi_nodes (true_edge->dest))
1214         {
1215           unsigned avail_expr_limit;
1216           unsigned const_and_copies_limit;
1217           unsigned currdefs_limit;
1218
1219           avail_expr_limit
1220             = bd->avail_exprs ? VARRAY_ACTIVE_SIZE (bd->avail_exprs) : 0;
1221           const_and_copies_limit
1222             = bd->const_and_copies ? VARRAY_ACTIVE_SIZE (bd->const_and_copies)
1223                                    : 0;
1224           currdefs_limit
1225             = bd->block_defs ? VARRAY_ACTIVE_SIZE (bd->block_defs) : 0;
1226
1227           /* Record any equivalences created by following this edge.  */
1228           if (TREE_CODE_CLASS (cond_code) == '<')
1229             {
1230               record_cond (cond, boolean_true_node, &bd->avail_exprs);
1231               record_cond (inverted, boolean_false_node, &bd->avail_exprs);
1232             }
1233           else if (cond_code == SSA_NAME)
1234             record_const_or_copy (cond, boolean_true_node,
1235                                   &bd->const_and_copies);
1236
1237           /* Now thread the edge.  */
1238           thread_across_edge (walk_data, true_edge);
1239
1240           /* And restore the various tables to their state before
1241              we threaded this edge.  */
1242           remove_local_expressions_from_table (bd->avail_exprs,
1243                                                avail_expr_limit,
1244                                                avail_exprs);
1245           restore_vars_to_original_value (bd->const_and_copies,
1246                                           const_and_copies_limit,
1247                                           const_and_copies);
1248           restore_currdefs_to_original_value (bd->block_defs, currdefs_limit);
1249         }
1250
1251       /* Similarly for the ELSE arm.  */
1252       if (get_immediate_dominator (CDI_DOMINATORS, false_edge->dest) != bb
1253           || phi_nodes (false_edge->dest))
1254         {
1255           /* Record any equivalences created by following this edge.  */
1256           if (TREE_CODE_CLASS (cond_code) == '<')
1257             {
1258               record_cond (cond, boolean_false_node, &bd->avail_exprs);
1259               record_cond (inverted, boolean_true_node, &bd->avail_exprs);
1260             }
1261           else if (cond_code == SSA_NAME)
1262             record_const_or_copy (cond, boolean_false_node,
1263                                   &bd->const_and_copies);
1264
1265           thread_across_edge (walk_data, false_edge);
1266
1267           /* No need to remove local expressions from our tables
1268              or restore vars to their original value as that will
1269              be done immediately below.  */
1270         }
1271     }
1272
1273   remove_local_expressions_from_table (bd->avail_exprs, 0, avail_exprs);
1274   restore_nonzero_vars_to_original_value (bd->nonzero_vars, 0, nonzero_vars);
1275   restore_vars_to_original_value (bd->const_and_copies, 0, const_and_copies);
1276   restore_currdefs_to_original_value (bd->block_defs, 0);
1277
1278   /* Remove VRP records associated with this basic block.  They are no
1279      longer valid.
1280
1281      To be efficient, we note which variables have had their values
1282      constrained in this block.  So walk over each variable in the
1283      VRP_VARIABLEs array.  */
1284   while (bd->vrp_variables && VARRAY_ACTIVE_SIZE (bd->vrp_variables) > 0)
1285     {
1286       tree var = VARRAY_TOP_TREE (bd->vrp_variables);
1287
1288       /* Each variable has a stack of value range records.  We want to
1289          invalidate those associated with our basic block.  So we walk
1290          the array backwards popping off records associated with our
1291          block.  Once we hit a record not associated with our block
1292          we are done.  */
1293       varray_type var_vrp_records = VARRAY_GENERIC_PTR (vrp_data,
1294                                                         SSA_NAME_VERSION (var));
1295
1296       while (VARRAY_ACTIVE_SIZE (var_vrp_records) > 0)
1297         {
1298           struct vrp_element *element
1299             = (struct vrp_element *)VARRAY_TOP_GENERIC_PTR (var_vrp_records);
1300
1301           if (element->bb != bb)
1302             break;
1303   
1304           VARRAY_POP (var_vrp_records);
1305         }
1306
1307       VARRAY_POP (bd->vrp_variables);
1308     }
1309
1310   /* Re-scan operands in all statements that may have had new symbols
1311      exposed.  */
1312   while (bd->stmts_to_rescan && VARRAY_ACTIVE_SIZE (bd->stmts_to_rescan) > 0)
1313     {
1314       tree stmt = VARRAY_TOP_TREE (bd->stmts_to_rescan);
1315       VARRAY_POP (bd->stmts_to_rescan);
1316       mark_new_vars_to_rename (stmt, vars_to_rename);
1317     }
1318 }
1319
1320 /* PHI nodes can create equivalences too.
1321
1322    Ignoring any alternatives which are the same as the result, if
1323    all the alternatives are equal, then the PHI node creates an
1324    equivalence.
1325
1326    Additionally, if all the PHI alternatives are known to have a nonzero
1327    value, then the result of this PHI is known to have a nonzero value,
1328    even if we do not know its exact value.  */
1329
1330 static void
1331 record_equivalences_from_phis (struct dom_walk_data *walk_data, basic_block bb)
1332 {
1333   struct dom_walk_block_data *bd
1334     = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
1335   tree phi;
1336
1337   for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
1338     {
1339       tree lhs = PHI_RESULT (phi);
1340       tree rhs = NULL;
1341       int i;
1342
1343       for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1344         {
1345           tree t = PHI_ARG_DEF (phi, i);
1346
1347           if (TREE_CODE (t) == SSA_NAME || is_gimple_min_invariant (t))
1348             {
1349               /* Ignore alternatives which are the same as our LHS.  */
1350               if (operand_equal_p (lhs, t, 0))
1351                 continue;
1352
1353               /* If we have not processed an alternative yet, then set
1354                  RHS to this alternative.  */
1355               if (rhs == NULL)
1356                 rhs = t;
1357               /* If we have processed an alternative (stored in RHS), then
1358                  see if it is equal to this one.  If it isn't, then stop
1359                  the search.  */
1360               else if (! operand_equal_p (rhs, t, 0))
1361                 break;
1362             }
1363           else
1364             break;
1365         }
1366
1367       /* If we had no interesting alternatives, then all the RHS alternatives
1368          must have been the same as LHS.  */
1369       if (!rhs)
1370         rhs = lhs;
1371
1372       /* If we managed to iterate through each PHI alternative without
1373          breaking out of the loop, then we have a PHI which may create
1374          a useful equivalence.  We do not need to record unwind data for
1375          this, since this is a true assignment and not an equivalence
1376          inferred from a comparison.  All uses of this ssa name are dominated
1377          by this assignment, so unwinding just costs time and space.  */
1378       if (i == PHI_NUM_ARGS (phi)
1379           && may_propagate_copy (lhs, rhs))
1380         set_value_for (lhs, rhs, const_and_copies);
1381
1382       /* Now see if we know anything about the nonzero property for the
1383          result of this PHI.  */
1384       for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1385         {
1386           if (!PHI_ARG_NONZERO (phi, i))
1387             break;
1388         }
1389
1390       if (i == PHI_NUM_ARGS (phi))
1391         bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
1392
1393       register_new_def (lhs, &bd->block_defs);
1394     }
1395 }
1396
1397 /* Record any equivalences created by the incoming edge to BB.  If BB
1398    has more than one incoming edge, then no equivalence is created.  */
1399
1400 static void
1401 record_equivalences_from_incoming_edge (struct dom_walk_data *walk_data,
1402                                         basic_block bb)
1403 {
1404   int edge_flags;
1405   basic_block parent;
1406   struct eq_expr_value eq_expr_value;
1407   tree parent_block_last_stmt = NULL;
1408   struct dom_walk_block_data *bd
1409     = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
1410
1411   /* If our parent block ended with a control statment, then we may be
1412      able to record some equivalences based on which outgoing edge from
1413      the parent was followed.  */
1414   parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1415   if (parent)
1416     {
1417       parent_block_last_stmt = last_stmt (parent);
1418       if (parent_block_last_stmt && !is_ctrl_stmt (parent_block_last_stmt))
1419         parent_block_last_stmt = NULL;
1420     }
1421
1422   eq_expr_value.src = NULL;
1423   eq_expr_value.dst = NULL;
1424
1425   /* If we have a single predecessor, then extract EDGE_FLAGS from
1426      our single incoming edge.  Otherwise clear EDGE_FLAGS and
1427      PARENT_BLOCK_LAST_STMT since they're not needed.  */
1428   if (bb->pred
1429       && ! bb->pred->pred_next
1430       && parent_block_last_stmt
1431       && bb_for_stmt (parent_block_last_stmt) == bb->pred->src)
1432     {
1433       edge_flags = bb->pred->flags;
1434     }
1435   else
1436     {
1437       edge_flags = 0;
1438       parent_block_last_stmt = NULL;
1439     }
1440
1441   /* If our parent block ended in a COND_EXPR, add any equivalences
1442      created by the COND_EXPR to the hash table and initialize
1443      EQ_EXPR_VALUE appropriately.
1444
1445      EQ_EXPR_VALUE is an assignment expression created when BB's immediate
1446      dominator ends in a COND_EXPR statement whose predicate is of the form
1447      'VAR == VALUE', where VALUE may be another variable or a constant.
1448      This is used to propagate VALUE on the THEN_CLAUSE of that
1449      conditional. This assignment is inserted in CONST_AND_COPIES so that
1450      the copy and constant propagator can find more propagation
1451      opportunities.  */
1452   if (parent_block_last_stmt
1453       && bb->pred->pred_next == NULL
1454       && TREE_CODE (parent_block_last_stmt) == COND_EXPR
1455       && (edge_flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1456     eq_expr_value = get_eq_expr_value (parent_block_last_stmt,
1457                                        (edge_flags & EDGE_TRUE_VALUE) != 0,
1458                                        &bd->avail_exprs,
1459                                        bb,
1460                                        &bd->vrp_variables);
1461   /* Similarly when the parent block ended in a SWITCH_EXPR.
1462      We can only know the value of the switch's condition if the dominator
1463      parent is also the only predecessor of this block.  */
1464   else if (parent_block_last_stmt
1465            && bb->pred->pred_next == NULL
1466            && bb->pred->src == parent
1467            && TREE_CODE (parent_block_last_stmt) == SWITCH_EXPR)
1468     {
1469       tree switch_cond = SWITCH_COND (parent_block_last_stmt);
1470
1471       /* If the switch's condition is an SSA variable, then we may
1472          know its value at each of the case labels.  */
1473       if (TREE_CODE (switch_cond) == SSA_NAME)
1474         {
1475           tree switch_vec = SWITCH_LABELS (parent_block_last_stmt);
1476           size_t i, n = TREE_VEC_LENGTH (switch_vec);
1477           int case_count = 0;
1478           tree match_case = NULL_TREE;
1479
1480           /* Search the case labels for those whose destination is
1481              the current basic block.  */
1482           for (i = 0; i < n; ++i)
1483             {
1484               tree elt = TREE_VEC_ELT (switch_vec, i);
1485               if (label_to_block (CASE_LABEL (elt)) == bb)
1486                 {
1487                   if (++case_count > 1 || CASE_HIGH (elt))
1488                     break;
1489                   match_case = elt;
1490                 }
1491             }
1492
1493           /* If we encountered precisely one CASE_LABEL_EXPR and it
1494              was not the default case, or a case range, then we know
1495              the exact value of SWITCH_COND which caused us to get to
1496              this block.  Record that equivalence in EQ_EXPR_VALUE.  */
1497           if (case_count == 1
1498               && match_case
1499               && CASE_LOW (match_case)
1500               && !CASE_HIGH (match_case))
1501             {
1502               eq_expr_value.dst = switch_cond;
1503               eq_expr_value.src = CASE_LOW (match_case);
1504             }
1505         }
1506     }
1507
1508   /* If EQ_EXPR_VALUE (VAR == VALUE) is given, register the VALUE as a
1509      new value for VAR, so that occurrences of VAR can be replaced with
1510      VALUE while re-writing the THEN arm of a COND_EXPR.  */
1511   if (eq_expr_value.src && eq_expr_value.dst)
1512     record_equality (eq_expr_value.dst, eq_expr_value.src,
1513                      &bd->const_and_copies);
1514 }
1515
1516 /* Dump SSA statistics on FILE.  */
1517
1518 void
1519 dump_dominator_optimization_stats (FILE *file)
1520 {
1521   long n_exprs;
1522
1523   fprintf (file, "Total number of statements:                   %6ld\n\n",
1524            opt_stats.num_stmts);
1525   fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1526            opt_stats.num_exprs_considered);
1527
1528   n_exprs = opt_stats.num_exprs_considered;
1529   if (n_exprs == 0)
1530     n_exprs = 1;
1531
1532   fprintf (file, "    Redundant expressions eliminated:         %6ld (%.0f%%)\n",
1533            opt_stats.num_re, PERCENT (opt_stats.num_re,
1534                                       n_exprs));
1535
1536   fprintf (file, "\nHash table statistics:\n");
1537
1538   fprintf (file, "    avail_exprs: ");
1539   htab_statistics (file, avail_exprs);
1540 }
1541
1542
1543 /* Dump SSA statistics on stderr.  */
1544
1545 void
1546 debug_dominator_optimization_stats (void)
1547 {
1548   dump_dominator_optimization_stats (stderr);
1549 }
1550
1551
1552 /* Dump statistics for the hash table HTAB.  */
1553
1554 static void
1555 htab_statistics (FILE *file, htab_t htab)
1556 {
1557   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1558            (long) htab_size (htab),
1559            (long) htab_elements (htab),
1560            htab_collisions (htab));
1561 }
1562
1563 /* Record the fact that VAR has a nonzero value, though we may not know
1564    its exact value.  Note that if VAR is already known to have a nonzero
1565    value, then we do nothing.  */
1566
1567 static void
1568 record_var_is_nonzero (tree var, varray_type *block_nonzero_vars_p)
1569 {
1570   int indx = SSA_NAME_VERSION (var);
1571
1572   if (bitmap_bit_p (nonzero_vars, indx))
1573     return;
1574
1575   /* Mark it in the global table.  */
1576   bitmap_set_bit (nonzero_vars, indx);
1577
1578   /* Record this SSA_NAME so that we can reset the global table
1579      when we leave this block.  */
1580   if (! *block_nonzero_vars_p)
1581     VARRAY_TREE_INIT (*block_nonzero_vars_p, 2, "block_nonzero_vars");
1582   VARRAY_PUSH_TREE (*block_nonzero_vars_p, var);
1583 }
1584
1585 /* Enter a statement into the true/false expression hash table indicating
1586    that the condition COND has the value VALUE.  */
1587
1588 static void
1589 record_cond (tree cond, tree value, varray_type *block_avail_exprs_p)
1590 {
1591   struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
1592   void **slot;
1593
1594   initialize_hash_element (cond, value, element);
1595
1596   slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1597                                    element->hash, true);
1598   if (*slot == NULL)
1599     {
1600       *slot = (void *) element;
1601       if (! *block_avail_exprs_p)
1602         VARRAY_TREE_INIT (*block_avail_exprs_p, 20, "block_avail_exprs");
1603       VARRAY_PUSH_TREE (*block_avail_exprs_p, cond);
1604     }
1605   else
1606     free (element);
1607 }
1608
1609 /* A helper function for record_const_or_copy and record_equality.
1610    Do the work of recording the value and undo info.  */
1611
1612 static void
1613 record_const_or_copy_1 (tree x, tree y, tree prev_x,
1614                         varray_type *block_const_and_copies_p)
1615 {
1616   set_value_for (x, y, const_and_copies);
1617
1618   if (!*block_const_and_copies_p)
1619     VARRAY_TREE_INIT (*block_const_and_copies_p, 2, "block_const_and_copies");
1620   VARRAY_PUSH_TREE (*block_const_and_copies_p, x);
1621   VARRAY_PUSH_TREE (*block_const_and_copies_p, prev_x);
1622 }
1623
1624 /* Record that X is equal to Y in const_and_copies.  Record undo
1625    information in the block-local varray.  */
1626
1627 static void
1628 record_const_or_copy (tree x, tree y, varray_type *block_const_and_copies_p)
1629 {
1630   tree prev_x = get_value_for (x, const_and_copies);
1631
1632   if (TREE_CODE (y) == SSA_NAME)
1633     {
1634       tree tmp = get_value_for (y, const_and_copies);
1635       if (tmp)
1636         y = tmp;
1637     }
1638
1639   record_const_or_copy_1 (x, y, prev_x, block_const_and_copies_p);
1640 }
1641
1642 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1643    This constrains the cases in which we may treat this as assignment.  */
1644
1645 static void
1646 record_equality (tree x, tree y, varray_type *block_const_and_copies_p)
1647 {
1648   tree prev_x = NULL, prev_y = NULL;
1649
1650   if (TREE_CODE (x) == SSA_NAME)
1651     prev_x = get_value_for (x, const_and_copies);
1652   if (TREE_CODE (y) == SSA_NAME)
1653     prev_y = get_value_for (y, const_and_copies);
1654
1655   /* If one of the previous values is invariant, then use that.
1656      Otherwise it doesn't matter which value we choose, just so
1657      long as we canonicalize on one value.  */
1658   if (TREE_INVARIANT (y))
1659     ;
1660   else if (TREE_INVARIANT (x))
1661     prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1662   else if (prev_x && TREE_INVARIANT (prev_x))
1663     x = y, y = prev_x, prev_x = prev_y;
1664   else if (prev_y)
1665     y = prev_y;
1666
1667   /* After the swapping, we must have one SSA_NAME.  */
1668   if (TREE_CODE (x) != SSA_NAME)
1669     return;
1670
1671   /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1672      variable compared against zero.  If we're honoring signed zeros,
1673      then we cannot record this value unless we know that the value is
1674      nonzero.  */
1675   if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1676       && (TREE_CODE (y) != REAL_CST
1677           || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1678     return;
1679
1680   record_const_or_copy_1 (x, y, prev_x, block_const_and_copies_p);
1681 }
1682
1683 /* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
1684    hash tables.  Try to simplify the RHS using whatever equivalences
1685    we may have recorded.
1686
1687    If we are able to simplify the RHS, then lookup the simplified form in
1688    the hash table and return the result.  Otherwise return NULL.  */
1689
1690 static tree
1691 simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *walk_data,
1692                                     tree stmt,
1693                                     stmt_ann_t ann,
1694                                     int insert)
1695 {
1696   tree rhs = TREE_OPERAND (stmt, 1);
1697   enum tree_code rhs_code = TREE_CODE (rhs);
1698   tree result = NULL;
1699   struct dom_walk_block_data *bd
1700     = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
1701
1702   /* If we have lhs = ~x, look and see if we earlier had x = ~y.
1703      In which case we can change this statement to be lhs = y.
1704      Which can then be copy propagated. 
1705
1706      Similarly for negation.  */
1707   if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
1708       && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1709     {
1710       /* Get the definition statement for our RHS.  */
1711       tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1712
1713       /* See if the RHS_DEF_STMT has the same form as our statement.  */
1714       if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
1715           && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
1716         {
1717           tree rhs_def_operand;
1718
1719           rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
1720
1721           /* Verify that RHS_DEF_OPERAND is a suitable SSA variable.  */
1722           if (TREE_CODE (rhs_def_operand) == SSA_NAME
1723               && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1724             result = update_rhs_and_lookup_avail_expr (stmt,
1725                                                        rhs_def_operand,
1726                                                        &bd->avail_exprs,
1727                                                        ann,
1728                                                        insert);
1729         }
1730     }
1731
1732   /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
1733      If OP is associative, create and fold (y OP C2) OP C1 which
1734      should result in (y OP C3), use that as the RHS for the
1735      assignment.  Add minus to this, as we handle it specially below.  */
1736   if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
1737       && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
1738       && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
1739     {
1740       tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1741
1742       /* See if the RHS_DEF_STMT has the same form as our statement.  */
1743       if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
1744         {
1745           tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
1746           enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
1747
1748           if (rhs_code == rhs_def_code
1749               || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
1750               || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
1751             {
1752               tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
1753               tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
1754
1755               if (TREE_CODE (def_stmt_op0) == SSA_NAME
1756                   && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
1757                   && is_gimple_min_invariant (def_stmt_op1))
1758                 {
1759                   tree outer_const = TREE_OPERAND (rhs, 1);
1760                   tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
1761                   tree t;
1762
1763                   /* Ho hum.  So fold will only operate on the outermost
1764                      thingy that we give it, so we have to build the new
1765                      expression in two pieces.  This requires that we handle
1766                      combinations of plus and minus.  */
1767                   if (rhs_def_code != rhs_code)
1768                     {
1769                       if (rhs_def_code == MINUS_EXPR)
1770                         t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
1771                       else
1772                         t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
1773                       rhs_code = PLUS_EXPR;
1774                     }
1775                   else if (rhs_def_code == MINUS_EXPR)
1776                     t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
1777                   else
1778                     t = build (rhs_def_code, type, def_stmt_op1, outer_const);
1779                   t = local_fold (t);
1780                   t = build (rhs_code, type, def_stmt_op0, t);
1781                   t = local_fold (t);
1782
1783                   /* If the result is a suitable looking gimple expression,
1784                      then use it instead of the original for STMT.  */
1785                   if (TREE_CODE (t) == SSA_NAME
1786                       || (TREE_CODE_CLASS (TREE_CODE (t)) == '1'
1787                           && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1788                       || ((TREE_CODE_CLASS (TREE_CODE (t)) == '2'
1789                            || TREE_CODE_CLASS (TREE_CODE (t)) == '<')
1790                           && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
1791                           && is_gimple_val (TREE_OPERAND (t, 1))))
1792                     result = update_rhs_and_lookup_avail_expr
1793                       (stmt, t, &bd->avail_exprs, ann, insert);
1794                 }
1795             }
1796         }
1797     }
1798
1799   /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
1800      and BIT_AND_EXPR respectively if the first operand is greater
1801      than zero and the second operand is an exact power of two.  */
1802   if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
1803       && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
1804       && integer_pow2p (TREE_OPERAND (rhs, 1)))
1805     {
1806       tree val;
1807       tree op = TREE_OPERAND (rhs, 0);
1808
1809       if (TYPE_UNSIGNED (TREE_TYPE (op)))
1810         {
1811           val = integer_one_node;
1812         }
1813       else
1814         {
1815           tree dummy_cond = walk_data->global_data;
1816
1817           if (! dummy_cond)
1818             {
1819               dummy_cond = build (GT_EXPR, boolean_type_node,
1820                                   op, integer_zero_node);
1821               dummy_cond = build (COND_EXPR, void_type_node,
1822                                   dummy_cond, NULL, NULL);
1823               walk_data->global_data = dummy_cond;
1824             }
1825           else
1826             {
1827               TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GT_EXPR);
1828               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1829               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1830                 = integer_zero_node;
1831             }
1832           val = simplify_cond_and_lookup_avail_expr (dummy_cond,
1833                                                      &bd->avail_exprs,
1834                                                      NULL, false);
1835         }
1836
1837       if (val && integer_onep (val))
1838         {
1839           tree t;
1840           tree op0 = TREE_OPERAND (rhs, 0);
1841           tree op1 = TREE_OPERAND (rhs, 1);
1842
1843           if (rhs_code == TRUNC_DIV_EXPR)
1844             t = build (RSHIFT_EXPR, TREE_TYPE (op0), op0,
1845                        build_int_2 (tree_log2 (op1), 0));
1846           else
1847             t = build (BIT_AND_EXPR, TREE_TYPE (op0), op0,
1848                        local_fold (build (MINUS_EXPR, TREE_TYPE (op1),
1849                                           op1, integer_one_node)));
1850
1851           result = update_rhs_and_lookup_avail_expr (stmt, t,
1852                                                      &bd->avail_exprs,
1853                                                      ann, insert);
1854         }
1855     }
1856
1857   /* Transform ABS (X) into X or -X as appropriate.  */
1858   if (rhs_code == ABS_EXPR
1859       && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
1860     {
1861       tree val;
1862       tree op = TREE_OPERAND (rhs, 0);
1863       tree type = TREE_TYPE (op);
1864
1865       if (TYPE_UNSIGNED (type))
1866         {
1867           val = integer_zero_node;
1868         }
1869       else
1870         {
1871           tree dummy_cond = walk_data->global_data;
1872
1873           if (! dummy_cond)
1874             {
1875               dummy_cond = build (LE_EXPR, boolean_type_node,
1876                                   op, integer_zero_node);
1877               dummy_cond = build (COND_EXPR, void_type_node,
1878                                   dummy_cond, NULL, NULL);
1879               walk_data->global_data = dummy_cond;
1880             }
1881           else
1882             {
1883               TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), LE_EXPR);
1884               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1885               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1886                 = fold_convert (type, integer_zero_node);
1887             }
1888           val = simplify_cond_and_lookup_avail_expr (dummy_cond,
1889                                                      &bd->avail_exprs,
1890                                                      NULL, false);
1891
1892           if (!val)
1893             {
1894               TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GE_EXPR);
1895               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1896               TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1897                 = fold_convert (type, integer_zero_node);
1898
1899               val = simplify_cond_and_lookup_avail_expr (dummy_cond,
1900                                                          &bd->avail_exprs,
1901                                                          NULL, false);
1902
1903               if (val)
1904                 {
1905                   if (integer_zerop (val))
1906                     val = integer_one_node;
1907                   else if (integer_onep (val))
1908                     val = integer_zero_node;
1909                 }
1910             }
1911         }
1912
1913       if (val
1914           && (integer_onep (val) || integer_zerop (val)))
1915         {
1916           tree t;
1917
1918           if (integer_onep (val))
1919             t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
1920           else
1921             t = op;
1922
1923           result = update_rhs_and_lookup_avail_expr (stmt, t,
1924                                                      &bd->avail_exprs,
1925                                                      ann, insert);
1926         }
1927     }
1928
1929   /* Optimize *"foo" into 'f'.  This is done here rather than
1930      in fold to avoid problems with stuff like &*"foo".  */
1931   if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
1932     {
1933       tree t = fold_read_from_constant_string (rhs);
1934
1935       if (t)
1936         result = update_rhs_and_lookup_avail_expr (stmt, t,
1937                                                    &bd->avail_exprs,
1938                                                    ann, insert);
1939     }
1940
1941   return result;
1942 }
1943
1944 /* COND is a condition of the form:
1945
1946      x == const or x != const
1947
1948    Look back to x's defining statement and see if x is defined as
1949
1950      x = (type) y;
1951
1952    If const is unchanged if we convert it to type, then we can build
1953    the equivalent expression:
1954
1955
1956       y == const or y != const
1957
1958    Which may allow further optimizations.
1959
1960    Return the equivalent comparison or NULL if no such equivalent comparison
1961    was found.  */
1962
1963 static tree
1964 find_equivalent_equality_comparison (tree cond)
1965 {
1966   tree op0 = TREE_OPERAND (cond, 0);
1967   tree op1 = TREE_OPERAND (cond, 1);
1968   tree def_stmt = SSA_NAME_DEF_STMT (op0);
1969
1970   /* OP0 might have been a parameter, so first make sure it
1971      was defined by a MODIFY_EXPR.  */
1972   if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
1973     {
1974       tree def_rhs = TREE_OPERAND (def_stmt, 1);
1975
1976       /* Now make sure the RHS of the MODIFY_EXPR is a typecast.  */
1977       if ((TREE_CODE (def_rhs) == NOP_EXPR
1978            || TREE_CODE (def_rhs) == CONVERT_EXPR)
1979           && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
1980         {
1981           tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
1982           tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
1983           tree new;
1984
1985           if (TYPE_PRECISION (def_rhs_inner_type)
1986               > TYPE_PRECISION (TREE_TYPE (def_rhs)))
1987             return NULL;
1988
1989           /* What we want to prove is that if we convert OP1 to
1990              the type of the object inside the NOP_EXPR that the
1991              result is still equivalent to SRC. 
1992
1993              If that is true, the build and return new equivalent
1994              condition which uses the source of the typecast and the
1995              new constant (which has only changed its type).  */
1996           new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
1997           new = local_fold (new);
1998           if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
1999             return build (TREE_CODE (cond), TREE_TYPE (cond),
2000                           def_rhs_inner, new);
2001         }
2002     }
2003   return NULL;
2004 }
2005
2006 /* STMT is a COND_EXPR for which we could not trivially determine its
2007    result.  This routine attempts to find equivalent forms of the
2008    condition which we may be able to optimize better.  It also 
2009    uses simple value range propagation to optimize conditionals.  */
2010
2011 static tree
2012 simplify_cond_and_lookup_avail_expr (tree stmt,
2013                                      varray_type *block_avail_exprs_p,
2014                                      stmt_ann_t ann,
2015                                      int insert)
2016 {
2017   tree cond = COND_EXPR_COND (stmt);
2018
2019   if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<')
2020     {
2021       tree op0 = TREE_OPERAND (cond, 0);
2022       tree op1 = TREE_OPERAND (cond, 1);
2023
2024       if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
2025         {
2026           int limit;
2027           tree low, high, cond_low, cond_high;
2028           int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
2029           varray_type vrp_records;
2030           struct vrp_element *element;
2031
2032           /* First see if we have test of an SSA_NAME against a constant
2033              where the SSA_NAME is defined by an earlier typecast which
2034              is irrelevant when performing tests against the given
2035              constant.  */
2036           if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2037             {
2038               tree new_cond = find_equivalent_equality_comparison (cond);
2039
2040               if (new_cond)
2041                 {
2042                   /* Update the statement to use the new equivalent
2043                      condition.  */
2044                   COND_EXPR_COND (stmt) = new_cond;
2045                   ann->modified = 1;
2046
2047                   /* Lookup the condition and return its known value if it
2048                      exists.  */
2049                   new_cond = lookup_avail_expr (stmt, block_avail_exprs_p,
2050                                                 insert);
2051                   if (new_cond)
2052                     return new_cond;
2053
2054                   /* The operands have changed, so update op0 and op1.  */
2055                   op0 = TREE_OPERAND (cond, 0);
2056                   op1 = TREE_OPERAND (cond, 1);
2057                 }
2058             }
2059
2060           /* Consult the value range records for this variable (if they exist)
2061              to see if we can eliminate or simplify this conditional. 
2062
2063              Note two tests are necessary to determine no records exist.
2064              First we have to see if the virtual array exists, if it 
2065              exists, then we have to check its active size. 
2066
2067              Also note the vast majority of conditionals are not testing
2068              a variable which has had its range constrained by an earlier
2069              conditional.  So this filter avoids a lot of unnecessary work.  */
2070           vrp_records = VARRAY_GENERIC_PTR (vrp_data, SSA_NAME_VERSION (op0));
2071           if (vrp_records == NULL)
2072             return NULL;
2073
2074           limit = VARRAY_ACTIVE_SIZE (vrp_records);
2075
2076           /* If we have no value range records for this variable, or we are
2077              unable to extract a range for this condition, then there is
2078              nothing to do.  */
2079           if (limit == 0
2080               || ! extract_range_from_cond (cond, &cond_high,
2081                                             &cond_low, &cond_inverted))
2082             return NULL;
2083
2084           /* We really want to avoid unnecessary computations of range
2085              info.  So all ranges are computed lazily; this avoids a
2086              lot of unnecessary work.  ie, we record the conditional,
2087              but do not process how it constrains the variable's 
2088              potential values until we know that processing the condition
2089              could be helpful.
2090
2091              However, we do not want to have to walk a potentially long
2092              list of ranges, nor do we want to compute a variable's
2093              range more than once for a given path.
2094
2095              Luckily, each time we encounter a conditional that can not
2096              be otherwise optimized we will end up here and we will
2097              compute the necessary range information for the variable
2098              used in this condition.
2099
2100              Thus you can conclude that there will never be more than one
2101              conditional associated with a variable which has not been
2102              processed.  So we never need to merge more than one new
2103              conditional into the current range. 
2104
2105              These properties also help us avoid unnecessary work.  */
2106            element
2107              = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records, limit - 1);
2108
2109           if (element->high && element->low)
2110             {
2111               /* The last element has been processed, so there is no range
2112                  merging to do, we can simply use the high/low values
2113                  recorded in the last element.  */
2114               low = element->low;
2115               high = element->high;
2116             }
2117           else
2118             {
2119               tree tmp_high, tmp_low;
2120               int dummy;
2121
2122               /* The last element has not been processed.  Process it now.  */
2123               extract_range_from_cond (element->cond, &tmp_high,
2124                                        &tmp_low, &dummy);
2125           
2126               /* If this is the only element, then no merging is necessary, 
2127                  the high/low values from extract_range_from_cond are all
2128                  we need.  */
2129               if (limit == 1)
2130                 {
2131                   low = tmp_low;
2132                   high = tmp_high;
2133                 }
2134               else
2135                 {
2136                   /* Get the high/low value from the previous element.  */
2137                   struct vrp_element *prev
2138                     = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records,
2139                                                                 limit - 2);
2140                   low = prev->low;
2141                   high = prev->high;
2142
2143                   /* Merge in this element's range with the range from the
2144                      previous element.
2145
2146                      The low value for the merged range is the maximum of
2147                      the previous low value and the low value of this record.
2148
2149                      Similarly the high value for the merged range is the
2150                      minimum of the previous high value and the high value of
2151                      this record.  */
2152                   low = (tree_int_cst_compare (low, tmp_low) == 1
2153                          ? low : tmp_low);
2154                   high = (tree_int_cst_compare (high, tmp_high) == -1
2155                           ? high : tmp_high);
2156                 }
2157
2158               /* And record the computed range.  */
2159               element->low = low;
2160               element->high = high;
2161
2162             }
2163
2164           /* After we have constrained this variable's potential values,
2165              we try to determine the result of the given conditional.
2166
2167              To simplify later tests, first determine if the current
2168              low value is the same low value as the conditional.
2169              Similarly for the current high value and the high value
2170              for the conditional.  */
2171           lowequal = tree_int_cst_equal (low, cond_low);
2172           highequal = tree_int_cst_equal (high, cond_high);
2173
2174           if (lowequal && highequal)
2175             return (cond_inverted ? boolean_false_node : boolean_true_node);
2176
2177           /* To simplify the overlap/subset tests below we may want
2178              to swap the two ranges so that the larger of the two
2179              ranges occurs "first".  */
2180           swapped = 0;
2181           if (tree_int_cst_compare (low, cond_low) == 1
2182               || (lowequal 
2183                   && tree_int_cst_compare (cond_high, high) == 1))
2184             {
2185               tree temp;
2186
2187               swapped = 1;
2188               temp = low;
2189               low = cond_low;
2190               cond_low = temp;
2191               temp = high;
2192               high = cond_high;
2193               cond_high = temp;
2194             }
2195
2196           /* Now determine if there is no overlap in the ranges
2197              or if the second range is a subset of the first range.  */
2198           no_overlap = tree_int_cst_lt (high, cond_low);
2199           subset = tree_int_cst_compare (cond_high, high) != 1;
2200
2201           /* If there was no overlap in the ranges, then this conditional
2202              always has a false value (unless we had to invert this
2203              conditional, in which case it always has a true value).  */
2204           if (no_overlap)
2205             return (cond_inverted ? boolean_true_node : boolean_false_node);
2206
2207           /* If the current range is a subset of the condition's range,
2208              then this conditional always has a true value (unless we
2209              had to invert this conditional, in which case it always
2210              has a true value).  */
2211           if (subset && swapped)
2212             return (cond_inverted ? boolean_false_node : boolean_true_node);
2213
2214           /* We were unable to determine the result of the conditional.
2215              However, we may be able to simplify the conditional.  First
2216              merge the ranges in the same manner as range merging above.  */
2217           low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
2218           high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
2219           
2220           /* If the range has converged to a single point, then turn this
2221              into an equality comparison.  */
2222           if (TREE_CODE (cond) != EQ_EXPR
2223               && TREE_CODE (cond) != NE_EXPR
2224               && tree_int_cst_equal (low, high))
2225             {
2226               TREE_SET_CODE (cond, EQ_EXPR);
2227               TREE_OPERAND (cond, 1) = high;
2228             }
2229         }
2230     }
2231   return 0;
2232 }
2233
2234 /* STMT is a SWITCH_EXPR for which we could not trivially determine its
2235    result.  This routine attempts to find equivalent forms of the
2236    condition which we may be able to optimize better.  */
2237
2238 static tree
2239 simplify_switch_and_lookup_avail_expr (tree stmt,
2240                                        varray_type *block_avail_exprs_p,
2241                                        stmt_ann_t ann,
2242                                        int insert)
2243 {
2244   tree cond = SWITCH_COND (stmt);
2245   tree def, to, ti;
2246
2247   /* The optimization that we really care about is removing unnecessary
2248      casts.  That will let us do much better in propagating the inferred
2249      constant at the switch target.  */
2250   if (TREE_CODE (cond) == SSA_NAME)
2251     {
2252       def = SSA_NAME_DEF_STMT (cond);
2253       if (TREE_CODE (def) == MODIFY_EXPR)
2254         {
2255           def = TREE_OPERAND (def, 1);
2256           if (TREE_CODE (def) == NOP_EXPR)
2257             {
2258               def = TREE_OPERAND (def, 0);
2259               to = TREE_TYPE (cond);
2260               ti = TREE_TYPE (def);
2261
2262               /* If we have an extension that preserves sign, then we
2263                  can copy the source value into the switch.  */
2264               if (TYPE_UNSIGNED (to) == TYPE_UNSIGNED (ti)
2265                   && TYPE_PRECISION (to) >= TYPE_PRECISION (ti)
2266                   && is_gimple_val (def))
2267                 {
2268                   SWITCH_COND (stmt) = def;
2269                   ann->modified = 1;
2270
2271                   return lookup_avail_expr (stmt, block_avail_exprs_p, insert);
2272                 }
2273             }
2274         }
2275     }
2276
2277   return 0;
2278 }
2279
2280 /* Propagate known constants/copies into PHI nodes of BB's successor
2281    blocks.  */
2282
2283 static void
2284 cprop_into_phis (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2285                  basic_block bb)
2286 {
2287   cprop_into_successor_phis (bb, const_and_copies, nonzero_vars);
2288 }
2289
2290 /* Search for redundant computations in STMT.  If any are found, then
2291    replace them with the variable holding the result of the computation.
2292
2293    If safe, record this expression into the available expression hash
2294    table.  */
2295
2296 static bool
2297 eliminate_redundant_computations (struct dom_walk_data *walk_data,
2298                                   tree stmt, stmt_ann_t ann)
2299 {
2300   v_may_def_optype v_may_defs = V_MAY_DEF_OPS (ann);
2301   tree *expr_p, def = NULL_TREE;
2302   bool insert = true;
2303   tree cached_lhs;
2304   bool retval = false;
2305   struct dom_walk_block_data *bd
2306     = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
2307
2308   if (TREE_CODE (stmt) == MODIFY_EXPR)
2309     def = TREE_OPERAND (stmt, 0);
2310
2311   /* Certain expressions on the RHS can be optimized away, but can not
2312      themselves be entered into the hash tables.   */
2313   if (ann->makes_aliased_stores
2314       || ! def
2315       || TREE_CODE (def) != SSA_NAME
2316       || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2317       || NUM_V_MAY_DEFS (v_may_defs) != 0)
2318     insert = false;
2319
2320   /* Check if the expression has been computed before.  */
2321   cached_lhs = lookup_avail_expr (stmt, &bd->avail_exprs, insert);
2322
2323   /* If this is an assignment and the RHS was not in the hash table,
2324      then try to simplify the RHS and lookup the new RHS in the
2325      hash table.  */
2326   if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
2327     cached_lhs = simplify_rhs_and_lookup_avail_expr (walk_data,
2328                                                      stmt,
2329                                                      ann,
2330                                                      insert);
2331   /* Similarly if this is a COND_EXPR and we did not find its
2332      expression in the hash table, simplify the condition and
2333      try again.  */
2334   else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
2335     cached_lhs = simplify_cond_and_lookup_avail_expr (stmt,
2336                                                       &bd->avail_exprs,
2337                                                       ann,
2338                                                       insert);
2339   /* Similarly for a SWITCH_EXPR.  */
2340   else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
2341     cached_lhs = simplify_switch_and_lookup_avail_expr (stmt,
2342                                                         &bd->avail_exprs,
2343                                                         ann,
2344                                                         insert);
2345
2346   opt_stats.num_exprs_considered++;
2347
2348   /* Get a pointer to the expression we are trying to optimize.  */
2349   if (TREE_CODE (stmt) == COND_EXPR)
2350     expr_p = &COND_EXPR_COND (stmt);
2351   else if (TREE_CODE (stmt) == SWITCH_EXPR)
2352     expr_p = &SWITCH_COND (stmt);
2353   else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
2354     expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
2355   else
2356     expr_p = &TREE_OPERAND (stmt, 1);
2357
2358   /* It is safe to ignore types here since we have already done
2359      type checking in the hashing and equality routines.  In fact
2360      type checking here merely gets in the way of constant
2361      propagation.  Also, make sure that it is safe to propagate
2362      CACHED_LHS into *EXPR_P.  */
2363   if (cached_lhs
2364       && (TREE_CODE (cached_lhs) != SSA_NAME
2365           || may_propagate_copy (cached_lhs, *expr_p)))
2366     {
2367       if (dump_file && (dump_flags & TDF_DETAILS))
2368         {
2369           fprintf (dump_file, "  Replaced redundant expr '");
2370           print_generic_expr (dump_file, *expr_p, dump_flags);
2371           fprintf (dump_file, "' with '");
2372           print_generic_expr (dump_file, cached_lhs, dump_flags);
2373            fprintf (dump_file, "'\n");
2374         }
2375
2376       opt_stats.num_re++;
2377
2378 #if defined ENABLE_CHECKING
2379       if (TREE_CODE (cached_lhs) != SSA_NAME
2380           && !is_gimple_min_invariant (cached_lhs))
2381         abort ();
2382 #endif
2383
2384       if (TREE_CODE (cached_lhs) == ADDR_EXPR
2385           || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
2386               && is_gimple_min_invariant (cached_lhs)))
2387         retval = true;
2388
2389       propagate_value (expr_p, cached_lhs);
2390       ann->modified = 1;
2391     }
2392   return retval;
2393 }
2394
2395 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
2396    the available expressions table or the const_and_copies table.
2397    Detect and record those equivalences.  */
2398
2399 static void
2400 record_equivalences_from_stmt (tree stmt,
2401                                varray_type *block_avail_exprs_p,
2402                                varray_type *block_nonzero_vars_p,
2403                                int may_optimize_p,
2404                                stmt_ann_t ann)
2405 {
2406   tree lhs = TREE_OPERAND (stmt, 0);
2407   enum tree_code lhs_code = TREE_CODE (lhs);
2408   int i;
2409
2410   if (lhs_code == SSA_NAME)
2411     {
2412       tree rhs = TREE_OPERAND (stmt, 1);
2413
2414       /* Strip away any useless type conversions.  */
2415       STRIP_USELESS_TYPE_CONVERSION (rhs);
2416
2417       /* If the RHS of the assignment is a constant or another variable that
2418          may be propagated, register it in the CONST_AND_COPIES table.  We
2419          do not need to record unwind data for this, since this is a true
2420          assignment and not an equivalence inferred from a comparison.  All
2421          uses of this ssa name are dominated by this assignment, so unwinding
2422          just costs time and space.  */
2423       if (may_optimize_p
2424           && (TREE_CODE (rhs) == SSA_NAME
2425               || is_gimple_min_invariant (rhs)))
2426         set_value_for (lhs, rhs, const_and_copies);
2427
2428       /* alloca never returns zero and the address of a non-weak symbol
2429          is never zero.  NOP_EXPRs and CONVERT_EXPRs can be completely
2430          stripped as they do not affect this equivalence.  */
2431       while (TREE_CODE (rhs) == NOP_EXPR
2432              || TREE_CODE (rhs) == CONVERT_EXPR)
2433         rhs = TREE_OPERAND (rhs, 0);
2434
2435       if (alloca_call_p (rhs)
2436           || (TREE_CODE (rhs) == ADDR_EXPR
2437               && DECL_P (TREE_OPERAND (rhs, 0))
2438               && ! DECL_WEAK (TREE_OPERAND (rhs, 0))))
2439         record_var_is_nonzero (lhs, block_nonzero_vars_p);
2440
2441       /* IOR of any value with a nonzero value will result in a nonzero
2442          value.  Even if we do not know the exact result recording that
2443          the result is nonzero is worth the effort.  */
2444       if (TREE_CODE (rhs) == BIT_IOR_EXPR
2445           && integer_nonzerop (TREE_OPERAND (rhs, 1)))
2446         record_var_is_nonzero (lhs, block_nonzero_vars_p);
2447     }
2448
2449   /* Look at both sides for pointer dereferences.  If we find one, then
2450      the pointer must be nonnull and we can enter that equivalence into
2451      the hash tables.  */
2452   if (flag_delete_null_pointer_checks)
2453     for (i = 0; i < 2; i++)
2454       {
2455         tree t = TREE_OPERAND (stmt, i);
2456
2457         /* Strip away any COMPONENT_REFs.  */
2458         while (TREE_CODE (t) == COMPONENT_REF)
2459           t = TREE_OPERAND (t, 0);
2460
2461         /* Now see if this is a pointer dereference.  */
2462         if (TREE_CODE (t) == INDIRECT_REF)
2463           {
2464             tree op = TREE_OPERAND (t, 0);
2465
2466             /* If the pointer is a SSA variable, then enter new
2467                equivalences into the hash table.  */
2468             while (TREE_CODE (op) == SSA_NAME)
2469               {
2470                 tree def = SSA_NAME_DEF_STMT (op);
2471
2472                 record_var_is_nonzero (op, block_nonzero_vars_p);
2473
2474                 /* And walk up the USE-DEF chains noting other SSA_NAMEs
2475                    which are known to have a nonzero value.  */
2476                 if (def
2477                     && TREE_CODE (def) == MODIFY_EXPR
2478                     && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
2479                   op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
2480                 else
2481                   break;
2482               }
2483           }
2484       }
2485
2486   /* A memory store, even an aliased store, creates a useful
2487      equivalence.  By exchanging the LHS and RHS, creating suitable
2488      vops and recording the result in the available expression table,
2489      we may be able to expose more redundant loads.  */
2490   if (!ann->has_volatile_ops
2491       && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
2492           || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
2493       && !is_gimple_reg (lhs))
2494     {
2495       tree rhs = TREE_OPERAND (stmt, 1);
2496       tree new;
2497       size_t j;
2498
2499       /* FIXME: If the LHS of the assignment is a bitfield and the RHS
2500          is a constant, we need to adjust the constant to fit into the
2501          type of the LHS.  If the LHS is a bitfield and the RHS is not
2502          a constant, then we can not record any equivalences for this
2503          statement since we would need to represent the widening or
2504          narrowing of RHS.  This fixes gcc.c-torture/execute/921016-1.c
2505          and should not be necessary if GCC represented bitfields
2506          properly.  */
2507       if (lhs_code == COMPONENT_REF
2508           && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
2509         {
2510           if (TREE_CONSTANT (rhs))
2511             rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
2512           else
2513             rhs = NULL;
2514
2515           /* If the value overflowed, then we can not use this equivalence.  */
2516           if (rhs && ! is_gimple_min_invariant (rhs))
2517             rhs = NULL;
2518         }
2519
2520       if (rhs)
2521         {
2522           v_may_def_optype v_may_defs = V_MAY_DEF_OPS (ann);
2523           v_must_def_optype v_must_defs = V_MUST_DEF_OPS (ann);
2524
2525           /* Build a new statement with the RHS and LHS exchanged.  */
2526           new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
2527
2528           /* Get an annotation and set up the real operands.  */
2529           get_stmt_ann (new);
2530           get_stmt_operands (new);
2531
2532           /* Clear out the virtual operands on the new statement, we are
2533              going to set them explicitly below.  */
2534           remove_vuses (new);
2535           remove_v_may_defs (new);
2536           remove_v_must_defs (new);
2537
2538           start_ssa_stmt_operands (new);
2539           /* For each VDEF on the original statement, we want to create a
2540              VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new 
2541              statement.  */
2542           for (j = 0; j < NUM_V_MAY_DEFS (v_may_defs); j++)
2543             {
2544               tree op = V_MAY_DEF_RESULT (v_may_defs, j);
2545               add_vuse (op, new);
2546             }
2547             
2548           for (j = 0; j < NUM_V_MUST_DEFS (v_must_defs); j++)
2549             {
2550               tree op = V_MUST_DEF_OP (v_must_defs, j);
2551               add_vuse (op, new);
2552             }
2553
2554           finalize_ssa_stmt_operands (new);
2555
2556           /* Finally enter the statement into the available expression
2557              table.  */
2558           lookup_avail_expr (new, block_avail_exprs_p, true);
2559         }
2560     }
2561 }
2562
2563 /* Optimize the statement pointed by iterator SI.
2564    
2565    We try to perform some simplistic global redundancy elimination and
2566    constant propagation:
2567
2568    1- To detect global redundancy, we keep track of expressions that have
2569       been computed in this block and its dominators.  If we find that the
2570       same expression is computed more than once, we eliminate repeated
2571       computations by using the target of the first one.
2572
2573    2- Constant values and copy assignments.  This is used to do very
2574       simplistic constant and copy propagation.  When a constant or copy
2575       assignment is found, we map the value on the RHS of the assignment to
2576       the variable in the LHS in the CONST_AND_COPIES table.  */
2577
2578 static void
2579 optimize_stmt (struct dom_walk_data *walk_data,
2580                basic_block bb ATTRIBUTE_UNUSED,
2581                block_stmt_iterator si)
2582 {
2583   stmt_ann_t ann;
2584   tree stmt;
2585   bool may_optimize_p;
2586   bool may_have_exposed_new_symbols = false;
2587   struct dom_walk_block_data *bd
2588     = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
2589
2590   stmt = bsi_stmt (si);
2591
2592   get_stmt_operands (stmt);
2593   ann = stmt_ann (stmt);
2594   opt_stats.num_stmts++;
2595   may_have_exposed_new_symbols = false;
2596
2597   if (dump_file && (dump_flags & TDF_DETAILS))
2598     {
2599       fprintf (dump_file, "Optimizing statement ");
2600       print_generic_stmt (dump_file, stmt, TDF_SLIM);
2601     }
2602
2603   /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs.  */
2604   may_have_exposed_new_symbols = cprop_into_stmt (stmt, const_and_copies);
2605
2606   /* If the statement has been modified with constant replacements,
2607      fold its RHS before checking for redundant computations.  */
2608   if (ann->modified)
2609     {
2610       /* Try to fold the statement making sure that STMT is kept
2611          up to date.  */
2612       if (fold_stmt (bsi_stmt_ptr (si)))
2613         {
2614           stmt = bsi_stmt (si);
2615           ann = stmt_ann (stmt);
2616
2617           if (dump_file && (dump_flags & TDF_DETAILS))
2618             {
2619               fprintf (dump_file, "  Folded to: ");
2620               print_generic_stmt (dump_file, stmt, TDF_SLIM);
2621             }
2622         }
2623
2624       /* Constant/copy propagation above may change the set of 
2625          virtual operands associated with this statement.  Folding
2626          may remove the need for some virtual operands.
2627
2628          Indicate we will need to rescan and rewrite the statement.  */
2629       may_have_exposed_new_symbols = true;
2630     }
2631
2632   /* Check for redundant computations.  Do this optimization only
2633      for assignments that have no volatile ops and conditionals.  */
2634   may_optimize_p = (!ann->has_volatile_ops
2635                     && ((TREE_CODE (stmt) == RETURN_EXPR
2636                          && TREE_OPERAND (stmt, 0)
2637                          && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
2638                          && ! (TREE_SIDE_EFFECTS
2639                                (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
2640                         || (TREE_CODE (stmt) == MODIFY_EXPR
2641                             && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
2642                         || TREE_CODE (stmt) == COND_EXPR
2643                         || TREE_CODE (stmt) == SWITCH_EXPR));
2644
2645   if (may_optimize_p)
2646     may_have_exposed_new_symbols
2647       |= eliminate_redundant_computations (walk_data, stmt, ann);
2648
2649   /* Record any additional equivalences created by this statement.  */
2650   if (TREE_CODE (stmt) == MODIFY_EXPR)
2651     record_equivalences_from_stmt (stmt,
2652                                    &bd->avail_exprs,
2653                                    &bd->nonzero_vars,
2654                                    may_optimize_p,
2655                                    ann);
2656
2657   register_definitions_for_stmt (ann, &bd->block_defs);
2658
2659   /* If STMT is a COND_EXPR and it was modified, then we may know
2660      where it goes.  If that is the case, then mark the CFG as altered.
2661
2662      This will cause us to later call remove_unreachable_blocks and
2663      cleanup_tree_cfg when it is safe to do so.  It is not safe to 
2664      clean things up here since removal of edges and such can trigger
2665      the removal of PHI nodes, which in turn can release SSA_NAMEs to
2666      the manager.
2667
2668      That's all fine and good, except that once SSA_NAMEs are released
2669      to the manager, we must not call create_ssa_name until all references
2670      to released SSA_NAMEs have been eliminated.
2671
2672      All references to the deleted SSA_NAMEs can not be eliminated until
2673      we remove unreachable blocks.
2674
2675      We can not remove unreachable blocks until after we have completed
2676      any queued jump threading.
2677
2678      We can not complete any queued jump threads until we have taken
2679      appropriate variables out of SSA form.  Taking variables out of
2680      SSA form can call create_ssa_name and thus we lose.
2681
2682      Ultimately I suspect we're going to need to change the interface
2683      into the SSA_NAME manager.  */
2684
2685   if (ann->modified)
2686     {
2687       tree val = NULL;
2688
2689       if (TREE_CODE (stmt) == COND_EXPR)
2690         val = COND_EXPR_COND (stmt);
2691       else if (TREE_CODE (stmt) == SWITCH_EXPR)
2692         val = SWITCH_COND (stmt);
2693
2694       if (val && TREE_CODE (val) == INTEGER_CST
2695           && find_taken_edge (bb_for_stmt (stmt), val))
2696         cfg_altered = true;
2697     }
2698                                                                                 
2699   if (may_have_exposed_new_symbols)
2700     {
2701       if (! bd->stmts_to_rescan)
2702         VARRAY_TREE_INIT (bd->stmts_to_rescan, 20, "stmts_to_rescan");
2703       VARRAY_PUSH_TREE (bd->stmts_to_rescan, bsi_stmt (si));
2704     }
2705 }
2706
2707 /* Replace the RHS of STMT with NEW_RHS.  If RHS can be found in the
2708    available expression hashtable, then return the LHS from the hash
2709    table.
2710
2711    If INSERT is true, then we also update the available expression
2712    hash table to account for the changes made to STMT.  */
2713
2714 static tree
2715 update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, 
2716                                   varray_type *block_avail_exprs_p,
2717                                   stmt_ann_t ann,
2718                                   bool insert)
2719 {
2720   tree cached_lhs = NULL;
2721
2722   /* Remove the old entry from the hash table.  */
2723   if (insert)
2724     {
2725       struct expr_hash_elt element;
2726
2727       initialize_hash_element (stmt, NULL, &element);
2728       htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
2729     }
2730
2731   /* Now update the RHS of the assignment.  */
2732   TREE_OPERAND (stmt, 1) = new_rhs;
2733
2734   /* Now lookup the updated statement in the hash table.  */
2735   cached_lhs = lookup_avail_expr (stmt, block_avail_exprs_p, insert);
2736
2737   /* We have now called lookup_avail_expr twice with two different
2738      versions of this same statement, once in optimize_stmt, once here.
2739
2740      We know the call in optimize_stmt did not find an existing entry
2741      in the hash table, so a new entry was created.  At the same time
2742      this statement was pushed onto the BLOCK_AVAIL_EXPRS varray. 
2743
2744      If this call failed to find an existing entry on the hash table,
2745      then the new version of this statement was entered into the
2746      hash table.  And this statement was pushed onto BLOCK_AVAIL_EXPR
2747      for the second time.  So there are two copies on BLOCK_AVAIL_EXPRs
2748
2749      If this call succeeded, we still have one copy of this statement
2750      on the BLOCK_AVAIL_EXPRs varray.
2751
2752      For both cases, we need to pop the most recent entry off the
2753      BLOCK_AVAIL_EXPRs varray.  For the case where we never found this
2754      statement in the hash tables, that will leave precisely one
2755      copy of this statement on BLOCK_AVAIL_EXPRs.  For the case where
2756      we found a copy of this statement in the second hash table lookup
2757      we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs.  */
2758   if (insert)
2759     VARRAY_POP (*block_avail_exprs_p);
2760
2761   /* And make sure we record the fact that we modified this
2762      statement.  */
2763   ann->modified = 1;
2764
2765   return cached_lhs;
2766 }
2767
2768 /* Search for an existing instance of STMT in the AVAIL_EXPRS table.  If
2769    found, return its LHS. Otherwise insert STMT in the table and return
2770    NULL_TREE.
2771
2772    Also, when an expression is first inserted in the AVAIL_EXPRS table, it
2773    is also added to the stack pointed by BLOCK_AVAIL_EXPRS_P, so that they
2774    can be removed when we finish processing this block and its children.
2775
2776    NOTE: This function assumes that STMT is a MODIFY_EXPR node that
2777    contains no CALL_EXPR on its RHS and makes no volatile nor
2778    aliased references.  */
2779
2780 static tree
2781 lookup_avail_expr (tree stmt, varray_type *block_avail_exprs_p, bool insert)
2782 {
2783   void **slot;
2784   tree lhs;
2785   tree temp;
2786   struct expr_hash_elt *element = xcalloc (sizeof (struct expr_hash_elt), 1);
2787
2788   lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
2789
2790   initialize_hash_element (stmt, lhs, element);
2791
2792   /* Don't bother remembering constant assignments and copy operations.
2793      Constants and copy operations are handled by the constant/copy propagator
2794      in optimize_stmt.  */
2795   if (TREE_CODE (element->rhs) == SSA_NAME
2796       || is_gimple_min_invariant (element->rhs))
2797     {
2798       free (element);
2799       return NULL_TREE;
2800     }
2801
2802   /* If this is an equality test against zero, see if we have recorded a
2803      nonzero value for the variable in question.  */
2804   if ((TREE_CODE (element->rhs) == EQ_EXPR
2805        || TREE_CODE  (element->rhs) == NE_EXPR)
2806       && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
2807       && integer_zerop (TREE_OPERAND (element->rhs, 1)))
2808     {
2809       int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
2810
2811       if (bitmap_bit_p (nonzero_vars, indx))
2812         {
2813           tree t = element->rhs;
2814           free (element);
2815
2816           if (TREE_CODE (t) == EQ_EXPR)
2817             return boolean_false_node;
2818           else
2819             return boolean_true_node;
2820         }
2821     }
2822
2823   /* Finally try to find the expression in the main expression hash table.  */
2824   slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
2825                                    (insert ? INSERT : NO_INSERT));
2826   if (slot == NULL)
2827     {
2828       free (element);
2829       return NULL_TREE;
2830     }
2831
2832   if (*slot == NULL)
2833     {
2834       *slot = (void *) element;
2835       if (! *block_avail_exprs_p)
2836         VARRAY_TREE_INIT (*block_avail_exprs_p, 20, "block_avail_exprs");
2837       VARRAY_PUSH_TREE (*block_avail_exprs_p, stmt ? stmt : element->rhs);
2838       return NULL_TREE;
2839     }
2840
2841   /* Extract the LHS of the assignment so that it can be used as the current
2842      definition of another variable.  */
2843   lhs = ((struct expr_hash_elt *)*slot)->lhs;
2844
2845   /* See if the LHS appears in the CONST_AND_COPIES table.  If it does, then
2846      use the value from the const_and_copies table.  */
2847   if (TREE_CODE (lhs) == SSA_NAME)
2848     {
2849       temp = get_value_for (lhs, const_and_copies);
2850       if (temp)
2851         lhs = temp;
2852     }
2853
2854   free (element);
2855   return lhs;
2856 }
2857
2858 /* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
2859    range of values that result in the conditional having a true value.
2860
2861    Return true if we are successful in extracting a range from COND and
2862    false if we are unsuccessful.  */
2863
2864 static bool
2865 extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
2866 {
2867   tree op1 = TREE_OPERAND (cond, 1);
2868   tree high, low, type;
2869   int inverted;
2870   
2871   /* Experiments have shown that it's rarely, if ever useful to
2872      record ranges for enumerations.  Presumably this is due to
2873      the fact that they're rarely used directly.  They are typically
2874      cast into an integer type and used that way.  */
2875   if (TREE_CODE (TREE_TYPE (op1)) != INTEGER_TYPE)
2876     return 0;
2877
2878   type = TREE_TYPE (op1);
2879
2880   switch (TREE_CODE (cond))
2881     {
2882     case EQ_EXPR:
2883       high = low = op1;
2884       inverted = 0;
2885       break;
2886
2887     case NE_EXPR:
2888       high = low = op1;
2889       inverted = 1;
2890       break;
2891
2892     case GE_EXPR:
2893       low = op1;
2894       high = TYPE_MAX_VALUE (type);
2895       inverted = 0;
2896       break;
2897
2898     case GT_EXPR:
2899       low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
2900       high = TYPE_MAX_VALUE (type);
2901       inverted = 0;
2902       break;
2903
2904     case LE_EXPR:
2905       high = op1;
2906       low = TYPE_MIN_VALUE (type);
2907       inverted = 0;
2908       break;
2909
2910     case LT_EXPR:
2911       high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
2912       low = TYPE_MIN_VALUE (type);
2913       inverted = 0;
2914       break;
2915
2916     default:
2917       return 0;
2918     }
2919
2920   *hi_p = high;
2921   *lo_p = low;
2922   *inverted_p = inverted;
2923   return 1;
2924 }
2925
2926 /* Record a range created by COND for basic block BB.  */
2927
2928 static void
2929 record_range (tree cond, basic_block bb, varray_type *vrp_variables_p)
2930 {
2931   /* We explicitly ignore NE_EXPRs.  They rarely allow for meaningful
2932      range optimizations and significantly complicate the implementation.  */
2933   if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<'
2934       && TREE_CODE (cond) != NE_EXPR
2935       && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
2936     {
2937       struct vrp_element *element = ggc_alloc (sizeof (struct vrp_element));
2938       int ssa_version = SSA_NAME_VERSION (TREE_OPERAND (cond, 0));
2939
2940       varray_type *vrp_records_p
2941         = (varray_type *)&VARRAY_GENERIC_PTR (vrp_data, ssa_version);
2942
2943       element->low = NULL;
2944       element->high = NULL;
2945       element->cond = cond;
2946       element->bb = bb;
2947
2948       if (*vrp_records_p == NULL)
2949         {
2950           VARRAY_GENERIC_PTR_INIT (*vrp_records_p, 2, "vrp records");
2951           VARRAY_GENERIC_PTR (vrp_data, ssa_version) = *vrp_records_p;
2952         }
2953       
2954       VARRAY_PUSH_GENERIC_PTR (*vrp_records_p, element);
2955       if (! *vrp_variables_p)
2956         VARRAY_TREE_INIT (*vrp_variables_p, 2, "vrp_variables");
2957       VARRAY_PUSH_TREE (*vrp_variables_p, TREE_OPERAND (cond, 0));
2958     }
2959 }
2960
2961 /* Given a conditional statement IF_STMT, return the assignment 'X = Y'
2962    known to be true depending on which arm of IF_STMT is taken.
2963
2964    Not all conditional statements will result in a useful assignment.
2965    Return NULL_TREE in that case.
2966
2967    Also enter into the available expression table statements of
2968    the form:
2969
2970      TRUE ARM           FALSE ARM
2971      1 = cond           1 = cond'
2972      0 = cond'          0 = cond
2973
2974    This allows us to lookup the condition in a dominated block and
2975    get back a constant indicating if the condition is true.  */
2976
2977 static struct eq_expr_value
2978 get_eq_expr_value (tree if_stmt,
2979                    int true_arm,
2980                    varray_type *block_avail_exprs_p,
2981                    basic_block bb,
2982                    varray_type *vrp_variables_p)
2983 {
2984   tree cond;
2985   struct eq_expr_value retval;
2986
2987   cond = COND_EXPR_COND (if_stmt);
2988   retval.src = NULL;
2989   retval.dst = NULL;
2990
2991   /* If the conditional is a single variable 'X', return 'X = 1' for
2992      the true arm and 'X = 0' on the false arm.   */
2993   if (TREE_CODE (cond) == SSA_NAME)
2994     {
2995       retval.dst = cond;
2996       retval.src = (true_arm ? integer_one_node : integer_zero_node);
2997       return retval;
2998     }
2999
3000   /* If we have a comparison expression, then record its result into
3001      the available expression table.  */
3002   if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<')
3003     {
3004       tree op0 = TREE_OPERAND (cond, 0);
3005       tree op1 = TREE_OPERAND (cond, 1);
3006
3007       /* Special case comparing booleans against a constant as we know
3008          the value of OP0 on both arms of the branch.  ie, we can record
3009          an equivalence for OP0 rather than COND.  */
3010       if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
3011           && TREE_CODE (op0) == SSA_NAME
3012           && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
3013           && is_gimple_min_invariant (op1))
3014         {
3015           if ((TREE_CODE (cond) == EQ_EXPR && true_arm)
3016               || (TREE_CODE (cond) == NE_EXPR && ! true_arm))
3017             {
3018               retval.src = op1;
3019             }
3020           else
3021             {
3022               if (integer_zerop (op1))
3023                 retval.src = boolean_true_node;
3024               else
3025                 retval.src = boolean_false_node;
3026             }
3027           retval.dst = op0;
3028           return retval;
3029         }
3030
3031       if (TREE_CODE (op0) == SSA_NAME
3032           && (is_gimple_min_invariant (op1) || TREE_CODE (op1) == SSA_NAME))
3033         {
3034           tree inverted = invert_truthvalue (cond);
3035
3036           /* When we find an available expression in the hash table, we replace
3037              the expression with the LHS of the statement in the hash table.
3038
3039              So, we want to build statements such as "1 = <condition>" on the
3040              true arm and "0 = <condition>" on the false arm.  That way if we
3041              find the expression in the table, we will replace it with its
3042              known constant value.  Also insert inversions of the result and
3043              condition into the hash table.  */
3044           if (true_arm)
3045             {
3046               record_cond (cond, boolean_true_node, block_avail_exprs_p);
3047               record_cond (inverted, boolean_false_node, block_avail_exprs_p);
3048
3049               if (TREE_CONSTANT (op1))
3050                 record_range (cond, bb, vrp_variables_p);
3051
3052                 /* If the conditional is of the form 'X == Y', return 'X = Y'
3053                    for the true arm.  */
3054               if (TREE_CODE (cond) == EQ_EXPR)
3055                 {
3056                   retval.dst = op0;
3057                   retval.src = op1;
3058                   return retval;
3059                 }
3060             }
3061           else
3062             {
3063
3064               record_cond (inverted, boolean_true_node, block_avail_exprs_p);
3065               record_cond (cond, boolean_false_node, block_avail_exprs_p);
3066
3067               if (TREE_CONSTANT (op1))
3068                 record_range (inverted, bb, vrp_variables_p);
3069
3070                 /* If the conditional is of the form 'X != Y', return 'X = Y'
3071                    for the false arm.  */
3072               if (TREE_CODE (cond) == NE_EXPR)
3073                 {
3074                   retval.dst = op0;
3075                   retval.src = op1;
3076                   return retval;
3077                 }
3078             }
3079         }
3080     }
3081
3082   return retval;
3083 }
3084
3085 /* Hashing and equality functions for AVAIL_EXPRS.  The table stores
3086    MODIFY_EXPR statements.  We compute a value number for expressions using
3087    the code of the expression and the SSA numbers of its operands.  */
3088
3089 static hashval_t
3090 avail_expr_hash (const void *p)
3091 {
3092   stmt_ann_t ann = ((struct expr_hash_elt *)p)->ann;
3093   tree rhs = ((struct expr_hash_elt *)p)->rhs;
3094   hashval_t val = 0;
3095   size_t i;
3096   vuse_optype vuses;
3097
3098   /* iterative_hash_expr knows how to deal with any expression and
3099      deals with commutative operators as well, so just use it instead
3100      of duplicating such complexities here.  */
3101   val = iterative_hash_expr (rhs, val);
3102
3103   /* If the hash table entry is not associated with a statement, then we
3104      can just hash the expression and not worry about virtual operands
3105      and such.  */
3106   if (!ann)
3107     return val;
3108
3109   /* Add the SSA version numbers of every vuse operand.  This is important
3110      because compound variables like arrays are not renamed in the
3111      operands.  Rather, the rename is done on the virtual variable
3112      representing all the elements of the array.  */
3113   vuses = VUSE_OPS (ann);
3114   for (i = 0; i < NUM_VUSES (vuses); i++)
3115     val = iterative_hash_expr (VUSE_OP (vuses, i), val);
3116
3117   return val;
3118 }
3119
3120
3121 static int
3122 avail_expr_eq (const void *p1, const void *p2)
3123 {
3124   stmt_ann_t ann1 = ((struct expr_hash_elt *)p1)->ann;
3125   tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
3126   stmt_ann_t ann2 = ((struct expr_hash_elt *)p2)->ann;
3127   tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
3128
3129   /* If they are the same physical expression, return true.  */
3130   if (rhs1 == rhs2 && ann1 == ann2)
3131     return true;
3132
3133   /* If their codes are not equal, then quit now.  */
3134   if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
3135     return false;
3136
3137   /* In case of a collision, both RHS have to be identical and have the
3138      same VUSE operands.  */
3139   if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
3140        || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
3141       && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
3142     {
3143       vuse_optype ops1 = NULL;
3144       vuse_optype ops2 = NULL;
3145       size_t num_ops1 = 0;
3146       size_t num_ops2 = 0;
3147       size_t i;
3148
3149       if (ann1)
3150         {
3151           ops1 = VUSE_OPS (ann1);
3152           num_ops1 = NUM_VUSES (ops1);
3153         }
3154
3155       if (ann2)
3156         {
3157           ops2 = VUSE_OPS (ann2);
3158           num_ops2 = NUM_VUSES (ops2);
3159         }
3160
3161       /* If the number of virtual uses is different, then we consider
3162          them not equal.  */
3163       if (num_ops1 != num_ops2)
3164         return false;
3165
3166       for (i = 0; i < num_ops1; i++)
3167         if (VUSE_OP (ops1, i) != VUSE_OP (ops2, i))
3168           return false;
3169
3170 #ifdef ENABLE_CHECKING
3171       if (((struct expr_hash_elt *)p1)->hash
3172           != ((struct expr_hash_elt *)p2)->hash)
3173         abort ();
3174 #endif
3175       return true;
3176     }
3177
3178   return false;
3179 }
3180
3181 /* Given STMT and a pointer to the block local defintions BLOCK_DEFS_P,
3182    register register all objects set by this statement into BLOCK_DEFS_P
3183    and CURRDEFS.  */
3184
3185 static void
3186 register_definitions_for_stmt (stmt_ann_t ann, varray_type *block_defs_p)
3187 {
3188   def_optype defs;
3189   v_may_def_optype v_may_defs;
3190   v_must_def_optype v_must_defs;
3191   unsigned int i;
3192
3193   defs = DEF_OPS (ann);
3194   for (i = 0; i < NUM_DEFS (defs); i++)
3195     {
3196       tree def = DEF_OP (defs, i);
3197
3198       /* FIXME: We shouldn't be registering new defs if the variable
3199          doesn't need to be renamed.  */
3200       register_new_def (def, block_defs_p);
3201     }
3202
3203   /* Register new virtual definitions made by the statement.  */
3204   v_may_defs = V_MAY_DEF_OPS (ann);
3205   for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
3206     {
3207       /* FIXME: We shouldn't be registering new defs if the variable
3208          doesn't need to be renamed.  */
3209       register_new_def (V_MAY_DEF_RESULT (v_may_defs, i), block_defs_p);
3210     }
3211     
3212   /* Register new virtual mustdefs made by the statement.  */
3213   v_must_defs = V_MUST_DEF_OPS (ann);
3214   for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
3215     {
3216       /* FIXME: We shouldn't be registering new defs if the variable
3217          doesn't need to be renamed.  */
3218       register_new_def (V_MUST_DEF_OP (v_must_defs, i), block_defs_p);
3219     }
3220 }
3221