OSDN Git Service

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