OSDN Git Service

2011-08-11 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-sccvn.c
1 /* SCC value numbering for trees
2    Copyright (C) 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Daniel Berlin <dan@dberlin.org>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "basic-block.h"
28 #include "tree-pretty-print.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-inline.h"
31 #include "tree-flow.h"
32 #include "gimple.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "fibheap.h"
36 #include "hashtab.h"
37 #include "tree-iterator.h"
38 #include "alloc-pool.h"
39 #include "tree-pass.h"
40 #include "flags.h"
41 #include "bitmap.h"
42 #include "langhooks.h"
43 #include "cfgloop.h"
44 #include "params.h"
45 #include "tree-ssa-propagate.h"
46 #include "tree-ssa-sccvn.h"
47 #include "gimple-fold.h"
48
49 /* This algorithm is based on the SCC algorithm presented by Keith
50    Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
51    (http://citeseer.ist.psu.edu/41805.html).  In
52    straight line code, it is equivalent to a regular hash based value
53    numbering that is performed in reverse postorder.
54
55    For code with cycles, there are two alternatives, both of which
56    require keeping the hashtables separate from the actual list of
57    value numbers for SSA names.
58
59    1. Iterate value numbering in an RPO walk of the blocks, removing
60    all the entries from the hashtable after each iteration (but
61    keeping the SSA name->value number mapping between iterations).
62    Iterate until it does not change.
63
64    2. Perform value numbering as part of an SCC walk on the SSA graph,
65    iterating only the cycles in the SSA graph until they do not change
66    (using a separate, optimistic hashtable for value numbering the SCC
67    operands).
68
69    The second is not just faster in practice (because most SSA graph
70    cycles do not involve all the variables in the graph), it also has
71    some nice properties.
72
73    One of these nice properties is that when we pop an SCC off the
74    stack, we are guaranteed to have processed all the operands coming from
75    *outside of that SCC*, so we do not need to do anything special to
76    ensure they have value numbers.
77
78    Another nice property is that the SCC walk is done as part of a DFS
79    of the SSA graph, which makes it easy to perform combining and
80    simplifying operations at the same time.
81
82    The code below is deliberately written in a way that makes it easy
83    to separate the SCC walk from the other work it does.
84
85    In order to propagate constants through the code, we track which
86    expressions contain constants, and use those while folding.  In
87    theory, we could also track expressions whose value numbers are
88    replaced, in case we end up folding based on expression
89    identities.
90
91    In order to value number memory, we assign value numbers to vuses.
92    This enables us to note that, for example, stores to the same
93    address of the same value from the same starting memory states are
94    equivalent.
95    TODO:
96
97    1. We can iterate only the changing portions of the SCC's, but
98    I have not seen an SCC big enough for this to be a win.
99    2. If you differentiate between phi nodes for loops and phi nodes
100    for if-then-else, you can properly consider phi nodes in different
101    blocks for equivalence.
102    3. We could value number vuses in more cases, particularly, whole
103    structure copies.
104 */
105
106 /* The set of hashtables and alloc_pool's for their items.  */
107
108 typedef struct vn_tables_s
109 {
110   htab_t nary;
111   htab_t phis;
112   htab_t references;
113   struct obstack nary_obstack;
114   alloc_pool phis_pool;
115   alloc_pool references_pool;
116 } *vn_tables_t;
117
118 static htab_t constant_to_value_id;
119 static bitmap constant_value_ids;
120
121
122 /* Valid hashtables storing information we have proven to be
123    correct.  */
124
125 static vn_tables_t valid_info;
126
127 /* Optimistic hashtables storing information we are making assumptions about
128    during iterations.  */
129
130 static vn_tables_t optimistic_info;
131
132 /* Pointer to the set of hashtables that is currently being used.
133    Should always point to either the optimistic_info, or the
134    valid_info.  */
135
136 static vn_tables_t current_info;
137
138
139 /* Reverse post order index for each basic block.  */
140
141 static int *rpo_numbers;
142
143 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
144
145 /* This represents the top of the VN lattice, which is the universal
146    value.  */
147
148 tree VN_TOP;
149
150 /* Unique counter for our value ids.  */
151
152 static unsigned int next_value_id;
153
154 /* Next DFS number and the stack for strongly connected component
155    detection. */
156
157 static unsigned int next_dfs_num;
158 static VEC (tree, heap) *sccstack;
159
160
161 DEF_VEC_P(vn_ssa_aux_t);
162 DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
163
164 /* Table of vn_ssa_aux_t's, one per ssa_name.  The vn_ssa_aux_t objects
165    are allocated on an obstack for locality reasons, and to free them
166    without looping over the VEC.  */
167
168 static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
169 static struct obstack vn_ssa_aux_obstack;
170
171 /* Return the value numbering information for a given SSA name.  */
172
173 vn_ssa_aux_t
174 VN_INFO (tree name)
175 {
176   vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
177                                 SSA_NAME_VERSION (name));
178   gcc_checking_assert (res);
179   return res;
180 }
181
182 /* Set the value numbering info for a given SSA name to a given
183    value.  */
184
185 static inline void
186 VN_INFO_SET (tree name, vn_ssa_aux_t value)
187 {
188   VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
189                SSA_NAME_VERSION (name), value);
190 }
191
192 /* Initialize the value numbering info for a given SSA name.
193    This should be called just once for every SSA name.  */
194
195 vn_ssa_aux_t
196 VN_INFO_GET (tree name)
197 {
198   vn_ssa_aux_t newinfo;
199
200   newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
201   memset (newinfo, 0, sizeof (struct vn_ssa_aux));
202   if (SSA_NAME_VERSION (name) >= VEC_length (vn_ssa_aux_t, vn_ssa_aux_table))
203     VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table,
204                    SSA_NAME_VERSION (name) + 1);
205   VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
206                SSA_NAME_VERSION (name), newinfo);
207   return newinfo;
208 }
209
210
211 /* Get the representative expression for the SSA_NAME NAME.  Returns
212    the representative SSA_NAME if there is no expression associated with it.  */
213
214 tree
215 vn_get_expr_for (tree name)
216 {
217   vn_ssa_aux_t vn = VN_INFO (name);
218   gimple def_stmt;
219   tree expr = NULL_TREE;
220
221   if (vn->valnum == VN_TOP)
222     return name;
223
224   /* If the value-number is a constant it is the representative
225      expression.  */
226   if (TREE_CODE (vn->valnum) != SSA_NAME)
227     return vn->valnum;
228
229   /* Get to the information of the value of this SSA_NAME.  */
230   vn = VN_INFO (vn->valnum);
231
232   /* If the value-number is a constant it is the representative
233      expression.  */
234   if (TREE_CODE (vn->valnum) != SSA_NAME)
235     return vn->valnum;
236
237   /* Else if we have an expression, return it.  */
238   if (vn->expr != NULL_TREE)
239     return vn->expr;
240
241   /* Otherwise use the defining statement to build the expression.  */
242   def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
243
244   /* If the value number is a default-definition or a PHI result
245      use it directly.  */
246   if (gimple_nop_p (def_stmt)
247       || gimple_code (def_stmt) == GIMPLE_PHI)
248     return vn->valnum;
249
250   if (!is_gimple_assign (def_stmt))
251     return vn->valnum;
252
253   /* FIXME tuples.  This is incomplete and likely will miss some
254      simplifications.  */
255   switch (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)))
256     {
257     case tcc_reference:
258       if ((gimple_assign_rhs_code (def_stmt) == VIEW_CONVERT_EXPR
259            || gimple_assign_rhs_code (def_stmt) == REALPART_EXPR
260            || gimple_assign_rhs_code (def_stmt) == IMAGPART_EXPR)
261           && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
262         expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
263                             gimple_expr_type (def_stmt),
264                             TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
265       break;
266
267     case tcc_unary:
268       expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
269                           gimple_expr_type (def_stmt),
270                           gimple_assign_rhs1 (def_stmt));
271       break;
272
273     case tcc_binary:
274       expr = fold_build2 (gimple_assign_rhs_code (def_stmt),
275                           gimple_expr_type (def_stmt),
276                           gimple_assign_rhs1 (def_stmt),
277                           gimple_assign_rhs2 (def_stmt));
278       break;
279
280     default:;
281     }
282   if (expr == NULL_TREE)
283     return vn->valnum;
284
285   /* Cache the expression.  */
286   vn->expr = expr;
287
288   return expr;
289 }
290
291
292 /* Free a phi operation structure VP.  */
293
294 static void
295 free_phi (void *vp)
296 {
297   vn_phi_t phi = (vn_phi_t) vp;
298   VEC_free (tree, heap, phi->phiargs);
299 }
300
301 /* Free a reference operation structure VP.  */
302
303 static void
304 free_reference (void *vp)
305 {
306   vn_reference_t vr = (vn_reference_t) vp;
307   VEC_free (vn_reference_op_s, heap, vr->operands);
308 }
309
310 /* Hash table equality function for vn_constant_t.  */
311
312 static int
313 vn_constant_eq (const void *p1, const void *p2)
314 {
315   const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
316   const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
317
318   if (vc1->hashcode != vc2->hashcode)
319     return false;
320
321   return vn_constant_eq_with_type (vc1->constant, vc2->constant);
322 }
323
324 /* Hash table hash function for vn_constant_t.  */
325
326 static hashval_t
327 vn_constant_hash (const void *p1)
328 {
329   const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
330   return vc1->hashcode;
331 }
332
333 /* Lookup a value id for CONSTANT and return it.  If it does not
334    exist returns 0.  */
335
336 unsigned int
337 get_constant_value_id (tree constant)
338 {
339   void **slot;
340   struct vn_constant_s vc;
341
342   vc.hashcode = vn_hash_constant_with_type (constant);
343   vc.constant = constant;
344   slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
345                                    vc.hashcode, NO_INSERT);
346   if (slot)
347     return ((vn_constant_t)*slot)->value_id;
348   return 0;
349 }
350
351 /* Lookup a value id for CONSTANT, and if it does not exist, create a
352    new one and return it.  If it does exist, return it.  */
353
354 unsigned int
355 get_or_alloc_constant_value_id (tree constant)
356 {
357   void **slot;
358   struct vn_constant_s vc;
359   vn_constant_t vcp;
360
361   vc.hashcode = vn_hash_constant_with_type (constant);
362   vc.constant = constant;
363   slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
364                                    vc.hashcode, INSERT);
365   if (*slot)
366     return ((vn_constant_t)*slot)->value_id;
367
368   vcp = XNEW (struct vn_constant_s);
369   vcp->hashcode = vc.hashcode;
370   vcp->constant = constant;
371   vcp->value_id = get_next_value_id ();
372   *slot = (void *) vcp;
373   bitmap_set_bit (constant_value_ids, vcp->value_id);
374   return vcp->value_id;
375 }
376
377 /* Return true if V is a value id for a constant.  */
378
379 bool
380 value_id_constant_p (unsigned int v)
381 {
382   return bitmap_bit_p (constant_value_ids, v);
383 }
384
385 /* Compare two reference operands P1 and P2 for equality.  Return true if
386    they are equal, and false otherwise.  */
387
388 static int
389 vn_reference_op_eq (const void *p1, const void *p2)
390 {
391   const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
392   const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
393
394   return (vro1->opcode == vro2->opcode
395           /* We do not care for differences in type qualification.  */
396           && (vro1->type == vro2->type
397               || (vro1->type && vro2->type
398                   && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
399                                          TYPE_MAIN_VARIANT (vro2->type))))
400           && expressions_equal_p (vro1->op0, vro2->op0)
401           && expressions_equal_p (vro1->op1, vro2->op1)
402           && expressions_equal_p (vro1->op2, vro2->op2));
403 }
404
405 /* Compute the hash for a reference operand VRO1.  */
406
407 static hashval_t
408 vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
409 {
410   result = iterative_hash_hashval_t (vro1->opcode, result);
411   if (vro1->op0)
412     result = iterative_hash_expr (vro1->op0, result);
413   if (vro1->op1)
414     result = iterative_hash_expr (vro1->op1, result);
415   if (vro1->op2)
416     result = iterative_hash_expr (vro1->op2, result);
417   return result;
418 }
419
420 /* Return the hashcode for a given reference operation P1.  */
421
422 static hashval_t
423 vn_reference_hash (const void *p1)
424 {
425   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
426   return vr1->hashcode;
427 }
428
429 /* Compute a hash for the reference operation VR1 and return it.  */
430
431 hashval_t
432 vn_reference_compute_hash (const vn_reference_t vr1)
433 {
434   hashval_t result = 0;
435   int i;
436   vn_reference_op_t vro;
437   HOST_WIDE_INT off = -1;
438   bool deref = false;
439
440   FOR_EACH_VEC_ELT (vn_reference_op_s, vr1->operands, i, vro)
441     {
442       if (vro->opcode == MEM_REF)
443         deref = true;
444       else if (vro->opcode != ADDR_EXPR)
445         deref = false;
446       if (vro->off != -1)
447         {
448           if (off == -1)
449             off = 0;
450           off += vro->off;
451         }
452       else
453         {
454           if (off != -1
455               && off != 0)
456             result = iterative_hash_hashval_t (off, result);
457           off = -1;
458           if (deref
459               && vro->opcode == ADDR_EXPR)
460             {
461               if (vro->op0)
462                 {
463                   tree op = TREE_OPERAND (vro->op0, 0);
464                   result = iterative_hash_hashval_t (TREE_CODE (op), result);
465                   result = iterative_hash_expr (op, result);
466                 }
467             }
468           else
469             result = vn_reference_op_compute_hash (vro, result);
470         }
471     }
472   if (vr1->vuse)
473     result += SSA_NAME_VERSION (vr1->vuse);
474
475   return result;
476 }
477
478 /* Return true if reference operations P1 and P2 are equivalent.  This
479    means they have the same set of operands and vuses.  */
480
481 int
482 vn_reference_eq (const void *p1, const void *p2)
483 {
484   unsigned i, j;
485
486   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
487   const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
488   if (vr1->hashcode != vr2->hashcode)
489     return false;
490
491   /* Early out if this is not a hash collision.  */
492   if (vr1->hashcode != vr2->hashcode)
493     return false;
494
495   /* The VOP needs to be the same.  */
496   if (vr1->vuse != vr2->vuse)
497     return false;
498
499   /* If the operands are the same we are done.  */
500   if (vr1->operands == vr2->operands)
501     return true;
502
503   if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
504     return false;
505
506   if (INTEGRAL_TYPE_P (vr1->type)
507       && INTEGRAL_TYPE_P (vr2->type))
508     {
509       if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
510         return false;
511     }
512   else if (INTEGRAL_TYPE_P (vr1->type)
513            && (TYPE_PRECISION (vr1->type)
514                != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
515     return false;
516   else if (INTEGRAL_TYPE_P (vr2->type)
517            && (TYPE_PRECISION (vr2->type)
518                != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
519     return false;
520
521   i = 0;
522   j = 0;
523   do
524     {
525       HOST_WIDE_INT off1 = 0, off2 = 0;
526       vn_reference_op_t vro1, vro2;
527       vn_reference_op_s tem1, tem2;
528       bool deref1 = false, deref2 = false;
529       for (; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro1); i++)
530         {
531           if (vro1->opcode == MEM_REF)
532             deref1 = true;
533           if (vro1->off == -1)
534             break;
535           off1 += vro1->off;
536         }
537       for (; VEC_iterate (vn_reference_op_s, vr2->operands, j, vro2); j++)
538         {
539           if (vro2->opcode == MEM_REF)
540             deref2 = true;
541           if (vro2->off == -1)
542             break;
543           off2 += vro2->off;
544         }
545       if (off1 != off2)
546         return false;
547       if (deref1 && vro1->opcode == ADDR_EXPR)
548         {
549           memset (&tem1, 0, sizeof (tem1));
550           tem1.op0 = TREE_OPERAND (vro1->op0, 0);
551           tem1.type = TREE_TYPE (tem1.op0);
552           tem1.opcode = TREE_CODE (tem1.op0);
553           vro1 = &tem1;
554         }
555       if (deref2 && vro2->opcode == ADDR_EXPR)
556         {
557           memset (&tem2, 0, sizeof (tem2));
558           tem2.op0 = TREE_OPERAND (vro2->op0, 0);
559           tem2.type = TREE_TYPE (tem2.op0);
560           tem2.opcode = TREE_CODE (tem2.op0);
561           vro2 = &tem2;
562         }
563       if (!vn_reference_op_eq (vro1, vro2))
564         return false;
565       ++j;
566       ++i;
567     }
568   while (VEC_length (vn_reference_op_s, vr1->operands) != i
569          || VEC_length (vn_reference_op_s, vr2->operands) != j);
570
571   return true;
572 }
573
574 /* Copy the operations present in load/store REF into RESULT, a vector of
575    vn_reference_op_s's.  */
576
577 void
578 copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
579 {
580   if (TREE_CODE (ref) == TARGET_MEM_REF)
581     {
582       vn_reference_op_s temp;
583
584       memset (&temp, 0, sizeof (temp));
585       temp.type = TREE_TYPE (ref);
586       temp.opcode = TREE_CODE (ref);
587       temp.op0 = TMR_INDEX (ref);
588       temp.op1 = TMR_STEP (ref);
589       temp.op2 = TMR_OFFSET (ref);
590       temp.off = -1;
591       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
592
593       memset (&temp, 0, sizeof (temp));
594       temp.type = NULL_TREE;
595       temp.opcode = ERROR_MARK;
596       temp.op0 = TMR_INDEX2 (ref);
597       temp.off = -1;
598       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
599
600       memset (&temp, 0, sizeof (temp));
601       temp.type = NULL_TREE;
602       temp.opcode = TREE_CODE (TMR_BASE (ref));
603       temp.op0 = TMR_BASE (ref);
604       temp.off = -1;
605       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
606       return;
607     }
608
609   /* For non-calls, store the information that makes up the address.  */
610
611   while (ref)
612     {
613       vn_reference_op_s temp;
614
615       memset (&temp, 0, sizeof (temp));
616       temp.type = TREE_TYPE (ref);
617       temp.opcode = TREE_CODE (ref);
618       temp.off = -1;
619
620       switch (temp.opcode)
621         {
622         case MEM_REF:
623           /* The base address gets its own vn_reference_op_s structure.  */
624           temp.op0 = TREE_OPERAND (ref, 1);
625           if (host_integerp (TREE_OPERAND (ref, 1), 0))
626             temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
627           break;
628         case BIT_FIELD_REF:
629           /* Record bits and position.  */
630           temp.op0 = TREE_OPERAND (ref, 1);
631           temp.op1 = TREE_OPERAND (ref, 2);
632           break;
633         case COMPONENT_REF:
634           /* The field decl is enough to unambiguously specify the field,
635              a matching type is not necessary and a mismatching type
636              is always a spurious difference.  */
637           temp.type = NULL_TREE;
638           temp.op0 = TREE_OPERAND (ref, 1);
639           temp.op1 = TREE_OPERAND (ref, 2);
640           {
641             tree this_offset = component_ref_field_offset (ref);
642             if (this_offset
643                 && TREE_CODE (this_offset) == INTEGER_CST)
644               {
645                 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
646                 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
647                   {
648                     double_int off
649                       = double_int_add (tree_to_double_int (this_offset),
650                                         double_int_rshift
651                                           (tree_to_double_int (bit_offset),
652                                            BITS_PER_UNIT == 8
653                                            ? 3 : exact_log2 (BITS_PER_UNIT),
654                                            HOST_BITS_PER_DOUBLE_INT, true));
655                     if (double_int_fits_in_shwi_p (off))
656                       temp.off = off.low;
657                   }
658               }
659           }
660           break;
661         case ARRAY_RANGE_REF:
662         case ARRAY_REF:
663           /* Record index as operand.  */
664           temp.op0 = TREE_OPERAND (ref, 1);
665           /* Always record lower bounds and element size.  */
666           temp.op1 = array_ref_low_bound (ref);
667           temp.op2 = array_ref_element_size (ref);
668           if (TREE_CODE (temp.op0) == INTEGER_CST
669               && TREE_CODE (temp.op1) == INTEGER_CST
670               && TREE_CODE (temp.op2) == INTEGER_CST)
671             {
672               double_int off = tree_to_double_int (temp.op0);
673               off = double_int_add (off,
674                                     double_int_neg
675                                       (tree_to_double_int (temp.op1)));
676               off = double_int_mul (off, tree_to_double_int (temp.op2));
677               if (double_int_fits_in_shwi_p (off))
678                 temp.off = off.low;
679             }
680           break;
681         case VAR_DECL:
682           if (DECL_HARD_REGISTER (ref))
683             {
684               temp.op0 = ref;
685               break;
686             }
687           /* Fallthru.  */
688         case PARM_DECL:
689         case CONST_DECL:
690         case RESULT_DECL:
691           /* Canonicalize decls to MEM[&decl] which is what we end up with
692              when valueizing MEM[ptr] with ptr = &decl.  */
693           temp.opcode = MEM_REF;
694           temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
695           temp.off = 0;
696           VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
697           temp.opcode = ADDR_EXPR;
698           temp.op0 = build_fold_addr_expr (ref);
699           temp.type = TREE_TYPE (temp.op0);
700           temp.off = -1;
701           break;
702         case STRING_CST:
703         case INTEGER_CST:
704         case COMPLEX_CST:
705         case VECTOR_CST:
706         case REAL_CST:
707         case FIXED_CST:
708         case CONSTRUCTOR:
709         case SSA_NAME:
710           temp.op0 = ref;
711           break;
712         case ADDR_EXPR:
713           if (is_gimple_min_invariant (ref))
714             {
715               temp.op0 = ref;
716               break;
717             }
718           /* Fallthrough.  */
719           /* These are only interesting for their operands, their
720              existence, and their type.  They will never be the last
721              ref in the chain of references (IE they require an
722              operand), so we don't have to put anything
723              for op* as it will be handled by the iteration  */
724         case REALPART_EXPR:
725         case VIEW_CONVERT_EXPR:
726           temp.off = 0;
727           break;
728         case IMAGPART_EXPR:
729           /* This is only interesting for its constant offset.  */
730           temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
731           break;
732         default:
733           gcc_unreachable ();
734         }
735       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
736
737       if (REFERENCE_CLASS_P (ref)
738           || (TREE_CODE (ref) == ADDR_EXPR
739               && !is_gimple_min_invariant (ref)))
740         ref = TREE_OPERAND (ref, 0);
741       else
742         ref = NULL_TREE;
743     }
744 }
745
746 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
747    operands in *OPS, the reference alias set SET and the reference type TYPE.
748    Return true if something useful was produced.  */
749
750 bool
751 ao_ref_init_from_vn_reference (ao_ref *ref,
752                                alias_set_type set, tree type,
753                                VEC (vn_reference_op_s, heap) *ops)
754 {
755   vn_reference_op_t op;
756   unsigned i;
757   tree base = NULL_TREE;
758   tree *op0_p = &base;
759   HOST_WIDE_INT offset = 0;
760   HOST_WIDE_INT max_size;
761   HOST_WIDE_INT size = -1;
762   tree size_tree = NULL_TREE;
763   alias_set_type base_alias_set = -1;
764
765   /* First get the final access size from just the outermost expression.  */
766   op = VEC_index (vn_reference_op_s, ops, 0);
767   if (op->opcode == COMPONENT_REF)
768     size_tree = DECL_SIZE (op->op0);
769   else if (op->opcode == BIT_FIELD_REF)
770     size_tree = op->op0;
771   else
772     {
773       enum machine_mode mode = TYPE_MODE (type);
774       if (mode == BLKmode)
775         size_tree = TYPE_SIZE (type);
776       else
777         size = GET_MODE_BITSIZE (mode);
778     }
779   if (size_tree != NULL_TREE)
780     {
781       if (!host_integerp (size_tree, 1))
782         size = -1;
783       else
784         size = TREE_INT_CST_LOW (size_tree);
785     }
786
787   /* Initially, maxsize is the same as the accessed element size.
788      In the following it will only grow (or become -1).  */
789   max_size = size;
790
791   /* Compute cumulative bit-offset for nested component-refs and array-refs,
792      and find the ultimate containing object.  */
793   FOR_EACH_VEC_ELT (vn_reference_op_s, ops, i, op)
794     {
795       switch (op->opcode)
796         {
797         /* These may be in the reference ops, but we cannot do anything
798            sensible with them here.  */
799         case ADDR_EXPR:
800           /* Apart from ADDR_EXPR arguments to MEM_REF.  */
801           if (base != NULL_TREE
802               && TREE_CODE (base) == MEM_REF
803               && op->op0
804               && DECL_P (TREE_OPERAND (op->op0, 0)))
805             {
806               vn_reference_op_t pop = VEC_index (vn_reference_op_s, ops, i-1);
807               base = TREE_OPERAND (op->op0, 0);
808               if (pop->off == -1)
809                 {
810                   max_size = -1;
811                   offset = 0;
812                 }
813               else
814                 offset += pop->off * BITS_PER_UNIT;
815               op0_p = NULL;
816               break;
817             }
818           /* Fallthru.  */
819         case CALL_EXPR:
820           return false;
821
822         /* Record the base objects.  */
823         case MEM_REF:
824           base_alias_set = get_deref_alias_set (op->op0);
825           *op0_p = build2 (MEM_REF, op->type,
826                            NULL_TREE, op->op0);
827           op0_p = &TREE_OPERAND (*op0_p, 0);
828           break;
829
830         case VAR_DECL:
831         case PARM_DECL:
832         case RESULT_DECL:
833         case SSA_NAME:
834           *op0_p = op->op0;
835           op0_p = NULL;
836           break;
837
838         /* And now the usual component-reference style ops.  */
839         case BIT_FIELD_REF:
840           offset += tree_low_cst (op->op1, 0);
841           break;
842
843         case COMPONENT_REF:
844           {
845             tree field = op->op0;
846             /* We do not have a complete COMPONENT_REF tree here so we
847                cannot use component_ref_field_offset.  Do the interesting
848                parts manually.  */
849
850             if (op->op1
851                 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
852               max_size = -1;
853             else
854               {
855                 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
856                            * BITS_PER_UNIT);
857                 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
858               }
859             break;
860           }
861
862         case ARRAY_RANGE_REF:
863         case ARRAY_REF:
864           /* We recorded the lower bound and the element size.  */
865           if (!host_integerp (op->op0, 0)
866               || !host_integerp (op->op1, 0)
867               || !host_integerp (op->op2, 0))
868             max_size = -1;
869           else
870             {
871               HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
872               hindex -= TREE_INT_CST_LOW (op->op1);
873               hindex *= TREE_INT_CST_LOW (op->op2);
874               hindex *= BITS_PER_UNIT;
875               offset += hindex;
876             }
877           break;
878
879         case REALPART_EXPR:
880           break;
881
882         case IMAGPART_EXPR:
883           offset += size;
884           break;
885
886         case VIEW_CONVERT_EXPR:
887           break;
888
889         case STRING_CST:
890         case INTEGER_CST:
891         case COMPLEX_CST:
892         case VECTOR_CST:
893         case REAL_CST:
894         case CONSTRUCTOR:
895         case CONST_DECL:
896           return false;
897
898         default:
899           return false;
900         }
901     }
902
903   if (base == NULL_TREE)
904     return false;
905
906   ref->ref = NULL_TREE;
907   ref->base = base;
908   ref->offset = offset;
909   ref->size = size;
910   ref->max_size = max_size;
911   ref->ref_alias_set = set;
912   if (base_alias_set != -1)
913     ref->base_alias_set = base_alias_set;
914   else
915     ref->base_alias_set = get_alias_set (base);
916
917   return true;
918 }
919
920 /* Copy the operations present in load/store/call REF into RESULT, a vector of
921    vn_reference_op_s's.  */
922
923 void
924 copy_reference_ops_from_call (gimple call,
925                               VEC(vn_reference_op_s, heap) **result)
926 {
927   vn_reference_op_s temp;
928   unsigned i;
929
930   /* Copy the type, opcode, function being called and static chain.  */
931   memset (&temp, 0, sizeof (temp));
932   temp.type = gimple_call_return_type (call);
933   temp.opcode = CALL_EXPR;
934   temp.op0 = gimple_call_fn (call);
935   temp.op1 = gimple_call_chain (call);
936   temp.off = -1;
937   VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
938
939   /* Copy the call arguments.  As they can be references as well,
940      just chain them together.  */
941   for (i = 0; i < gimple_call_num_args (call); ++i)
942     {
943       tree callarg = gimple_call_arg (call, i);
944       copy_reference_ops_from_ref (callarg, result);
945     }
946 }
947
948 /* Create a vector of vn_reference_op_s structures from REF, a
949    REFERENCE_CLASS_P tree.  The vector is not shared. */
950
951 static VEC(vn_reference_op_s, heap) *
952 create_reference_ops_from_ref (tree ref)
953 {
954   VEC (vn_reference_op_s, heap) *result = NULL;
955
956   copy_reference_ops_from_ref (ref, &result);
957   return result;
958 }
959
960 /* Create a vector of vn_reference_op_s structures from CALL, a
961    call statement.  The vector is not shared.  */
962
963 static VEC(vn_reference_op_s, heap) *
964 create_reference_ops_from_call (gimple call)
965 {
966   VEC (vn_reference_op_s, heap) *result = NULL;
967
968   copy_reference_ops_from_call (call, &result);
969   return result;
970 }
971
972 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS.  Updates
973    *I_P to point to the last element of the replacement.  */
974 void
975 vn_reference_fold_indirect (VEC (vn_reference_op_s, heap) **ops,
976                             unsigned int *i_p)
977 {
978   unsigned int i = *i_p;
979   vn_reference_op_t op = VEC_index (vn_reference_op_s, *ops, i);
980   vn_reference_op_t mem_op = VEC_index (vn_reference_op_s, *ops, i - 1);
981   tree addr_base;
982   HOST_WIDE_INT addr_offset;
983
984   /* The only thing we have to do is from &OBJ.foo.bar add the offset
985      from .foo.bar to the preceeding MEM_REF offset and replace the
986      address with &OBJ.  */
987   addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
988                                              &addr_offset);
989   gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
990   if (addr_base != op->op0)
991     {
992       double_int off = tree_to_double_int (mem_op->op0);
993       off = double_int_sext (off, TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
994       off = double_int_add (off, shwi_to_double_int (addr_offset));
995       mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
996       op->op0 = build_fold_addr_expr (addr_base);
997       if (host_integerp (mem_op->op0, 0))
998         mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
999       else
1000         mem_op->off = -1;
1001     }
1002 }
1003
1004 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS.  Updates
1005    *I_P to point to the last element of the replacement.  */
1006 static void
1007 vn_reference_maybe_forwprop_address (VEC (vn_reference_op_s, heap) **ops,
1008                                      unsigned int *i_p)
1009 {
1010   unsigned int i = *i_p;
1011   vn_reference_op_t op = VEC_index (vn_reference_op_s, *ops, i);
1012   vn_reference_op_t mem_op = VEC_index (vn_reference_op_s, *ops, i - 1);
1013   gimple def_stmt;
1014   enum tree_code code;
1015   double_int off;
1016
1017   def_stmt = SSA_NAME_DEF_STMT (op->op0);
1018   if (!is_gimple_assign (def_stmt))
1019     return;
1020
1021   code = gimple_assign_rhs_code (def_stmt);
1022   if (code != ADDR_EXPR
1023       && code != POINTER_PLUS_EXPR)
1024     return;
1025
1026   off = tree_to_double_int (mem_op->op0);
1027   off = double_int_sext (off, TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1028
1029   /* The only thing we have to do is from &OBJ.foo.bar add the offset
1030      from .foo.bar to the preceeding MEM_REF offset and replace the
1031      address with &OBJ.  */
1032   if (code == ADDR_EXPR)
1033     {
1034       tree addr, addr_base;
1035       HOST_WIDE_INT addr_offset;
1036
1037       addr = gimple_assign_rhs1 (def_stmt);
1038       addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1039                                                  &addr_offset);
1040       if (!addr_base
1041           || TREE_CODE (addr_base) != MEM_REF)
1042         return;
1043
1044       off = double_int_add (off, shwi_to_double_int (addr_offset));
1045       off = double_int_add (off, mem_ref_offset (addr_base));
1046       op->op0 = TREE_OPERAND (addr_base, 0);
1047     }
1048   else
1049     {
1050       tree ptr, ptroff;
1051       ptr = gimple_assign_rhs1 (def_stmt);
1052       ptroff = gimple_assign_rhs2 (def_stmt);
1053       if (TREE_CODE (ptr) != SSA_NAME
1054           || TREE_CODE (ptroff) != INTEGER_CST)
1055         return;
1056
1057       off = double_int_add (off, tree_to_double_int (ptroff));
1058       op->op0 = ptr;
1059     }
1060
1061   mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1062   if (host_integerp (mem_op->op0, 0))
1063     mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1064   else
1065     mem_op->off = -1;
1066   if (TREE_CODE (op->op0) == SSA_NAME)
1067     op->op0 = SSA_VAL (op->op0);
1068   if (TREE_CODE (op->op0) != SSA_NAME)
1069     op->opcode = TREE_CODE (op->op0);
1070
1071   /* And recurse.  */
1072   if (TREE_CODE (op->op0) == SSA_NAME)
1073     vn_reference_maybe_forwprop_address (ops, i_p);
1074   else if (TREE_CODE (op->op0) == ADDR_EXPR)
1075     vn_reference_fold_indirect (ops, i_p);
1076 }
1077
1078 /* Optimize the reference REF to a constant if possible or return
1079    NULL_TREE if not.  */
1080
1081 tree
1082 fully_constant_vn_reference_p (vn_reference_t ref)
1083 {
1084   VEC (vn_reference_op_s, heap) *operands = ref->operands;
1085   vn_reference_op_t op;
1086
1087   /* Try to simplify the translated expression if it is
1088      a call to a builtin function with at most two arguments.  */
1089   op = VEC_index (vn_reference_op_s, operands, 0);
1090   if (op->opcode == CALL_EXPR
1091       && TREE_CODE (op->op0) == ADDR_EXPR
1092       && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1093       && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1094       && VEC_length (vn_reference_op_s, operands) >= 2
1095       && VEC_length (vn_reference_op_s, operands) <= 3)
1096     {
1097       vn_reference_op_t arg0, arg1 = NULL;
1098       bool anyconst = false;
1099       arg0 = VEC_index (vn_reference_op_s, operands, 1);
1100       if (VEC_length (vn_reference_op_s, operands) > 2)
1101         arg1 = VEC_index (vn_reference_op_s, operands, 2);
1102       if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1103           || (arg0->opcode == ADDR_EXPR
1104               && is_gimple_min_invariant (arg0->op0)))
1105         anyconst = true;
1106       if (arg1
1107           && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1108               || (arg1->opcode == ADDR_EXPR
1109                   && is_gimple_min_invariant (arg1->op0))))
1110         anyconst = true;
1111       if (anyconst)
1112         {
1113           tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1114                                          arg1 ? 2 : 1,
1115                                          arg0->op0,
1116                                          arg1 ? arg1->op0 : NULL);
1117           if (folded
1118               && TREE_CODE (folded) == NOP_EXPR)
1119             folded = TREE_OPERAND (folded, 0);
1120           if (folded
1121               && is_gimple_min_invariant (folded))
1122             return folded;
1123         }
1124     }
1125
1126   /* Simplify reads from constant strings.  */
1127   else if (op->opcode == ARRAY_REF
1128            && TREE_CODE (op->op0) == INTEGER_CST
1129            && integer_zerop (op->op1)
1130            && VEC_length (vn_reference_op_s, operands) == 2)
1131     {
1132       vn_reference_op_t arg0;
1133       arg0 = VEC_index (vn_reference_op_s, operands, 1);
1134       if (arg0->opcode == STRING_CST
1135           && (TYPE_MODE (op->type)
1136               == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1137           && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1138           && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1139           && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1140         return build_int_cst_type (op->type,
1141                                    (TREE_STRING_POINTER (arg0->op0)
1142                                     [TREE_INT_CST_LOW (op->op0)]));
1143     }
1144
1145   return NULL_TREE;
1146 }
1147
1148 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1149    structures into their value numbers.  This is done in-place, and
1150    the vector passed in is returned.  *VALUEIZED_ANYTHING will specify
1151    whether any operands were valueized.  */
1152
1153 static VEC (vn_reference_op_s, heap) *
1154 valueize_refs_1 (VEC (vn_reference_op_s, heap) *orig, bool *valueized_anything)
1155 {
1156   vn_reference_op_t vro;
1157   unsigned int i;
1158
1159   *valueized_anything = false;
1160
1161   FOR_EACH_VEC_ELT (vn_reference_op_s, orig, i, vro)
1162     {
1163       if (vro->opcode == SSA_NAME
1164           || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1165         {
1166           tree tem = SSA_VAL (vro->op0);
1167           if (tem != vro->op0)
1168             {
1169               *valueized_anything = true;
1170               vro->op0 = tem;
1171             }
1172           /* If it transforms from an SSA_NAME to a constant, update
1173              the opcode.  */
1174           if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1175             vro->opcode = TREE_CODE (vro->op0);
1176         }
1177       if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1178         {
1179           tree tem = SSA_VAL (vro->op1);
1180           if (tem != vro->op1)
1181             {
1182               *valueized_anything = true;
1183               vro->op1 = tem;
1184             }
1185         }
1186       if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1187         {
1188           tree tem = SSA_VAL (vro->op2);
1189           if (tem != vro->op2)
1190             {
1191               *valueized_anything = true;
1192               vro->op2 = tem;
1193             }
1194         }
1195       /* If it transforms from an SSA_NAME to an address, fold with
1196          a preceding indirect reference.  */
1197       if (i > 0
1198           && vro->op0
1199           && TREE_CODE (vro->op0) == ADDR_EXPR
1200           && VEC_index (vn_reference_op_s,
1201                         orig, i - 1)->opcode == MEM_REF)
1202         vn_reference_fold_indirect (&orig, &i);
1203       else if (i > 0
1204                && vro->opcode == SSA_NAME
1205                && VEC_index (vn_reference_op_s,
1206                              orig, i - 1)->opcode == MEM_REF)
1207         vn_reference_maybe_forwprop_address (&orig, &i);
1208       /* If it transforms a non-constant ARRAY_REF into a constant
1209          one, adjust the constant offset.  */
1210       else if (vro->opcode == ARRAY_REF
1211                && vro->off == -1
1212                && TREE_CODE (vro->op0) == INTEGER_CST
1213                && TREE_CODE (vro->op1) == INTEGER_CST
1214                && TREE_CODE (vro->op2) == INTEGER_CST)
1215         {
1216           double_int off = tree_to_double_int (vro->op0);
1217           off = double_int_add (off,
1218                                 double_int_neg
1219                                   (tree_to_double_int (vro->op1)));
1220           off = double_int_mul (off, tree_to_double_int (vro->op2));
1221           if (double_int_fits_in_shwi_p (off))
1222             vro->off = off.low;
1223         }
1224     }
1225
1226   return orig;
1227 }
1228
1229 static VEC (vn_reference_op_s, heap) *
1230 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
1231 {
1232   bool tem;
1233   return valueize_refs_1 (orig, &tem);
1234 }
1235
1236 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
1237
1238 /* Create a vector of vn_reference_op_s structures from REF, a
1239    REFERENCE_CLASS_P tree.  The vector is shared among all callers of
1240    this function.  *VALUEIZED_ANYTHING will specify whether any
1241    operands were valueized.  */
1242
1243 static VEC(vn_reference_op_s, heap) *
1244 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1245 {
1246   if (!ref)
1247     return NULL;
1248   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1249   copy_reference_ops_from_ref (ref, &shared_lookup_references);
1250   shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1251                                               valueized_anything);
1252   return shared_lookup_references;
1253 }
1254
1255 /* Create a vector of vn_reference_op_s structures from CALL, a
1256    call statement.  The vector is shared among all callers of
1257    this function.  */
1258
1259 static VEC(vn_reference_op_s, heap) *
1260 valueize_shared_reference_ops_from_call (gimple call)
1261 {
1262   if (!call)
1263     return NULL;
1264   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1265   copy_reference_ops_from_call (call, &shared_lookup_references);
1266   shared_lookup_references = valueize_refs (shared_lookup_references);
1267   return shared_lookup_references;
1268 }
1269
1270 /* Lookup a SCCVN reference operation VR in the current hash table.
1271    Returns the resulting value number if it exists in the hash table,
1272    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
1273    vn_reference_t stored in the hashtable if something is found.  */
1274
1275 static tree
1276 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1277 {
1278   void **slot;
1279   hashval_t hash;
1280
1281   hash = vr->hashcode;
1282   slot = htab_find_slot_with_hash (current_info->references, vr,
1283                                    hash, NO_INSERT);
1284   if (!slot && current_info == optimistic_info)
1285     slot = htab_find_slot_with_hash (valid_info->references, vr,
1286                                      hash, NO_INSERT);
1287   if (slot)
1288     {
1289       if (vnresult)
1290         *vnresult = (vn_reference_t)*slot;
1291       return ((vn_reference_t)*slot)->result;
1292     }
1293
1294   return NULL_TREE;
1295 }
1296
1297 static tree *last_vuse_ptr;
1298 static vn_lookup_kind vn_walk_kind;
1299 static vn_lookup_kind default_vn_walk_kind;
1300
1301 /* Callback for walk_non_aliased_vuses.  Adjusts the vn_reference_t VR_
1302    with the current VUSE and performs the expression lookup.  */
1303
1304 static void *
1305 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, void *vr_)
1306 {
1307   vn_reference_t vr = (vn_reference_t)vr_;
1308   void **slot;
1309   hashval_t hash;
1310
1311   if (last_vuse_ptr)
1312     *last_vuse_ptr = vuse;
1313
1314   /* Fixup vuse and hash.  */
1315   if (vr->vuse)
1316     vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1317   vr->vuse = SSA_VAL (vuse);
1318   if (vr->vuse)
1319     vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1320
1321   hash = vr->hashcode;
1322   slot = htab_find_slot_with_hash (current_info->references, vr,
1323                                    hash, NO_INSERT);
1324   if (!slot && current_info == optimistic_info)
1325     slot = htab_find_slot_with_hash (valid_info->references, vr,
1326                                      hash, NO_INSERT);
1327   if (slot)
1328     return *slot;
1329
1330   return NULL;
1331 }
1332
1333 /* Callback for walk_non_aliased_vuses.  Tries to perform a lookup
1334    from the statement defining VUSE and if not successful tries to
1335    translate *REFP and VR_ through an aggregate copy at the defintion
1336    of VUSE.  */
1337
1338 static void *
1339 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1340 {
1341   vn_reference_t vr = (vn_reference_t)vr_;
1342   gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1343   tree base;
1344   HOST_WIDE_INT offset, maxsize;
1345   static VEC (vn_reference_op_s, heap) *lhs_ops = NULL;
1346   ao_ref lhs_ref;
1347   bool lhs_ref_ok = false;
1348
1349   /* First try to disambiguate after value-replacing in the definitions LHS.  */
1350   if (is_gimple_assign (def_stmt))
1351     {
1352       VEC (vn_reference_op_s, heap) *tem;
1353       tree lhs = gimple_assign_lhs (def_stmt);
1354       bool valueized_anything = false;
1355       /* Avoid re-allocation overhead.  */
1356       VEC_truncate (vn_reference_op_s, lhs_ops, 0);
1357       copy_reference_ops_from_ref (lhs, &lhs_ops);
1358       tem = lhs_ops;
1359       lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1360       gcc_assert (lhs_ops == tem);
1361       if (valueized_anything)
1362         {
1363           lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1364                                                       get_alias_set (lhs),
1365                                                       TREE_TYPE (lhs), lhs_ops);
1366           if (lhs_ref_ok
1367               && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1368             return NULL;
1369         }
1370       else
1371         {
1372           ao_ref_init (&lhs_ref, lhs);
1373           lhs_ref_ok = true;
1374         }
1375     }
1376
1377   base = ao_ref_base (ref);
1378   offset = ref->offset;
1379   maxsize = ref->max_size;
1380
1381   /* If we cannot constrain the size of the reference we cannot
1382      test if anything kills it.  */
1383   if (maxsize == -1)
1384     return (void *)-1;
1385
1386   /* def_stmt may-defs *ref.  See if we can derive a value for *ref
1387      from that defintion.
1388      1) Memset.  */
1389   if (is_gimple_reg_type (vr->type)
1390       && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1391       && integer_zerop (gimple_call_arg (def_stmt, 1))
1392       && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1393       && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1394     {
1395       tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1396       tree base2;
1397       HOST_WIDE_INT offset2, size2, maxsize2;
1398       base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1399       size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1400       if ((unsigned HOST_WIDE_INT)size2 / 8
1401           == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1402           && maxsize2 != -1
1403           && operand_equal_p (base, base2, 0)
1404           && offset2 <= offset
1405           && offset2 + size2 >= offset + maxsize)
1406         {
1407           tree val = build_zero_cst (vr->type);
1408           unsigned int value_id = get_or_alloc_constant_value_id (val);
1409           return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1410                                              VEC_copy (vn_reference_op_s,
1411                                                        heap, vr->operands),
1412                                              val, value_id);
1413         }
1414     }
1415
1416   /* 2) Assignment from an empty CONSTRUCTOR.  */
1417   else if (is_gimple_reg_type (vr->type)
1418            && gimple_assign_single_p (def_stmt)
1419            && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1420            && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1421     {
1422       tree base2;
1423       HOST_WIDE_INT offset2, size2, maxsize2;
1424       base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1425                                        &offset2, &size2, &maxsize2);
1426       if (maxsize2 != -1
1427           && operand_equal_p (base, base2, 0)
1428           && offset2 <= offset
1429           && offset2 + size2 >= offset + maxsize)
1430         {
1431           tree val = build_zero_cst (vr->type);
1432           unsigned int value_id = get_or_alloc_constant_value_id (val);
1433           return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1434                                              VEC_copy (vn_reference_op_s,
1435                                                        heap, vr->operands),
1436                                              val, value_id);
1437         }
1438     }
1439
1440   /* 3) For aggregate copies translate the reference through them if
1441      the copy kills ref.  */
1442   else if (vn_walk_kind == VN_WALKREWRITE
1443            && gimple_assign_single_p (def_stmt)
1444            && (DECL_P (gimple_assign_rhs1 (def_stmt))
1445                || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1446                || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1447     {
1448       tree base2;
1449       HOST_WIDE_INT offset2, size2, maxsize2;
1450       int i, j;
1451       VEC (vn_reference_op_s, heap) *rhs = NULL;
1452       vn_reference_op_t vro;
1453       ao_ref r;
1454
1455       if (!lhs_ref_ok)
1456         return (void *)-1;
1457
1458       /* See if the assignment kills REF.  */
1459       base2 = ao_ref_base (&lhs_ref);
1460       offset2 = lhs_ref.offset;
1461       size2 = lhs_ref.size;
1462       maxsize2 = lhs_ref.max_size;
1463       if (maxsize2 == -1
1464           || (base != base2 && !operand_equal_p (base, base2, 0))
1465           || offset2 > offset
1466           || offset2 + size2 < offset + maxsize)
1467         return (void *)-1;
1468
1469       /* Find the common base of ref and the lhs.  lhs_ops already
1470          contains valueized operands for the lhs.  */
1471       i = VEC_length (vn_reference_op_s, vr->operands) - 1;
1472       j = VEC_length (vn_reference_op_s, lhs_ops) - 1;
1473       while (j >= 0 && i >= 0
1474              && vn_reference_op_eq (VEC_index (vn_reference_op_s,
1475                                                vr->operands, i),
1476                                     VEC_index (vn_reference_op_s, lhs_ops, j)))
1477         {
1478           i--;
1479           j--;
1480         }
1481
1482       /* ???  The innermost op should always be a MEM_REF and we already
1483          checked that the assignment to the lhs kills vr.  Thus for
1484          aggregate copies using char[] types the vn_reference_op_eq
1485          may fail when comparing types for compatibility.  But we really
1486          don't care here - further lookups with the rewritten operands
1487          will simply fail if we messed up types too badly.  */
1488       if (j == 0 && i == 0
1489           && VEC_index (vn_reference_op_s, lhs_ops, 0)->opcode == MEM_REF
1490           && VEC_index (vn_reference_op_s, vr->operands, i)->opcode == MEM_REF
1491           && tree_int_cst_equal
1492                (VEC_index (vn_reference_op_s, lhs_ops, 0)->op0,
1493                 VEC_index (vn_reference_op_s, vr->operands, i)->op0))
1494         i--, j--;
1495
1496       /* i now points to the first additional op.
1497          ???  LHS may not be completely contained in VR, one or more
1498          VIEW_CONVERT_EXPRs could be in its way.  We could at least
1499          try handling outermost VIEW_CONVERT_EXPRs.  */
1500       if (j != -1)
1501         return (void *)-1;
1502
1503       /* Now re-write REF to be based on the rhs of the assignment.  */
1504       copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1505       /* We need to pre-pend vr->operands[0..i] to rhs.  */
1506       if (i + 1 + VEC_length (vn_reference_op_s, rhs)
1507           > VEC_length (vn_reference_op_s, vr->operands))
1508         {
1509           VEC (vn_reference_op_s, heap) *old = vr->operands;
1510           VEC_safe_grow (vn_reference_op_s, heap, vr->operands,
1511                          i + 1 + VEC_length (vn_reference_op_s, rhs));
1512           if (old == shared_lookup_references
1513               && vr->operands != old)
1514             shared_lookup_references = NULL;
1515         }
1516       else
1517         VEC_truncate (vn_reference_op_s, vr->operands,
1518                       i + 1 + VEC_length (vn_reference_op_s, rhs));
1519       FOR_EACH_VEC_ELT (vn_reference_op_s, rhs, j, vro)
1520         VEC_replace (vn_reference_op_s, vr->operands, i + 1 + j, vro);
1521       VEC_free (vn_reference_op_s, heap, rhs);
1522       vr->hashcode = vn_reference_compute_hash (vr);
1523
1524       /* Adjust *ref from the new operands.  */
1525       if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1526         return (void *)-1;
1527       /* This can happen with bitfields.  */
1528       if (ref->size != r.size)
1529         return (void *)-1;
1530       *ref = r;
1531
1532       /* Do not update last seen VUSE after translating.  */
1533       last_vuse_ptr = NULL;
1534
1535       /* Keep looking for the adjusted *REF / VR pair.  */
1536       return NULL;
1537     }
1538
1539   /* 4) For memcpy copies translate the reference through them if
1540      the copy kills ref.  */
1541   else if (vn_walk_kind == VN_WALKREWRITE
1542            && is_gimple_reg_type (vr->type)
1543            /* ???  Handle BCOPY as well.  */
1544            && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1545                || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1546                || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1547            && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1548                || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1549            && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1550                || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1551            && host_integerp (gimple_call_arg (def_stmt, 2), 1))
1552     {
1553       tree lhs, rhs;
1554       ao_ref r;
1555       HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1556       vn_reference_op_s op;
1557       HOST_WIDE_INT at;
1558
1559
1560       /* Only handle non-variable, addressable refs.  */
1561       if (ref->size != maxsize
1562           || offset % BITS_PER_UNIT != 0
1563           || ref->size % BITS_PER_UNIT != 0)
1564         return (void *)-1;
1565
1566       /* Extract a pointer base and an offset for the destination.  */
1567       lhs = gimple_call_arg (def_stmt, 0);
1568       lhs_offset = 0;
1569       if (TREE_CODE (lhs) == SSA_NAME)
1570         lhs = SSA_VAL (lhs);
1571       if (TREE_CODE (lhs) == ADDR_EXPR)
1572         {
1573           tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1574                                                     &lhs_offset);
1575           if (!tem)
1576             return (void *)-1;
1577           if (TREE_CODE (tem) == MEM_REF
1578               && host_integerp (TREE_OPERAND (tem, 1), 1))
1579             {
1580               lhs = TREE_OPERAND (tem, 0);
1581               lhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1582             }
1583           else if (DECL_P (tem))
1584             lhs = build_fold_addr_expr (tem);
1585           else
1586             return (void *)-1;
1587         }
1588       if (TREE_CODE (lhs) != SSA_NAME
1589           && TREE_CODE (lhs) != ADDR_EXPR)
1590         return (void *)-1;
1591
1592       /* Extract a pointer base and an offset for the source.  */
1593       rhs = gimple_call_arg (def_stmt, 1);
1594       rhs_offset = 0;
1595       if (TREE_CODE (rhs) == SSA_NAME)
1596         rhs = SSA_VAL (rhs);
1597       if (TREE_CODE (rhs) == ADDR_EXPR)
1598         {
1599           tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1600                                                     &rhs_offset);
1601           if (!tem)
1602             return (void *)-1;
1603           if (TREE_CODE (tem) == MEM_REF
1604               && host_integerp (TREE_OPERAND (tem, 1), 1))
1605             {
1606               rhs = TREE_OPERAND (tem, 0);
1607               rhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1608             }
1609           else if (DECL_P (tem))
1610             rhs = build_fold_addr_expr (tem);
1611           else
1612             return (void *)-1;
1613         }
1614       if (TREE_CODE (rhs) != SSA_NAME
1615           && TREE_CODE (rhs) != ADDR_EXPR)
1616         return (void *)-1;
1617
1618       copy_size = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2));
1619
1620       /* The bases of the destination and the references have to agree.  */
1621       if ((TREE_CODE (base) != MEM_REF
1622            && !DECL_P (base))
1623           || (TREE_CODE (base) == MEM_REF
1624               && (TREE_OPERAND (base, 0) != lhs
1625                   || !host_integerp (TREE_OPERAND (base, 1), 1)))
1626           || (DECL_P (base)
1627               && (TREE_CODE (lhs) != ADDR_EXPR
1628                   || TREE_OPERAND (lhs, 0) != base)))
1629         return (void *)-1;
1630
1631       /* And the access has to be contained within the memcpy destination.  */
1632       at = offset / BITS_PER_UNIT;
1633       if (TREE_CODE (base) == MEM_REF)
1634         at += TREE_INT_CST_LOW (TREE_OPERAND (base, 1));
1635       if (lhs_offset > at
1636           || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1637         return (void *)-1;
1638
1639       /* Make room for 2 operands in the new reference.  */
1640       if (VEC_length (vn_reference_op_s, vr->operands) < 2)
1641         {
1642           VEC (vn_reference_op_s, heap) *old = vr->operands;
1643           VEC_safe_grow (vn_reference_op_s, heap, vr->operands, 2);
1644           if (old == shared_lookup_references
1645               && vr->operands != old)
1646             shared_lookup_references = NULL;
1647         }
1648       else
1649         VEC_truncate (vn_reference_op_s, vr->operands, 2);
1650
1651       /* The looked-through reference is a simple MEM_REF.  */
1652       memset (&op, 0, sizeof (op));
1653       op.type = vr->type;
1654       op.opcode = MEM_REF;
1655       op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
1656       op.off = at - lhs_offset + rhs_offset;
1657       VEC_replace (vn_reference_op_s, vr->operands, 0, &op);
1658       op.type = TREE_TYPE (rhs);
1659       op.opcode = TREE_CODE (rhs);
1660       op.op0 = rhs;
1661       op.off = -1;
1662       VEC_replace (vn_reference_op_s, vr->operands, 1, &op);
1663       vr->hashcode = vn_reference_compute_hash (vr);
1664
1665       /* Adjust *ref from the new operands.  */
1666       if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1667         return (void *)-1;
1668       /* This can happen with bitfields.  */
1669       if (ref->size != r.size)
1670         return (void *)-1;
1671       *ref = r;
1672
1673       /* Do not update last seen VUSE after translating.  */
1674       last_vuse_ptr = NULL;
1675
1676       /* Keep looking for the adjusted *REF / VR pair.  */
1677       return NULL;
1678     }
1679
1680   /* Bail out and stop walking.  */
1681   return (void *)-1;
1682 }
1683
1684 /* Lookup a reference operation by it's parts, in the current hash table.
1685    Returns the resulting value number if it exists in the hash table,
1686    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
1687    vn_reference_t stored in the hashtable if something is found.  */
1688
1689 tree
1690 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1691                             VEC (vn_reference_op_s, heap) *operands,
1692                             vn_reference_t *vnresult, vn_lookup_kind kind)
1693 {
1694   struct vn_reference_s vr1;
1695   vn_reference_t tmp;
1696   tree cst;
1697
1698   if (!vnresult)
1699     vnresult = &tmp;
1700   *vnresult = NULL;
1701
1702   vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1703   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1704   VEC_safe_grow (vn_reference_op_s, heap, shared_lookup_references,
1705                  VEC_length (vn_reference_op_s, operands));
1706   memcpy (VEC_address (vn_reference_op_s, shared_lookup_references),
1707           VEC_address (vn_reference_op_s, operands),
1708           sizeof (vn_reference_op_s)
1709           * VEC_length (vn_reference_op_s, operands));
1710   vr1.operands = operands = shared_lookup_references
1711     = valueize_refs (shared_lookup_references);
1712   vr1.type = type;
1713   vr1.set = set;
1714   vr1.hashcode = vn_reference_compute_hash (&vr1);
1715   if ((cst = fully_constant_vn_reference_p (&vr1)))
1716     return cst;
1717
1718   vn_reference_lookup_1 (&vr1, vnresult);
1719   if (!*vnresult
1720       && kind != VN_NOWALK
1721       && vr1.vuse)
1722     {
1723       ao_ref r;
1724       vn_walk_kind = kind;
1725       if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
1726         *vnresult =
1727           (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1728                                                   vn_reference_lookup_2,
1729                                                   vn_reference_lookup_3, &vr1);
1730       if (vr1.operands != operands)
1731         VEC_free (vn_reference_op_s, heap, vr1.operands);
1732     }
1733
1734   if (*vnresult)
1735      return (*vnresult)->result;
1736
1737   return NULL_TREE;
1738 }
1739
1740 /* Lookup OP in the current hash table, and return the resulting value
1741    number if it exists in the hash table.  Return NULL_TREE if it does
1742    not exist in the hash table or if the result field of the structure
1743    was NULL..  VNRESULT will be filled in with the vn_reference_t
1744    stored in the hashtable if one exists.  */
1745
1746 tree
1747 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
1748                      vn_reference_t *vnresult)
1749 {
1750   VEC (vn_reference_op_s, heap) *operands;
1751   struct vn_reference_s vr1;
1752   tree cst;
1753   bool valuezied_anything;
1754
1755   if (vnresult)
1756     *vnresult = NULL;
1757
1758   vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1759   vr1.operands = operands
1760     = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
1761   vr1.type = TREE_TYPE (op);
1762   vr1.set = get_alias_set (op);
1763   vr1.hashcode = vn_reference_compute_hash (&vr1);
1764   if ((cst = fully_constant_vn_reference_p (&vr1)))
1765     return cst;
1766
1767   if (kind != VN_NOWALK
1768       && vr1.vuse)
1769     {
1770       vn_reference_t wvnresult;
1771       ao_ref r;
1772       /* Make sure to use a valueized reference if we valueized anything.
1773          Otherwise preserve the full reference for advanced TBAA.  */
1774       if (!valuezied_anything
1775           || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
1776                                              vr1.operands))
1777         ao_ref_init (&r, op);
1778       vn_walk_kind = kind;
1779       wvnresult =
1780         (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1781                                                 vn_reference_lookup_2,
1782                                                 vn_reference_lookup_3, &vr1);
1783       if (vr1.operands != operands)
1784         VEC_free (vn_reference_op_s, heap, vr1.operands);
1785       if (wvnresult)
1786         {
1787           if (vnresult)
1788             *vnresult = wvnresult;
1789           return wvnresult->result;
1790         }
1791
1792       return NULL_TREE;
1793     }
1794
1795   return vn_reference_lookup_1 (&vr1, vnresult);
1796 }
1797
1798
1799 /* Insert OP into the current hash table with a value number of
1800    RESULT, and return the resulting reference structure we created.  */
1801
1802 vn_reference_t
1803 vn_reference_insert (tree op, tree result, tree vuse)
1804 {
1805   void **slot;
1806   vn_reference_t vr1;
1807
1808   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1809   if (TREE_CODE (result) == SSA_NAME)
1810     vr1->value_id = VN_INFO (result)->value_id;
1811   else
1812     vr1->value_id = get_or_alloc_constant_value_id (result);
1813   vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1814   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
1815   vr1->type = TREE_TYPE (op);
1816   vr1->set = get_alias_set (op);
1817   vr1->hashcode = vn_reference_compute_hash (vr1);
1818   vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
1819
1820   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1821                                    INSERT);
1822
1823   /* Because we lookup stores using vuses, and value number failures
1824      using the vdefs (see visit_reference_op_store for how and why),
1825      it's possible that on failure we may try to insert an already
1826      inserted store.  This is not wrong, there is no ssa name for a
1827      store that we could use as a differentiator anyway.  Thus, unlike
1828      the other lookup functions, you cannot gcc_assert (!*slot)
1829      here.  */
1830
1831   /* But free the old slot in case of a collision.  */
1832   if (*slot)
1833     free_reference (*slot);
1834
1835   *slot = vr1;
1836   return vr1;
1837 }
1838
1839 /* Insert a reference by it's pieces into the current hash table with
1840    a value number of RESULT.  Return the resulting reference
1841    structure we created.  */
1842
1843 vn_reference_t
1844 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
1845                             VEC (vn_reference_op_s, heap) *operands,
1846                             tree result, unsigned int value_id)
1847
1848 {
1849   void **slot;
1850   vn_reference_t vr1;
1851
1852   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1853   vr1->value_id = value_id;
1854   vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1855   vr1->operands = valueize_refs (operands);
1856   vr1->type = type;
1857   vr1->set = set;
1858   vr1->hashcode = vn_reference_compute_hash (vr1);
1859   if (result && TREE_CODE (result) == SSA_NAME)
1860     result = SSA_VAL (result);
1861   vr1->result = result;
1862
1863   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1864                                    INSERT);
1865
1866   /* At this point we should have all the things inserted that we have
1867      seen before, and we should never try inserting something that
1868      already exists.  */
1869   gcc_assert (!*slot);
1870   if (*slot)
1871     free_reference (*slot);
1872
1873   *slot = vr1;
1874   return vr1;
1875 }
1876
1877 /* Compute and return the hash value for nary operation VBO1.  */
1878
1879 hashval_t
1880 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
1881 {
1882   hashval_t hash;
1883   unsigned i;
1884
1885   for (i = 0; i < vno1->length; ++i)
1886     if (TREE_CODE (vno1->op[i]) == SSA_NAME)
1887       vno1->op[i] = SSA_VAL (vno1->op[i]);
1888
1889   if (vno1->length == 2
1890       && commutative_tree_code (vno1->opcode)
1891       && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
1892     {
1893       tree temp = vno1->op[0];
1894       vno1->op[0] = vno1->op[1];
1895       vno1->op[1] = temp;
1896     }
1897
1898   hash = iterative_hash_hashval_t (vno1->opcode, 0);
1899   for (i = 0; i < vno1->length; ++i)
1900     hash = iterative_hash_expr (vno1->op[i], hash);
1901
1902   return hash;
1903 }
1904
1905 /* Return the computed hashcode for nary operation P1.  */
1906
1907 static hashval_t
1908 vn_nary_op_hash (const void *p1)
1909 {
1910   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1911   return vno1->hashcode;
1912 }
1913
1914 /* Compare nary operations P1 and P2 and return true if they are
1915    equivalent.  */
1916
1917 int
1918 vn_nary_op_eq (const void *p1, const void *p2)
1919 {
1920   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1921   const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
1922   unsigned i;
1923
1924   if (vno1->hashcode != vno2->hashcode)
1925     return false;
1926
1927   if (vno1->opcode != vno2->opcode
1928       || !types_compatible_p (vno1->type, vno2->type))
1929     return false;
1930
1931   for (i = 0; i < vno1->length; ++i)
1932     if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
1933       return false;
1934
1935   return true;
1936 }
1937
1938 /* Initialize VNO from the pieces provided.  */
1939
1940 static void
1941 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
1942                              enum tree_code code, tree type, tree op0,
1943                              tree op1, tree op2, tree op3)
1944 {
1945   vno->opcode = code;
1946   vno->length = length;
1947   vno->type = type;
1948   switch (length)
1949     {
1950       /* The fallthrus here are deliberate.  */
1951     case 4: vno->op[3] = op3;
1952     case 3: vno->op[2] = op2;
1953     case 2: vno->op[1] = op1;
1954     case 1: vno->op[0] = op0;
1955     default:
1956       break;
1957     }
1958 }
1959
1960 /* Initialize VNO from OP.  */
1961
1962 static void
1963 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
1964 {
1965   unsigned i;
1966
1967   vno->opcode = TREE_CODE (op);
1968   vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
1969   vno->type = TREE_TYPE (op);
1970   for (i = 0; i < vno->length; ++i)
1971     vno->op[i] = TREE_OPERAND (op, i);
1972 }
1973
1974 /* Initialize VNO from STMT.  */
1975
1976 static void
1977 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
1978 {
1979   unsigned i;
1980
1981   vno->opcode = gimple_assign_rhs_code (stmt);
1982   vno->length = gimple_num_ops (stmt) - 1;
1983   vno->type = gimple_expr_type (stmt);
1984   for (i = 0; i < vno->length; ++i)
1985     vno->op[i] = gimple_op (stmt, i + 1);
1986   if (vno->opcode == REALPART_EXPR
1987       || vno->opcode == IMAGPART_EXPR
1988       || vno->opcode == VIEW_CONVERT_EXPR)
1989     vno->op[0] = TREE_OPERAND (vno->op[0], 0);
1990 }
1991
1992 /* Compute the hashcode for VNO and look for it in the hash table;
1993    return the resulting value number if it exists in the hash table.
1994    Return NULL_TREE if it does not exist in the hash table or if the
1995    result field of the operation is NULL.  VNRESULT will contain the
1996    vn_nary_op_t from the hashtable if it exists.  */
1997
1998 static tree
1999 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2000 {
2001   void **slot;
2002
2003   if (vnresult)
2004     *vnresult = NULL;
2005
2006   vno->hashcode = vn_nary_op_compute_hash (vno);
2007   slot = htab_find_slot_with_hash (current_info->nary, vno, vno->hashcode,
2008                                    NO_INSERT);
2009   if (!slot && current_info == optimistic_info)
2010     slot = htab_find_slot_with_hash (valid_info->nary, vno, vno->hashcode,
2011                                      NO_INSERT);
2012   if (!slot)
2013     return NULL_TREE;
2014   if (vnresult)
2015     *vnresult = (vn_nary_op_t)*slot;
2016   return ((vn_nary_op_t)*slot)->result;
2017 }
2018
2019 /* Lookup a n-ary operation by its pieces and return the resulting value
2020    number if it exists in the hash table.  Return NULL_TREE if it does
2021    not exist in the hash table or if the result field of the operation
2022    is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2023    if it exists.  */
2024
2025 tree
2026 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2027                           tree type, tree op0, tree op1, tree op2,
2028                           tree op3, vn_nary_op_t *vnresult)
2029 {
2030   struct vn_nary_op_s vno1;
2031   init_vn_nary_op_from_pieces (&vno1, length, code, type, op0, op1, op2, op3);
2032   return vn_nary_op_lookup_1 (&vno1, vnresult);
2033 }
2034
2035 /* Lookup OP in the current hash table, and return the resulting value
2036    number if it exists in the hash table.  Return NULL_TREE if it does
2037    not exist in the hash table or if the result field of the operation
2038    is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2039    if it exists.  */
2040
2041 tree
2042 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2043 {
2044   struct vn_nary_op_s vno1;
2045   init_vn_nary_op_from_op (&vno1, op);
2046   return vn_nary_op_lookup_1 (&vno1, vnresult);
2047 }
2048
2049 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2050    value number if it exists in the hash table.  Return NULL_TREE if
2051    it does not exist in the hash table.  VNRESULT will contain the
2052    vn_nary_op_t from the hashtable if it exists.  */
2053
2054 tree
2055 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2056 {
2057   struct vn_nary_op_s vno1;
2058   init_vn_nary_op_from_stmt (&vno1, stmt);
2059   return vn_nary_op_lookup_1 (&vno1, vnresult);
2060 }
2061
2062 /* Return the size of a vn_nary_op_t with LENGTH operands.  */
2063
2064 static size_t
2065 sizeof_vn_nary_op (unsigned int length)
2066 {
2067   return sizeof (struct vn_nary_op_s) - sizeof (tree) * (4 - length);
2068 }
2069
2070 /* Allocate a vn_nary_op_t with LENGTH operands on STACK.  */
2071
2072 static vn_nary_op_t
2073 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2074 {
2075   return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2076 }
2077
2078 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2079    obstack.  */
2080
2081 static vn_nary_op_t
2082 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2083 {
2084   vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2085                                                &current_info->nary_obstack);
2086
2087   vno1->value_id = value_id;
2088   vno1->length = length;
2089   vno1->result = result;
2090
2091   return vno1;
2092 }
2093
2094 /* Insert VNO into TABLE.  If COMPUTE_HASH is true, then compute
2095    VNO->HASHCODE first.  */
2096
2097 static vn_nary_op_t
2098 vn_nary_op_insert_into (vn_nary_op_t vno, htab_t table, bool compute_hash)
2099 {
2100   void **slot;
2101
2102   if (compute_hash)
2103     vno->hashcode = vn_nary_op_compute_hash (vno);
2104
2105   slot = htab_find_slot_with_hash (table, vno, vno->hashcode, INSERT);
2106   gcc_assert (!*slot);
2107
2108   *slot = vno;
2109   return vno;
2110 }
2111
2112 /* Insert a n-ary operation into the current hash table using it's
2113    pieces.  Return the vn_nary_op_t structure we created and put in
2114    the hashtable.  */
2115
2116 vn_nary_op_t
2117 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2118                           tree type, tree op0,
2119                           tree op1, tree op2, tree op3,
2120                           tree result,
2121                           unsigned int value_id)
2122 {
2123   vn_nary_op_t vno1;
2124
2125   vno1 = alloc_vn_nary_op (length, result, value_id);
2126   init_vn_nary_op_from_pieces (vno1, length, code, type, op0, op1, op2, op3);
2127   return vn_nary_op_insert_into (vno1, current_info->nary, true);
2128 }
2129
2130 /* Insert OP into the current hash table with a value number of
2131    RESULT.  Return the vn_nary_op_t structure we created and put in
2132    the hashtable.  */
2133
2134 vn_nary_op_t
2135 vn_nary_op_insert (tree op, tree result)
2136 {
2137   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2138   vn_nary_op_t vno1;
2139
2140   vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2141   init_vn_nary_op_from_op (vno1, op);
2142   return vn_nary_op_insert_into (vno1, current_info->nary, true);
2143 }
2144
2145 /* Insert the rhs of STMT into the current hash table with a value number of
2146    RESULT.  */
2147
2148 vn_nary_op_t
2149 vn_nary_op_insert_stmt (gimple stmt, tree result)
2150 {
2151   unsigned length = gimple_num_ops (stmt) - 1;
2152   vn_nary_op_t vno1;
2153
2154   vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2155   init_vn_nary_op_from_stmt (vno1, stmt);
2156   return vn_nary_op_insert_into (vno1, current_info->nary, true);
2157 }
2158
2159 /* Compute a hashcode for PHI operation VP1 and return it.  */
2160
2161 static inline hashval_t
2162 vn_phi_compute_hash (vn_phi_t vp1)
2163 {
2164   hashval_t result;
2165   int i;
2166   tree phi1op;
2167   tree type;
2168
2169   result = vp1->block->index;
2170
2171   /* If all PHI arguments are constants we need to distinguish
2172      the PHI node via its type.  */
2173   type = TREE_TYPE (VEC_index (tree, vp1->phiargs, 0));
2174   result += (INTEGRAL_TYPE_P (type)
2175              + (INTEGRAL_TYPE_P (type)
2176                 ? TYPE_PRECISION (type) + TYPE_UNSIGNED (type) : 0));
2177
2178   FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
2179     {
2180       if (phi1op == VN_TOP)
2181         continue;
2182       result = iterative_hash_expr (phi1op, result);
2183     }
2184
2185   return result;
2186 }
2187
2188 /* Return the computed hashcode for phi operation P1.  */
2189
2190 static hashval_t
2191 vn_phi_hash (const void *p1)
2192 {
2193   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
2194   return vp1->hashcode;
2195 }
2196
2197 /* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
2198
2199 static int
2200 vn_phi_eq (const void *p1, const void *p2)
2201 {
2202   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
2203   const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
2204
2205   if (vp1->hashcode != vp2->hashcode)
2206     return false;
2207
2208   if (vp1->block == vp2->block)
2209     {
2210       int i;
2211       tree phi1op;
2212
2213       /* If the PHI nodes do not have compatible types
2214          they are not the same.  */
2215       if (!types_compatible_p (TREE_TYPE (VEC_index (tree, vp1->phiargs, 0)),
2216                                TREE_TYPE (VEC_index (tree, vp2->phiargs, 0))))
2217         return false;
2218
2219       /* Any phi in the same block will have it's arguments in the
2220          same edge order, because of how we store phi nodes.  */
2221       FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
2222         {
2223           tree phi2op = VEC_index (tree, vp2->phiargs, i);
2224           if (phi1op == VN_TOP || phi2op == VN_TOP)
2225             continue;
2226           if (!expressions_equal_p (phi1op, phi2op))
2227             return false;
2228         }
2229       return true;
2230     }
2231   return false;
2232 }
2233
2234 static VEC(tree, heap) *shared_lookup_phiargs;
2235
2236 /* Lookup PHI in the current hash table, and return the resulting
2237    value number if it exists in the hash table.  Return NULL_TREE if
2238    it does not exist in the hash table. */
2239
2240 static tree
2241 vn_phi_lookup (gimple phi)
2242 {
2243   void **slot;
2244   struct vn_phi_s vp1;
2245   unsigned i;
2246
2247   VEC_truncate (tree, shared_lookup_phiargs, 0);
2248
2249   /* Canonicalize the SSA_NAME's to their value number.  */
2250   for (i = 0; i < gimple_phi_num_args (phi); i++)
2251     {
2252       tree def = PHI_ARG_DEF (phi, i);
2253       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2254       VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
2255     }
2256   vp1.phiargs = shared_lookup_phiargs;
2257   vp1.block = gimple_bb (phi);
2258   vp1.hashcode = vn_phi_compute_hash (&vp1);
2259   slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
2260                                    NO_INSERT);
2261   if (!slot && current_info == optimistic_info)
2262     slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
2263                                      NO_INSERT);
2264   if (!slot)
2265     return NULL_TREE;
2266   return ((vn_phi_t)*slot)->result;
2267 }
2268
2269 /* Insert PHI into the current hash table with a value number of
2270    RESULT.  */
2271
2272 static vn_phi_t
2273 vn_phi_insert (gimple phi, tree result)
2274 {
2275   void **slot;
2276   vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2277   unsigned i;
2278   VEC (tree, heap) *args = NULL;
2279
2280   /* Canonicalize the SSA_NAME's to their value number.  */
2281   for (i = 0; i < gimple_phi_num_args (phi); i++)
2282     {
2283       tree def = PHI_ARG_DEF (phi, i);
2284       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2285       VEC_safe_push (tree, heap, args, def);
2286     }
2287   vp1->value_id = VN_INFO (result)->value_id;
2288   vp1->phiargs = args;
2289   vp1->block = gimple_bb (phi);
2290   vp1->result = result;
2291   vp1->hashcode = vn_phi_compute_hash (vp1);
2292
2293   slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
2294                                    INSERT);
2295
2296   /* Because we iterate over phi operations more than once, it's
2297      possible the slot might already exist here, hence no assert.*/
2298   *slot = vp1;
2299   return vp1;
2300 }
2301
2302
2303 /* Print set of components in strongly connected component SCC to OUT. */
2304
2305 static void
2306 print_scc (FILE *out, VEC (tree, heap) *scc)
2307 {
2308   tree var;
2309   unsigned int i;
2310
2311   fprintf (out, "SCC consists of: ");
2312   FOR_EACH_VEC_ELT (tree, scc, i, var)
2313     {
2314       print_generic_expr (out, var, 0);
2315       fprintf (out, " ");
2316     }
2317   fprintf (out, "\n");
2318 }
2319
2320 /* Set the value number of FROM to TO, return true if it has changed
2321    as a result.  */
2322
2323 static inline bool
2324 set_ssa_val_to (tree from, tree to)
2325 {
2326   tree currval = SSA_VAL (from);
2327
2328   if (from != to)
2329     {
2330       if (currval == from)
2331         {
2332           if (dump_file && (dump_flags & TDF_DETAILS))
2333             {
2334               fprintf (dump_file, "Not changing value number of ");
2335               print_generic_expr (dump_file, from, 0);
2336               fprintf (dump_file, " from VARYING to ");
2337               print_generic_expr (dump_file, to, 0);
2338               fprintf (dump_file, "\n");
2339             }
2340           return false;
2341         }
2342       else if (TREE_CODE (to) == SSA_NAME
2343                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2344         to = from;
2345     }
2346
2347   /* The only thing we allow as value numbers are VN_TOP, ssa_names
2348      and invariants.  So assert that here.  */
2349   gcc_assert (to != NULL_TREE
2350               && (to == VN_TOP
2351                   || TREE_CODE (to) == SSA_NAME
2352                   || is_gimple_min_invariant (to)));
2353
2354   if (dump_file && (dump_flags & TDF_DETAILS))
2355     {
2356       fprintf (dump_file, "Setting value number of ");
2357       print_generic_expr (dump_file, from, 0);
2358       fprintf (dump_file, " to ");
2359       print_generic_expr (dump_file, to, 0);
2360     }
2361
2362   if (currval != to  && !operand_equal_p (currval, to, OEP_PURE_SAME))
2363     {
2364       VN_INFO (from)->valnum = to;
2365       if (dump_file && (dump_flags & TDF_DETAILS))
2366         fprintf (dump_file, " (changed)\n");
2367       return true;
2368     }
2369   if (dump_file && (dump_flags & TDF_DETAILS))
2370     fprintf (dump_file, "\n");
2371   return false;
2372 }
2373
2374 /* Set all definitions in STMT to value number to themselves.
2375    Return true if a value number changed. */
2376
2377 static bool
2378 defs_to_varying (gimple stmt)
2379 {
2380   bool changed = false;
2381   ssa_op_iter iter;
2382   def_operand_p defp;
2383
2384   FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2385     {
2386       tree def = DEF_FROM_PTR (defp);
2387
2388       VN_INFO (def)->use_processed = true;
2389       changed |= set_ssa_val_to (def, def);
2390     }
2391   return changed;
2392 }
2393
2394 static bool expr_has_constants (tree expr);
2395 static tree valueize_expr (tree expr);
2396
2397 /* Visit a copy between LHS and RHS, return true if the value number
2398    changed.  */
2399
2400 static bool
2401 visit_copy (tree lhs, tree rhs)
2402 {
2403   /* Follow chains of copies to their destination.  */
2404   while (TREE_CODE (rhs) == SSA_NAME
2405          && SSA_VAL (rhs) != rhs)
2406     rhs = SSA_VAL (rhs);
2407
2408   /* The copy may have a more interesting constant filled expression
2409      (we don't, since we know our RHS is just an SSA name).  */
2410   if (TREE_CODE (rhs) == SSA_NAME)
2411     {
2412       VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2413       VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2414     }
2415
2416   return set_ssa_val_to (lhs, rhs);
2417 }
2418
2419 /* Visit a nary operator RHS, value number it, and return true if the
2420    value number of LHS has changed as a result.  */
2421
2422 static bool
2423 visit_nary_op (tree lhs, gimple stmt)
2424 {
2425   bool changed = false;
2426   tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2427
2428   if (result)
2429     changed = set_ssa_val_to (lhs, result);
2430   else
2431     {
2432       changed = set_ssa_val_to (lhs, lhs);
2433       vn_nary_op_insert_stmt (stmt, lhs);
2434     }
2435
2436   return changed;
2437 }
2438
2439 /* Visit a call STMT storing into LHS.  Return true if the value number
2440    of the LHS has changed as a result.  */
2441
2442 static bool
2443 visit_reference_op_call (tree lhs, gimple stmt)
2444 {
2445   bool changed = false;
2446   struct vn_reference_s vr1;
2447   tree result;
2448   tree vuse = gimple_vuse (stmt);
2449
2450   vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2451   vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2452   vr1.type = gimple_expr_type (stmt);
2453   vr1.set = 0;
2454   vr1.hashcode = vn_reference_compute_hash (&vr1);
2455   result = vn_reference_lookup_1 (&vr1, NULL);
2456   if (result)
2457     {
2458       changed = set_ssa_val_to (lhs, result);
2459       if (TREE_CODE (result) == SSA_NAME
2460           && VN_INFO (result)->has_constants)
2461         VN_INFO (lhs)->has_constants = true;
2462     }
2463   else
2464     {
2465       void **slot;
2466       vn_reference_t vr2;
2467       changed = set_ssa_val_to (lhs, lhs);
2468       vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2469       vr2->vuse = vr1.vuse;
2470       vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2471       vr2->type = vr1.type;
2472       vr2->set = vr1.set;
2473       vr2->hashcode = vr1.hashcode;
2474       vr2->result = lhs;
2475       slot = htab_find_slot_with_hash (current_info->references,
2476                                        vr2, vr2->hashcode, INSERT);
2477       if (*slot)
2478         free_reference (*slot);
2479       *slot = vr2;
2480     }
2481
2482   return changed;
2483 }
2484
2485 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2486    and return true if the value number of the LHS has changed as a result.  */
2487
2488 static bool
2489 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2490 {
2491   bool changed = false;
2492   tree last_vuse;
2493   tree result;
2494
2495   last_vuse = gimple_vuse (stmt);
2496   last_vuse_ptr = &last_vuse;
2497   result = vn_reference_lookup (op, gimple_vuse (stmt),
2498                                 default_vn_walk_kind, NULL);
2499   last_vuse_ptr = NULL;
2500
2501   /* If we have a VCE, try looking up its operand as it might be stored in
2502      a different type.  */
2503   if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2504     result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2505                                   default_vn_walk_kind, NULL);
2506
2507   /* We handle type-punning through unions by value-numbering based
2508      on offset and size of the access.  Be prepared to handle a
2509      type-mismatch here via creating a VIEW_CONVERT_EXPR.  */
2510   if (result
2511       && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2512     {
2513       /* We will be setting the value number of lhs to the value number
2514          of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2515          So first simplify and lookup this expression to see if it
2516          is already available.  */
2517       tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2518       if ((CONVERT_EXPR_P (val)
2519            || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2520           && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2521         {
2522           tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2523           if ((CONVERT_EXPR_P (tem)
2524                || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2525               && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2526                                                     TREE_TYPE (val), tem)))
2527             val = tem;
2528         }
2529       result = val;
2530       if (!is_gimple_min_invariant (val)
2531           && TREE_CODE (val) != SSA_NAME)
2532         result = vn_nary_op_lookup (val, NULL);
2533       /* If the expression is not yet available, value-number lhs to
2534          a new SSA_NAME we create.  */
2535       if (!result)
2536         {
2537           result = make_ssa_name (SSA_NAME_VAR (lhs), gimple_build_nop ());
2538           /* Initialize value-number information properly.  */
2539           VN_INFO_GET (result)->valnum = result;
2540           VN_INFO (result)->value_id = get_next_value_id ();
2541           VN_INFO (result)->expr = val;
2542           VN_INFO (result)->has_constants = expr_has_constants (val);
2543           VN_INFO (result)->needs_insertion = true;
2544           /* As all "inserted" statements are singleton SCCs, insert
2545              to the valid table.  This is strictly needed to
2546              avoid re-generating new value SSA_NAMEs for the same
2547              expression during SCC iteration over and over (the
2548              optimistic table gets cleared after each iteration).
2549              We do not need to insert into the optimistic table, as
2550              lookups there will fall back to the valid table.  */
2551           if (current_info == optimistic_info)
2552             {
2553               current_info = valid_info;
2554               vn_nary_op_insert (val, result);
2555               current_info = optimistic_info;
2556             }
2557           else
2558             vn_nary_op_insert (val, result);
2559           if (dump_file && (dump_flags & TDF_DETAILS))
2560             {
2561               fprintf (dump_file, "Inserting name ");
2562               print_generic_expr (dump_file, result, 0);
2563               fprintf (dump_file, " for expression ");
2564               print_generic_expr (dump_file, val, 0);
2565               fprintf (dump_file, "\n");
2566             }
2567         }
2568     }
2569
2570   if (result)
2571     {
2572       changed = set_ssa_val_to (lhs, result);
2573       if (TREE_CODE (result) == SSA_NAME
2574           && VN_INFO (result)->has_constants)
2575         {
2576           VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2577           VN_INFO (lhs)->has_constants = true;
2578         }
2579     }
2580   else
2581     {
2582       changed = set_ssa_val_to (lhs, lhs);
2583       vn_reference_insert (op, lhs, last_vuse);
2584     }
2585
2586   return changed;
2587 }
2588
2589
2590 /* Visit a store to a reference operator LHS, part of STMT, value number it,
2591    and return true if the value number of the LHS has changed as a result.  */
2592
2593 static bool
2594 visit_reference_op_store (tree lhs, tree op, gimple stmt)
2595 {
2596   bool changed = false;
2597   tree result;
2598   bool resultsame = false;
2599
2600   /* First we want to lookup using the *vuses* from the store and see
2601      if there the last store to this location with the same address
2602      had the same value.
2603
2604      The vuses represent the memory state before the store.  If the
2605      memory state, address, and value of the store is the same as the
2606      last store to this location, then this store will produce the
2607      same memory state as that store.
2608
2609      In this case the vdef versions for this store are value numbered to those
2610      vuse versions, since they represent the same memory state after
2611      this store.
2612
2613      Otherwise, the vdefs for the store are used when inserting into
2614      the table, since the store generates a new memory state.  */
2615
2616   result = vn_reference_lookup (lhs, gimple_vuse (stmt), VN_NOWALK, NULL);
2617
2618   if (result)
2619     {
2620       if (TREE_CODE (result) == SSA_NAME)
2621         result = SSA_VAL (result);
2622       if (TREE_CODE (op) == SSA_NAME)
2623         op = SSA_VAL (op);
2624       resultsame = expressions_equal_p (result, op);
2625     }
2626
2627   if (!result || !resultsame)
2628     {
2629       tree vdef;
2630
2631       if (dump_file && (dump_flags & TDF_DETAILS))
2632         {
2633           fprintf (dump_file, "No store match\n");
2634           fprintf (dump_file, "Value numbering store ");
2635           print_generic_expr (dump_file, lhs, 0);
2636           fprintf (dump_file, " to ");
2637           print_generic_expr (dump_file, op, 0);
2638           fprintf (dump_file, "\n");
2639         }
2640       /* Have to set value numbers before insert, since insert is
2641          going to valueize the references in-place.  */
2642       if ((vdef = gimple_vdef (stmt)))
2643         {
2644           VN_INFO (vdef)->use_processed = true;
2645           changed |= set_ssa_val_to (vdef, vdef);
2646         }
2647
2648       /* Do not insert structure copies into the tables.  */
2649       if (is_gimple_min_invariant (op)
2650           || is_gimple_reg (op))
2651         vn_reference_insert (lhs, op, vdef);
2652     }
2653   else
2654     {
2655       /* We had a match, so value number the vdef to have the value
2656          number of the vuse it came from.  */
2657       tree def, use;
2658
2659       if (dump_file && (dump_flags & TDF_DETAILS))
2660         fprintf (dump_file, "Store matched earlier value,"
2661                  "value numbering store vdefs to matching vuses.\n");
2662
2663       def = gimple_vdef (stmt);
2664       use = gimple_vuse (stmt);
2665
2666       VN_INFO (def)->use_processed = true;
2667       changed |= set_ssa_val_to (def, SSA_VAL (use));
2668     }
2669
2670   return changed;
2671 }
2672
2673 /* Visit and value number PHI, return true if the value number
2674    changed.  */
2675
2676 static bool
2677 visit_phi (gimple phi)
2678 {
2679   bool changed = false;
2680   tree result;
2681   tree sameval = VN_TOP;
2682   bool allsame = true;
2683   unsigned i;
2684
2685   /* TODO: We could check for this in init_sccvn, and replace this
2686      with a gcc_assert.  */
2687   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
2688     return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2689
2690   /* See if all non-TOP arguments have the same value.  TOP is
2691      equivalent to everything, so we can ignore it.  */
2692   for (i = 0; i < gimple_phi_num_args (phi); i++)
2693     {
2694       tree def = PHI_ARG_DEF (phi, i);
2695
2696       if (TREE_CODE (def) == SSA_NAME)
2697         def = SSA_VAL (def);
2698       if (def == VN_TOP)
2699         continue;
2700       if (sameval == VN_TOP)
2701         {
2702           sameval = def;
2703         }
2704       else
2705         {
2706           if (!expressions_equal_p (def, sameval))
2707             {
2708               allsame = false;
2709               break;
2710             }
2711         }
2712     }
2713
2714   /* If all value numbered to the same value, the phi node has that
2715      value.  */
2716   if (allsame)
2717     {
2718       if (is_gimple_min_invariant (sameval))
2719         {
2720           VN_INFO (PHI_RESULT (phi))->has_constants = true;
2721           VN_INFO (PHI_RESULT (phi))->expr = sameval;
2722         }
2723       else
2724         {
2725           VN_INFO (PHI_RESULT (phi))->has_constants = false;
2726           VN_INFO (PHI_RESULT (phi))->expr = sameval;
2727         }
2728
2729       if (TREE_CODE (sameval) == SSA_NAME)
2730         return visit_copy (PHI_RESULT (phi), sameval);
2731
2732       return set_ssa_val_to (PHI_RESULT (phi), sameval);
2733     }
2734
2735   /* Otherwise, see if it is equivalent to a phi node in this block.  */
2736   result = vn_phi_lookup (phi);
2737   if (result)
2738     {
2739       if (TREE_CODE (result) == SSA_NAME)
2740         changed = visit_copy (PHI_RESULT (phi), result);
2741       else
2742         changed = set_ssa_val_to (PHI_RESULT (phi), result);
2743     }
2744   else
2745     {
2746       vn_phi_insert (phi, PHI_RESULT (phi));
2747       VN_INFO (PHI_RESULT (phi))->has_constants = false;
2748       VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
2749       changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2750     }
2751
2752   return changed;
2753 }
2754
2755 /* Return true if EXPR contains constants.  */
2756
2757 static bool
2758 expr_has_constants (tree expr)
2759 {
2760   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2761     {
2762     case tcc_unary:
2763       return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
2764
2765     case tcc_binary:
2766       return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
2767         || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
2768       /* Constants inside reference ops are rarely interesting, but
2769          it can take a lot of looking to find them.  */
2770     case tcc_reference:
2771     case tcc_declaration:
2772       return false;
2773     default:
2774       return is_gimple_min_invariant (expr);
2775     }
2776   return false;
2777 }
2778
2779 /* Return true if STMT contains constants.  */
2780
2781 static bool
2782 stmt_has_constants (gimple stmt)
2783 {
2784   if (gimple_code (stmt) != GIMPLE_ASSIGN)
2785     return false;
2786
2787   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2788     {
2789     case GIMPLE_UNARY_RHS:
2790       return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2791
2792     case GIMPLE_BINARY_RHS:
2793       return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
2794               || is_gimple_min_invariant (gimple_assign_rhs2 (stmt)));
2795     case GIMPLE_TERNARY_RHS:
2796       return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
2797               || is_gimple_min_invariant (gimple_assign_rhs2 (stmt))
2798               || is_gimple_min_invariant (gimple_assign_rhs3 (stmt)));
2799     case GIMPLE_SINGLE_RHS:
2800       /* Constants inside reference ops are rarely interesting, but
2801          it can take a lot of looking to find them.  */
2802       return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2803     default:
2804       gcc_unreachable ();
2805     }
2806   return false;
2807 }
2808
2809 /* Replace SSA_NAMES in expr with their value numbers, and return the
2810    result.
2811    This is performed in place. */
2812
2813 static tree
2814 valueize_expr (tree expr)
2815 {
2816   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2817     {
2818     case tcc_unary:
2819       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2820           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2821         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2822       break;
2823     case tcc_binary:
2824       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2825           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2826         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2827       if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
2828           && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
2829         TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
2830       break;
2831     default:
2832       break;
2833     }
2834   return expr;
2835 }
2836
2837 /* Simplify the binary expression RHS, and return the result if
2838    simplified. */
2839
2840 static tree
2841 simplify_binary_expression (gimple stmt)
2842 {
2843   tree result = NULL_TREE;
2844   tree op0 = gimple_assign_rhs1 (stmt);
2845   tree op1 = gimple_assign_rhs2 (stmt);
2846
2847   /* This will not catch every single case we could combine, but will
2848      catch those with constants.  The goal here is to simultaneously
2849      combine constants between expressions, but avoid infinite
2850      expansion of expressions during simplification.  */
2851   if (TREE_CODE (op0) == SSA_NAME)
2852     {
2853       if (VN_INFO (op0)->has_constants
2854           || TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
2855         op0 = valueize_expr (vn_get_expr_for (op0));
2856       else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
2857         op0 = SSA_VAL (op0);
2858     }
2859
2860   if (TREE_CODE (op1) == SSA_NAME)
2861     {
2862       if (VN_INFO (op1)->has_constants)
2863         op1 = valueize_expr (vn_get_expr_for (op1));
2864       else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
2865         op1 = SSA_VAL (op1);
2866     }
2867
2868   /* Pointer plus constant can be represented as invariant address.
2869      Do so to allow further propatation, see also tree forwprop.  */
2870   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
2871       && host_integerp (op1, 1)
2872       && TREE_CODE (op0) == ADDR_EXPR
2873       && is_gimple_min_invariant (op0))
2874     return build_invariant_address (TREE_TYPE (op0),
2875                                     TREE_OPERAND (op0, 0),
2876                                     TREE_INT_CST_LOW (op1));
2877
2878   /* Avoid folding if nothing changed.  */
2879   if (op0 == gimple_assign_rhs1 (stmt)
2880       && op1 == gimple_assign_rhs2 (stmt))
2881     return NULL_TREE;
2882
2883   fold_defer_overflow_warnings ();
2884
2885   result = fold_binary (gimple_assign_rhs_code (stmt),
2886                         gimple_expr_type (stmt), op0, op1);
2887   if (result)
2888     STRIP_USELESS_TYPE_CONVERSION (result);
2889
2890   fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
2891                                   stmt, 0);
2892
2893   /* Make sure result is not a complex expression consisting
2894      of operators of operators (IE (a + b) + (a + c))
2895      Otherwise, we will end up with unbounded expressions if
2896      fold does anything at all.  */
2897   if (result && valid_gimple_rhs_p (result))
2898     return result;
2899
2900   return NULL_TREE;
2901 }
2902
2903 /* Simplify the unary expression RHS, and return the result if
2904    simplified. */
2905
2906 static tree
2907 simplify_unary_expression (gimple stmt)
2908 {
2909   tree result = NULL_TREE;
2910   tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
2911
2912   /* We handle some tcc_reference codes here that are all
2913      GIMPLE_ASSIGN_SINGLE codes.  */
2914   if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
2915       || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2916       || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2917     op0 = TREE_OPERAND (op0, 0);
2918
2919   if (TREE_CODE (op0) != SSA_NAME)
2920     return NULL_TREE;
2921
2922   orig_op0 = op0;
2923   if (VN_INFO (op0)->has_constants)
2924     op0 = valueize_expr (vn_get_expr_for (op0));
2925   else if (gimple_assign_cast_p (stmt)
2926            || gimple_assign_rhs_code (stmt) == REALPART_EXPR
2927            || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2928            || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2929     {
2930       /* We want to do tree-combining on conversion-like expressions.
2931          Make sure we feed only SSA_NAMEs or constants to fold though.  */
2932       tree tem = valueize_expr (vn_get_expr_for (op0));
2933       if (UNARY_CLASS_P (tem)
2934           || BINARY_CLASS_P (tem)
2935           || TREE_CODE (tem) == VIEW_CONVERT_EXPR
2936           || TREE_CODE (tem) == SSA_NAME
2937           || is_gimple_min_invariant (tem))
2938         op0 = tem;
2939     }
2940
2941   /* Avoid folding if nothing changed, but remember the expression.  */
2942   if (op0 == orig_op0)
2943     return NULL_TREE;
2944
2945   result = fold_unary_ignore_overflow (gimple_assign_rhs_code (stmt),
2946                                        gimple_expr_type (stmt), op0);
2947   if (result)
2948     {
2949       STRIP_USELESS_TYPE_CONVERSION (result);
2950       if (valid_gimple_rhs_p (result))
2951         return result;
2952     }
2953
2954   return NULL_TREE;
2955 }
2956
2957 /* Valueize NAME if it is an SSA name, otherwise just return it.  */
2958
2959 static inline tree
2960 vn_valueize (tree name)
2961 {
2962   if (TREE_CODE (name) == SSA_NAME)
2963     {
2964       tree tem = SSA_VAL (name);
2965       return tem == VN_TOP ? name : tem;
2966     }
2967   return name;
2968 }
2969
2970 /* Try to simplify RHS using equivalences and constant folding.  */
2971
2972 static tree
2973 try_to_simplify (gimple stmt)
2974 {
2975   tree tem;
2976
2977   /* For stores we can end up simplifying a SSA_NAME rhs.  Just return
2978      in this case, there is no point in doing extra work.  */
2979   if (gimple_assign_copy_p (stmt)
2980       && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2981     return NULL_TREE;
2982
2983   /* First try constant folding based on our current lattice.  */
2984   tem = gimple_fold_stmt_to_constant (stmt, vn_valueize);
2985   if (tem)
2986     return tem;
2987
2988   /* If that didn't work try combining multiple statements.  */
2989   switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2990     {
2991     case tcc_reference:
2992       /* Fallthrough for some codes that can operate on registers.  */
2993       if (!(TREE_CODE (gimple_assign_rhs1 (stmt)) == REALPART_EXPR
2994             || TREE_CODE (gimple_assign_rhs1 (stmt)) == IMAGPART_EXPR
2995             || TREE_CODE (gimple_assign_rhs1 (stmt)) == VIEW_CONVERT_EXPR))
2996         break;
2997       /* We could do a little more with unary ops, if they expand
2998          into binary ops, but it's debatable whether it is worth it. */
2999     case tcc_unary:
3000       return simplify_unary_expression (stmt);
3001
3002     case tcc_comparison:
3003     case tcc_binary:
3004       return simplify_binary_expression (stmt);
3005
3006     default:
3007       break;
3008     }
3009
3010   return NULL_TREE;
3011 }
3012
3013 /* Visit and value number USE, return true if the value number
3014    changed. */
3015
3016 static bool
3017 visit_use (tree use)
3018 {
3019   bool changed = false;
3020   gimple stmt = SSA_NAME_DEF_STMT (use);
3021
3022   VN_INFO (use)->use_processed = true;
3023
3024   gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3025   if (dump_file && (dump_flags & TDF_DETAILS)
3026       && !SSA_NAME_IS_DEFAULT_DEF (use))
3027     {
3028       fprintf (dump_file, "Value numbering ");
3029       print_generic_expr (dump_file, use, 0);
3030       fprintf (dump_file, " stmt = ");
3031       print_gimple_stmt (dump_file, stmt, 0, 0);
3032     }
3033
3034   /* Handle uninitialized uses.  */
3035   if (SSA_NAME_IS_DEFAULT_DEF (use))
3036     changed = set_ssa_val_to (use, use);
3037   else
3038     {
3039       if (gimple_code (stmt) == GIMPLE_PHI)
3040         changed = visit_phi (stmt);
3041       else if (!gimple_has_lhs (stmt)
3042                || gimple_has_volatile_ops (stmt)
3043                || stmt_could_throw_p (stmt))
3044         changed = defs_to_varying (stmt);
3045       else if (is_gimple_assign (stmt))
3046         {
3047           tree lhs = gimple_assign_lhs (stmt);
3048           tree simplified;
3049
3050           /* Shortcut for copies. Simplifying copies is pointless,
3051              since we copy the expression and value they represent.  */
3052           if (gimple_assign_copy_p (stmt)
3053               && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
3054               && TREE_CODE (lhs) == SSA_NAME)
3055             {
3056               changed = visit_copy (lhs, gimple_assign_rhs1 (stmt));
3057               goto done;
3058             }
3059           simplified = try_to_simplify (stmt);
3060           if (simplified)
3061             {
3062               if (dump_file && (dump_flags & TDF_DETAILS))
3063                 {
3064                   fprintf (dump_file, "RHS ");
3065                   print_gimple_expr (dump_file, stmt, 0, 0);
3066                   fprintf (dump_file, " simplified to ");
3067                   print_generic_expr (dump_file, simplified, 0);
3068                   if (TREE_CODE (lhs) == SSA_NAME)
3069                     fprintf (dump_file, " has constants %d\n",
3070                              expr_has_constants (simplified));
3071                   else
3072                     fprintf (dump_file, "\n");
3073                 }
3074             }
3075           /* Setting value numbers to constants will occasionally
3076              screw up phi congruence because constants are not
3077              uniquely associated with a single ssa name that can be
3078              looked up.  */
3079           if (simplified
3080               && is_gimple_min_invariant (simplified)
3081               && TREE_CODE (lhs) == SSA_NAME)
3082             {
3083               VN_INFO (lhs)->expr = simplified;
3084               VN_INFO (lhs)->has_constants = true;
3085               changed = set_ssa_val_to (lhs, simplified);
3086               goto done;
3087             }
3088           else if (simplified
3089                    && TREE_CODE (simplified) == SSA_NAME
3090                    && TREE_CODE (lhs) == SSA_NAME)
3091             {
3092               changed = visit_copy (lhs, simplified);
3093               goto done;
3094             }
3095           else if (simplified)
3096             {
3097               if (TREE_CODE (lhs) == SSA_NAME)
3098                 {
3099                   VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3100                   /* We have to unshare the expression or else
3101                      valuizing may change the IL stream.  */
3102                   VN_INFO (lhs)->expr = unshare_expr (simplified);
3103                 }
3104             }
3105           else if (stmt_has_constants (stmt)
3106                    && TREE_CODE (lhs) == SSA_NAME)
3107             VN_INFO (lhs)->has_constants = true;
3108           else if (TREE_CODE (lhs) == SSA_NAME)
3109             {
3110               /* We reset expr and constantness here because we may
3111                  have been value numbering optimistically, and
3112                  iterating. They may become non-constant in this case,
3113                  even if they were optimistically constant. */
3114
3115               VN_INFO (lhs)->has_constants = false;
3116               VN_INFO (lhs)->expr = NULL_TREE;
3117             }
3118
3119           if ((TREE_CODE (lhs) == SSA_NAME
3120                /* We can substitute SSA_NAMEs that are live over
3121                   abnormal edges with their constant value.  */
3122                && !(gimple_assign_copy_p (stmt)
3123                     && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3124                && !(simplified
3125                     && is_gimple_min_invariant (simplified))
3126                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3127               /* Stores or copies from SSA_NAMEs that are live over
3128                  abnormal edges are a problem.  */
3129               || (gimple_assign_single_p (stmt)
3130                   && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
3131                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt))))
3132             changed = defs_to_varying (stmt);
3133           else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
3134             {
3135               changed = visit_reference_op_store (lhs, gimple_assign_rhs1 (stmt), stmt);
3136             }
3137           else if (TREE_CODE (lhs) == SSA_NAME)
3138             {
3139               if ((gimple_assign_copy_p (stmt)
3140                    && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3141                   || (simplified
3142                       && is_gimple_min_invariant (simplified)))
3143                 {
3144                   VN_INFO (lhs)->has_constants = true;
3145                   if (simplified)
3146                     changed = set_ssa_val_to (lhs, simplified);
3147                   else
3148                     changed = set_ssa_val_to (lhs, gimple_assign_rhs1 (stmt));
3149                 }
3150               else
3151                 {
3152                   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3153                     {
3154                     case GIMPLE_UNARY_RHS:
3155                     case GIMPLE_BINARY_RHS:
3156                     case GIMPLE_TERNARY_RHS:
3157                       changed = visit_nary_op (lhs, stmt);
3158                       break;
3159                     case GIMPLE_SINGLE_RHS:
3160                       switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
3161                         {
3162                         case tcc_reference:
3163                           /* VOP-less references can go through unary case.  */
3164                           if ((gimple_assign_rhs_code (stmt) == REALPART_EXPR
3165                                || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
3166                                || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
3167                               && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (stmt), 0)) == SSA_NAME)
3168                             {
3169                               changed = visit_nary_op (lhs, stmt);
3170                               break;
3171                             }
3172                           /* Fallthrough.  */
3173                         case tcc_declaration:
3174                           changed = visit_reference_op_load
3175                               (lhs, gimple_assign_rhs1 (stmt), stmt);
3176                           break;
3177                         case tcc_expression:
3178                           if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
3179                             {
3180                               changed = visit_nary_op (lhs, stmt);
3181                               break;
3182                             }
3183                           /* Fallthrough.  */
3184                         default:
3185                           changed = defs_to_varying (stmt);
3186                         }
3187                       break;
3188                     default:
3189                       changed = defs_to_varying (stmt);
3190                       break;
3191                     }
3192                 }
3193             }
3194           else
3195             changed = defs_to_varying (stmt);
3196         }
3197       else if (is_gimple_call (stmt))
3198         {
3199           tree lhs = gimple_call_lhs (stmt);
3200
3201           /* ???  We could try to simplify calls.  */
3202
3203           if (stmt_has_constants (stmt)
3204               && TREE_CODE (lhs) == SSA_NAME)
3205             VN_INFO (lhs)->has_constants = true;
3206           else if (TREE_CODE (lhs) == SSA_NAME)
3207             {
3208               /* We reset expr and constantness here because we may
3209                  have been value numbering optimistically, and
3210                  iterating. They may become non-constant in this case,
3211                  even if they were optimistically constant. */
3212               VN_INFO (lhs)->has_constants = false;
3213               VN_INFO (lhs)->expr = NULL_TREE;
3214             }
3215
3216           if (TREE_CODE (lhs) == SSA_NAME
3217               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3218             changed = defs_to_varying (stmt);
3219           /* ???  We should handle stores from calls.  */
3220           else if (TREE_CODE (lhs) == SSA_NAME)
3221             {
3222               if (!gimple_call_internal_p (stmt)
3223                   && gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST))
3224                 changed = visit_reference_op_call (lhs, stmt);
3225               else
3226                 changed = defs_to_varying (stmt);
3227             }
3228           else
3229             changed = defs_to_varying (stmt);
3230         }
3231     }
3232  done:
3233   return changed;
3234 }
3235
3236 /* Compare two operands by reverse postorder index */
3237
3238 static int
3239 compare_ops (const void *pa, const void *pb)
3240 {
3241   const tree opa = *((const tree *)pa);
3242   const tree opb = *((const tree *)pb);
3243   gimple opstmta = SSA_NAME_DEF_STMT (opa);
3244   gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3245   basic_block bba;
3246   basic_block bbb;
3247
3248   if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3249     return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3250   else if (gimple_nop_p (opstmta))
3251     return -1;
3252   else if (gimple_nop_p (opstmtb))
3253     return 1;
3254
3255   bba = gimple_bb (opstmta);
3256   bbb = gimple_bb (opstmtb);
3257
3258   if (!bba && !bbb)
3259     return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3260   else if (!bba)
3261     return -1;
3262   else if (!bbb)
3263     return 1;
3264
3265   if (bba == bbb)
3266     {
3267       if (gimple_code (opstmta) == GIMPLE_PHI
3268           && gimple_code (opstmtb) == GIMPLE_PHI)
3269         return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3270       else if (gimple_code (opstmta) == GIMPLE_PHI)
3271         return -1;
3272       else if (gimple_code (opstmtb) == GIMPLE_PHI)
3273         return 1;
3274       else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3275         return gimple_uid (opstmta) - gimple_uid (opstmtb);
3276       else
3277         return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3278     }
3279   return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3280 }
3281
3282 /* Sort an array containing members of a strongly connected component
3283    SCC so that the members are ordered by RPO number.
3284    This means that when the sort is complete, iterating through the
3285    array will give you the members in RPO order.  */
3286
3287 static void
3288 sort_scc (VEC (tree, heap) *scc)
3289 {
3290   VEC_qsort (tree, scc, compare_ops);
3291 }
3292
3293 /* Insert the no longer used nary ONARY to the hash INFO.  */
3294
3295 static void
3296 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3297 {
3298   size_t size = sizeof_vn_nary_op (onary->length);
3299   vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3300                                                &info->nary_obstack);
3301   memcpy (nary, onary, size);
3302   vn_nary_op_insert_into (nary, info->nary, false);
3303 }
3304
3305 /* Insert the no longer used phi OPHI to the hash INFO.  */
3306
3307 static void
3308 copy_phi (vn_phi_t ophi, vn_tables_t info)
3309 {
3310   vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3311   void **slot;
3312   memcpy (phi, ophi, sizeof (*phi));
3313   ophi->phiargs = NULL;
3314   slot = htab_find_slot_with_hash (info->phis, phi, phi->hashcode, INSERT);
3315   gcc_assert (!*slot);
3316   *slot = phi;
3317 }
3318
3319 /* Insert the no longer used reference OREF to the hash INFO.  */
3320
3321 static void
3322 copy_reference (vn_reference_t oref, vn_tables_t info)
3323 {
3324   vn_reference_t ref;
3325   void **slot;
3326   ref = (vn_reference_t) pool_alloc (info->references_pool);
3327   memcpy (ref, oref, sizeof (*ref));
3328   oref->operands = NULL;
3329   slot = htab_find_slot_with_hash (info->references, ref, ref->hashcode,
3330                                    INSERT);
3331   if (*slot)
3332     free_reference (*slot);
3333   *slot = ref;
3334 }
3335
3336 /* Process a strongly connected component in the SSA graph.  */
3337
3338 static void
3339 process_scc (VEC (tree, heap) *scc)
3340 {
3341   tree var;
3342   unsigned int i;
3343   unsigned int iterations = 0;
3344   bool changed = true;
3345   htab_iterator hi;
3346   vn_nary_op_t nary;
3347   vn_phi_t phi;
3348   vn_reference_t ref;
3349
3350   /* If the SCC has a single member, just visit it.  */
3351   if (VEC_length (tree, scc) == 1)
3352     {
3353       tree use = VEC_index (tree, scc, 0);
3354       if (VN_INFO (use)->use_processed)
3355         return;
3356       /* We need to make sure it doesn't form a cycle itself, which can
3357          happen for self-referential PHI nodes.  In that case we would
3358          end up inserting an expression with VN_TOP operands into the
3359          valid table which makes us derive bogus equivalences later.
3360          The cheapest way to check this is to assume it for all PHI nodes.  */
3361       if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3362         /* Fallthru to iteration.  */ ;
3363       else
3364         {
3365           visit_use (use);
3366           return;
3367         }
3368     }
3369
3370   /* Iterate over the SCC with the optimistic table until it stops
3371      changing.  */
3372   current_info = optimistic_info;
3373   while (changed)
3374     {
3375       changed = false;
3376       iterations++;
3377       if (dump_file && (dump_flags & TDF_DETAILS))
3378         fprintf (dump_file, "Starting iteration %d\n", iterations);
3379       /* As we are value-numbering optimistically we have to
3380          clear the expression tables and the simplified expressions
3381          in each iteration until we converge.  */
3382       htab_empty (optimistic_info->nary);
3383       htab_empty (optimistic_info->phis);
3384       htab_empty (optimistic_info->references);
3385       obstack_free (&optimistic_info->nary_obstack, NULL);
3386       gcc_obstack_init (&optimistic_info->nary_obstack);
3387       empty_alloc_pool (optimistic_info->phis_pool);
3388       empty_alloc_pool (optimistic_info->references_pool);
3389       FOR_EACH_VEC_ELT (tree, scc, i, var)
3390         VN_INFO (var)->expr = NULL_TREE;
3391       FOR_EACH_VEC_ELT (tree, scc, i, var)
3392         changed |= visit_use (var);
3393     }
3394
3395   statistics_histogram_event (cfun, "SCC iterations", iterations);
3396
3397   /* Finally, copy the contents of the no longer used optimistic
3398      table to the valid table.  */
3399   FOR_EACH_HTAB_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hi)
3400     copy_nary (nary, valid_info);
3401   FOR_EACH_HTAB_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hi)
3402     copy_phi (phi, valid_info);
3403   FOR_EACH_HTAB_ELEMENT (optimistic_info->references, ref, vn_reference_t, hi)
3404     copy_reference (ref, valid_info);
3405
3406   current_info = valid_info;
3407 }
3408
3409 DEF_VEC_O(ssa_op_iter);
3410 DEF_VEC_ALLOC_O(ssa_op_iter,heap);
3411
3412 /* Pop the components of the found SCC for NAME off the SCC stack
3413    and process them.  Returns true if all went well, false if
3414    we run into resource limits.  */
3415
3416 static bool
3417 extract_and_process_scc_for_name (tree name)
3418 {
3419   VEC (tree, heap) *scc = NULL;
3420   tree x;
3421
3422   /* Found an SCC, pop the components off the SCC stack and
3423      process them.  */
3424   do
3425     {
3426       x = VEC_pop (tree, sccstack);
3427
3428       VN_INFO (x)->on_sccstack = false;
3429       VEC_safe_push (tree, heap, scc, x);
3430     } while (x != name);
3431
3432   /* Bail out of SCCVN in case a SCC turns out to be incredibly large.  */
3433   if (VEC_length (tree, scc)
3434       > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3435     {
3436       if (dump_file)
3437         fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3438                  "SCC size %u exceeding %u\n", VEC_length (tree, scc),
3439                  (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3440       return false;
3441     }
3442
3443   if (VEC_length (tree, scc) > 1)
3444     sort_scc (scc);
3445
3446   if (dump_file && (dump_flags & TDF_DETAILS))
3447     print_scc (dump_file, scc);
3448
3449   process_scc (scc);
3450
3451   VEC_free (tree, heap, scc);
3452
3453   return true;
3454 }
3455
3456 /* Depth first search on NAME to discover and process SCC's in the SSA
3457    graph.
3458    Execution of this algorithm relies on the fact that the SCC's are
3459    popped off the stack in topological order.
3460    Returns true if successful, false if we stopped processing SCC's due
3461    to resource constraints.  */
3462
3463 static bool
3464 DFS (tree name)
3465 {
3466   VEC(ssa_op_iter, heap) *itervec = NULL;
3467   VEC(tree, heap) *namevec = NULL;
3468   use_operand_p usep = NULL;
3469   gimple defstmt;
3470   tree use;
3471   ssa_op_iter iter;
3472
3473 start_over:
3474   /* SCC info */
3475   VN_INFO (name)->dfsnum = next_dfs_num++;
3476   VN_INFO (name)->visited = true;
3477   VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3478
3479   VEC_safe_push (tree, heap, sccstack, name);
3480   VN_INFO (name)->on_sccstack = true;
3481   defstmt = SSA_NAME_DEF_STMT (name);
3482
3483   /* Recursively DFS on our operands, looking for SCC's.  */
3484   if (!gimple_nop_p (defstmt))
3485     {
3486       /* Push a new iterator.  */
3487       if (gimple_code (defstmt) == GIMPLE_PHI)
3488         usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3489       else
3490         usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3491     }
3492   else
3493     clear_and_done_ssa_iter (&iter);
3494
3495   while (1)
3496     {
3497       /* If we are done processing uses of a name, go up the stack
3498          of iterators and process SCCs as we found them.  */
3499       if (op_iter_done (&iter))
3500         {
3501           /* See if we found an SCC.  */
3502           if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3503             if (!extract_and_process_scc_for_name (name))
3504               {
3505                 VEC_free (tree, heap, namevec);
3506                 VEC_free (ssa_op_iter, heap, itervec);
3507                 return false;
3508               }
3509
3510           /* Check if we are done.  */
3511           if (VEC_empty (tree, namevec))
3512             {
3513               VEC_free (tree, heap, namevec);
3514               VEC_free (ssa_op_iter, heap, itervec);
3515               return true;
3516             }
3517
3518           /* Restore the last use walker and continue walking there.  */
3519           use = name;
3520           name = VEC_pop (tree, namevec);
3521           memcpy (&iter, VEC_last (ssa_op_iter, itervec),
3522                   sizeof (ssa_op_iter));
3523           VEC_pop (ssa_op_iter, itervec);
3524           goto continue_walking;
3525         }
3526
3527       use = USE_FROM_PTR (usep);
3528
3529       /* Since we handle phi nodes, we will sometimes get
3530          invariants in the use expression.  */
3531       if (TREE_CODE (use) == SSA_NAME)
3532         {
3533           if (! (VN_INFO (use)->visited))
3534             {
3535               /* Recurse by pushing the current use walking state on
3536                  the stack and starting over.  */
3537               VEC_safe_push(ssa_op_iter, heap, itervec, &iter);
3538               VEC_safe_push(tree, heap, namevec, name);
3539               name = use;
3540               goto start_over;
3541
3542 continue_walking:
3543               VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3544                                          VN_INFO (use)->low);
3545             }
3546           if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3547               && VN_INFO (use)->on_sccstack)
3548             {
3549               VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3550                                          VN_INFO (name)->low);
3551             }
3552         }
3553
3554       usep = op_iter_next_use (&iter);
3555     }
3556 }
3557
3558 /* Allocate a value number table.  */
3559
3560 static void
3561 allocate_vn_table (vn_tables_t table)
3562 {
3563   table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
3564   table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
3565   table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
3566                                    free_reference);
3567
3568   gcc_obstack_init (&table->nary_obstack);
3569   table->phis_pool = create_alloc_pool ("VN phis",
3570                                         sizeof (struct vn_phi_s),
3571                                         30);
3572   table->references_pool = create_alloc_pool ("VN references",
3573                                               sizeof (struct vn_reference_s),
3574                                               30);
3575 }
3576
3577 /* Free a value number table.  */
3578
3579 static void
3580 free_vn_table (vn_tables_t table)
3581 {
3582   htab_delete (table->phis);
3583   htab_delete (table->nary);
3584   htab_delete (table->references);
3585   obstack_free (&table->nary_obstack, NULL);
3586   free_alloc_pool (table->phis_pool);
3587   free_alloc_pool (table->references_pool);
3588 }
3589
3590 static void
3591 init_scc_vn (void)
3592 {
3593   size_t i;
3594   int j;
3595   int *rpo_numbers_temp;
3596
3597   calculate_dominance_info (CDI_DOMINATORS);
3598   sccstack = NULL;
3599   constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
3600                                   free);
3601
3602   constant_value_ids = BITMAP_ALLOC (NULL);
3603
3604   next_dfs_num = 1;
3605   next_value_id = 1;
3606
3607   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
3608   /* VEC_alloc doesn't actually grow it to the right size, it just
3609      preallocates the space to do so.  */
3610   VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
3611   gcc_obstack_init (&vn_ssa_aux_obstack);
3612
3613   shared_lookup_phiargs = NULL;
3614   shared_lookup_references = NULL;
3615   rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3616   rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3617   pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3618
3619   /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3620      the i'th block in RPO order is bb.  We want to map bb's to RPO
3621      numbers, so we need to rearrange this array.  */
3622   for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3623     rpo_numbers[rpo_numbers_temp[j]] = j;
3624
3625   XDELETE (rpo_numbers_temp);
3626
3627   VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3628
3629   /* Create the VN_INFO structures, and initialize value numbers to
3630      TOP.  */
3631   for (i = 0; i < num_ssa_names; i++)
3632     {
3633       tree name = ssa_name (i);
3634       if (name)
3635         {
3636           VN_INFO_GET (name)->valnum = VN_TOP;
3637           VN_INFO (name)->expr = NULL_TREE;
3638           VN_INFO (name)->value_id = 0;
3639         }
3640     }
3641
3642   renumber_gimple_stmt_uids ();
3643
3644   /* Create the valid and optimistic value numbering tables.  */
3645   valid_info = XCNEW (struct vn_tables_s);
3646   allocate_vn_table (valid_info);
3647   optimistic_info = XCNEW (struct vn_tables_s);
3648   allocate_vn_table (optimistic_info);
3649 }
3650
3651 void
3652 free_scc_vn (void)
3653 {
3654   size_t i;
3655
3656   htab_delete (constant_to_value_id);
3657   BITMAP_FREE (constant_value_ids);
3658   VEC_free (tree, heap, shared_lookup_phiargs);
3659   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
3660   XDELETEVEC (rpo_numbers);
3661
3662   for (i = 0; i < num_ssa_names; i++)
3663     {
3664       tree name = ssa_name (i);
3665       if (name
3666           && VN_INFO (name)->needs_insertion)
3667         release_ssa_name (name);
3668     }
3669   obstack_free (&vn_ssa_aux_obstack, NULL);
3670   VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
3671
3672   VEC_free (tree, heap, sccstack);
3673   free_vn_table (valid_info);
3674   XDELETE (valid_info);
3675   free_vn_table (optimistic_info);
3676   XDELETE (optimistic_info);
3677 }
3678
3679 /* Set *ID if we computed something useful in RESULT.  */
3680
3681 static void
3682 set_value_id_for_result (tree result, unsigned int *id)
3683 {
3684   if (result)
3685     {
3686       if (TREE_CODE (result) == SSA_NAME)
3687         *id = VN_INFO (result)->value_id;
3688       else if (is_gimple_min_invariant (result))
3689         *id = get_or_alloc_constant_value_id (result);
3690     }
3691 }
3692
3693 /* Set the value ids in the valid hash tables.  */
3694
3695 static void
3696 set_hashtable_value_ids (void)
3697 {
3698   htab_iterator hi;
3699   vn_nary_op_t vno;
3700   vn_reference_t vr;
3701   vn_phi_t vp;
3702
3703   /* Now set the value ids of the things we had put in the hash
3704      table.  */
3705
3706   FOR_EACH_HTAB_ELEMENT (valid_info->nary,
3707                          vno, vn_nary_op_t, hi)
3708     set_value_id_for_result (vno->result, &vno->value_id);
3709
3710   FOR_EACH_HTAB_ELEMENT (valid_info->phis,
3711                          vp, vn_phi_t, hi)
3712     set_value_id_for_result (vp->result, &vp->value_id);
3713
3714   FOR_EACH_HTAB_ELEMENT (valid_info->references,
3715                          vr, vn_reference_t, hi)
3716     set_value_id_for_result (vr->result, &vr->value_id);
3717 }
3718
3719 /* Do SCCVN.  Returns true if it finished, false if we bailed out
3720    due to resource constraints.  DEFAULT_VN_WALK_KIND_ specifies
3721    how we use the alias oracle walking during the VN process.  */
3722
3723 bool
3724 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
3725 {
3726   size_t i;
3727   tree param;
3728   bool changed = true;
3729
3730   default_vn_walk_kind = default_vn_walk_kind_;
3731
3732   init_scc_vn ();
3733   current_info = valid_info;
3734
3735   for (param = DECL_ARGUMENTS (current_function_decl);
3736        param;
3737        param = DECL_CHAIN (param))
3738     {
3739       if (gimple_default_def (cfun, param) != NULL)
3740         {
3741           tree def = gimple_default_def (cfun, param);
3742           VN_INFO (def)->valnum = def;
3743         }
3744     }
3745
3746   for (i = 1; i < num_ssa_names; ++i)
3747     {
3748       tree name = ssa_name (i);
3749       if (name
3750           && VN_INFO (name)->visited == false
3751           && !has_zero_uses (name))
3752         if (!DFS (name))
3753           {
3754             free_scc_vn ();
3755             return false;
3756           }
3757     }
3758
3759   /* Initialize the value ids.  */
3760
3761   for (i = 1; i < num_ssa_names; ++i)
3762     {
3763       tree name = ssa_name (i);
3764       vn_ssa_aux_t info;
3765       if (!name)
3766         continue;
3767       info = VN_INFO (name);
3768       if (info->valnum == name
3769           || info->valnum == VN_TOP)
3770         info->value_id = get_next_value_id ();
3771       else if (is_gimple_min_invariant (info->valnum))
3772         info->value_id = get_or_alloc_constant_value_id (info->valnum);
3773     }
3774
3775   /* Propagate until they stop changing.  */
3776   while (changed)
3777     {
3778       changed = false;
3779       for (i = 1; i < num_ssa_names; ++i)
3780         {
3781           tree name = ssa_name (i);
3782           vn_ssa_aux_t info;
3783           if (!name)
3784             continue;
3785           info = VN_INFO (name);
3786           if (TREE_CODE (info->valnum) == SSA_NAME
3787               && info->valnum != name
3788               && info->value_id != VN_INFO (info->valnum)->value_id)
3789             {
3790               changed = true;
3791               info->value_id = VN_INFO (info->valnum)->value_id;
3792             }
3793         }
3794     }
3795
3796   set_hashtable_value_ids ();
3797
3798   if (dump_file && (dump_flags & TDF_DETAILS))
3799     {
3800       fprintf (dump_file, "Value numbers:\n");
3801       for (i = 0; i < num_ssa_names; i++)
3802         {
3803           tree name = ssa_name (i);
3804           if (name
3805               && VN_INFO (name)->visited
3806               && SSA_VAL (name) != name)
3807             {
3808               print_generic_expr (dump_file, name, 0);
3809               fprintf (dump_file, " = ");
3810               print_generic_expr (dump_file, SSA_VAL (name), 0);
3811               fprintf (dump_file, "\n");
3812             }
3813         }
3814     }
3815
3816   return true;
3817 }
3818
3819 /* Return the maximum value id we have ever seen.  */
3820
3821 unsigned int
3822 get_max_value_id (void)
3823 {
3824   return next_value_id;
3825 }
3826
3827 /* Return the next unique value id.  */
3828
3829 unsigned int
3830 get_next_value_id (void)
3831 {
3832   return next_value_id++;
3833 }
3834
3835
3836 /* Compare two expressions E1 and E2 and return true if they are equal.  */
3837
3838 bool
3839 expressions_equal_p (tree e1, tree e2)
3840 {
3841   /* The obvious case.  */
3842   if (e1 == e2)
3843     return true;
3844
3845   /* If only one of them is null, they cannot be equal.  */
3846   if (!e1 || !e2)
3847     return false;
3848
3849   /* Now perform the actual comparison.  */
3850   if (TREE_CODE (e1) == TREE_CODE (e2)
3851       && operand_equal_p (e1, e2, OEP_PURE_SAME))
3852     return true;
3853
3854   return false;
3855 }
3856
3857
3858 /* Return true if the nary operation NARY may trap.  This is a copy
3859    of stmt_could_throw_1_p adjusted to the SCCVN IL.  */
3860
3861 bool
3862 vn_nary_may_trap (vn_nary_op_t nary)
3863 {
3864   tree type;
3865   tree rhs2 = NULL_TREE;
3866   bool honor_nans = false;
3867   bool honor_snans = false;
3868   bool fp_operation = false;
3869   bool honor_trapv = false;
3870   bool handled, ret;
3871   unsigned i;
3872
3873   if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
3874       || TREE_CODE_CLASS (nary->opcode) == tcc_unary
3875       || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
3876     {
3877       type = nary->type;
3878       fp_operation = FLOAT_TYPE_P (type);
3879       if (fp_operation)
3880         {
3881           honor_nans = flag_trapping_math && !flag_finite_math_only;
3882           honor_snans = flag_signaling_nans != 0;
3883         }
3884       else if (INTEGRAL_TYPE_P (type)
3885                && TYPE_OVERFLOW_TRAPS (type))
3886         honor_trapv = true;
3887     }
3888   if (nary->length >= 2)
3889     rhs2 = nary->op[1];
3890   ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
3891                                        honor_trapv,
3892                                        honor_nans, honor_snans, rhs2,
3893                                        &handled);
3894   if (handled
3895       && ret)
3896     return true;
3897
3898   for (i = 0; i < nary->length; ++i)
3899     if (tree_could_trap_p (nary->op[i]))
3900       return true;
3901
3902   return false;
3903 }