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