OSDN Git Service

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