OSDN Git Service

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