gimple.h (gimple_call_set_chain): Accept SSA variables.
[platform/upstream/gcc.git] / gcc / tree-ssa-sccvn.c
1 /* SCC value numbering for trees
2    Copyright (C) 2006, 2007, 2008
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         expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
264                             gimple_expr_type (def_stmt),
265                             TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
266       break;
267
268     case tcc_unary:
269       expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
270                           gimple_expr_type (def_stmt),
271                           gimple_assign_rhs1 (def_stmt));
272       break;
273
274     case tcc_binary:
275       expr = fold_build2 (gimple_assign_rhs_code (def_stmt),
276                           gimple_expr_type (def_stmt),
277                           gimple_assign_rhs1 (def_stmt),
278                           gimple_assign_rhs2 (def_stmt));
279       break;
280
281     default:;
282     }
283   if (expr == NULL_TREE)
284     return vn->valnum;
285
286   /* Cache the expression.  */
287   vn->expr = expr;
288
289   return expr;
290 }
291
292
293 /* Free a phi operation structure VP.  */
294
295 static void
296 free_phi (void *vp)
297 {
298   vn_phi_t phi = (vn_phi_t) vp;
299   VEC_free (tree, heap, phi->phiargs);
300 }
301
302 /* Free a reference operation structure VP.  */
303
304 static void
305 free_reference (void *vp)
306 {
307   vn_reference_t vr = (vn_reference_t) vp;
308   VEC_free (vn_reference_op_s, heap, vr->operands);
309 }
310
311 /* Hash table equality function for vn_constant_t.  */
312
313 static int
314 vn_constant_eq (const void *p1, const void *p2)
315 {
316   const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
317   const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
318
319   return vn_constant_eq_with_type (vc1->constant, vc2->constant);
320 }
321
322 /* Hash table hash function for vn_constant_t.  */
323    
324 static hashval_t
325 vn_constant_hash (const void *p1)
326 {
327   const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
328   return vc1->hashcode;
329 }
330
331 /* Lookup a value id for CONSTANT and return it.  If it does not
332    exist returns 0.  */
333
334 unsigned int
335 get_constant_value_id (tree constant)
336 {
337   void **slot;
338   struct vn_constant_s vc;
339
340   vc.hashcode = vn_hash_constant_with_type (constant);
341   vc.constant = constant;
342   slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
343                                    vc.hashcode, NO_INSERT);
344   if (slot)
345     return ((vn_constant_t)*slot)->value_id;
346   return 0;
347 }
348
349 /* Lookup a value id for CONSTANT, and if it does not exist, create a
350    new one and return it.  If it does exist, return it.  */
351
352 unsigned int
353 get_or_alloc_constant_value_id (tree constant)
354 {
355   void **slot;
356   vn_constant_t vc = XNEW (struct vn_constant_s);
357   
358   vc->hashcode = vn_hash_constant_with_type (constant);
359   vc->constant = constant;
360   slot = htab_find_slot_with_hash (constant_to_value_id, vc,
361                                    vc->hashcode, INSERT);  
362   if (*slot)
363     {
364       free (vc);
365       return ((vn_constant_t)*slot)->value_id;
366     }
367   vc->value_id = get_next_value_id ();
368   *slot = vc;
369   bitmap_set_bit (constant_value_ids, vc->value_id);
370   return vc->value_id;
371 }
372
373 /* Return true if V is a value id for a constant.  */
374
375 bool
376 value_id_constant_p (unsigned int v)
377 {
378   return bitmap_bit_p (constant_value_ids, v);  
379 }
380
381 /* Compare two reference operands P1 and P2 for equality.  Return true if
382    they are equal, and false otherwise.  */
383
384 static int
385 vn_reference_op_eq (const void *p1, const void *p2)
386 {
387   const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
388   const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
389   return vro1->opcode == vro2->opcode
390     && vro1->type == vro2->type
391     && expressions_equal_p (vro1->op0, vro2->op0)
392     && expressions_equal_p (vro1->op1, vro2->op1)
393     && expressions_equal_p (vro1->op2, vro2->op2);
394 }
395
396 /* Compute the hash for a reference operand VRO1.  */
397
398 static hashval_t
399 vn_reference_op_compute_hash (const vn_reference_op_t vro1)
400 {
401   return iterative_hash_expr (vro1->op0, vro1->opcode)
402     + iterative_hash_expr (vro1->op1, vro1->opcode)
403     + iterative_hash_expr (vro1->op2, vro1->opcode);
404 }
405
406 /* Return the hashcode for a given reference operation P1.  */
407
408 static hashval_t
409 vn_reference_hash (const void *p1)
410 {
411   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
412   return vr1->hashcode;
413 }
414
415 /* Compute a hash for the reference operation VR1 and return it.  */
416
417 hashval_t
418 vn_reference_compute_hash (const vn_reference_t vr1)
419 {
420   hashval_t result = 0;
421   tree v;
422   int i;
423   vn_reference_op_t vro;
424
425   for (i = 0; VEC_iterate (tree, vr1->vuses, i, v); i++)
426     result += iterative_hash_expr (v, 0);
427   for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
428     result += vn_reference_op_compute_hash (vro);
429
430   return result;
431 }
432
433 /* Return true if reference operations P1 and P2 are equivalent.  This
434    means they have the same set of operands and vuses.  */
435
436 int
437 vn_reference_eq (const void *p1, const void *p2)
438 {
439   tree v;
440   int i;
441   vn_reference_op_t vro;
442
443   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
444   const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
445
446   if (vr1->vuses == vr2->vuses
447       && vr1->operands == vr2->operands)
448     return true;
449
450   /* Impossible for them to be equivalent if they have different
451      number of vuses.  */
452   if (VEC_length (tree, vr1->vuses) != VEC_length (tree, vr2->vuses))
453     return false;
454
455   /* We require that address operands be canonicalized in a way that
456      two memory references will have the same operands if they are
457      equivalent.  */
458   if (VEC_length (vn_reference_op_s, vr1->operands)
459       != VEC_length (vn_reference_op_s, vr2->operands))
460     return false;
461
462   /* The memory state is more often different than the address of the
463      store/load, so check it first.  */
464   for (i = 0; VEC_iterate (tree, vr1->vuses, i, v); i++)
465     {
466       if (VEC_index (tree, vr2->vuses, i) != v)
467         return false;
468     }
469
470   for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
471     {
472       if (!vn_reference_op_eq (VEC_index (vn_reference_op_s, vr2->operands, i),
473                                vro))
474         return false;
475     }
476   return true;
477 }
478
479 /* Place the vuses from STMT into *result.  */
480
481 static inline void
482 vuses_to_vec (gimple stmt, VEC (tree, gc) **result)
483 {
484   ssa_op_iter iter;
485   tree vuse;
486
487   if (!stmt)
488     return;
489
490   VEC_reserve_exact (tree, gc, *result,
491                      num_ssa_operands (stmt, SSA_OP_VIRTUAL_USES));
492
493   FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VIRTUAL_USES)
494     VEC_quick_push (tree, *result, vuse);
495 }
496
497
498 /* Copy the VUSE names in STMT into a vector, and return
499    the vector.  */
500
501 VEC (tree, gc) *
502 copy_vuses_from_stmt (gimple stmt)
503 {
504   VEC (tree, gc) *vuses = NULL;
505
506   vuses_to_vec (stmt, &vuses);
507
508   return vuses;
509 }
510
511 /* Place the vdefs from STMT into *result.  */
512
513 static inline void
514 vdefs_to_vec (gimple stmt, VEC (tree, gc) **result)
515 {
516   ssa_op_iter iter;
517   tree vdef;
518
519   if (!stmt)
520     return;
521
522   *result = VEC_alloc (tree, gc, num_ssa_operands (stmt, SSA_OP_VIRTUAL_DEFS));
523
524   FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, iter, SSA_OP_VIRTUAL_DEFS)
525     VEC_quick_push (tree, *result, vdef);
526 }
527
528 /* Copy the names of vdef results in STMT into a vector, and return
529    the vector.  */
530
531 static VEC (tree, gc) *
532 copy_vdefs_from_stmt (gimple stmt)
533 {
534   VEC (tree, gc) *vdefs = NULL;
535
536   vdefs_to_vec (stmt, &vdefs);
537
538   return vdefs;
539 }
540
541 /* Place for shared_v{uses/defs}_from_stmt to shove vuses/vdefs.  */
542 static VEC (tree, gc) *shared_lookup_vops;
543
544 /* Copy the virtual uses from STMT into SHARED_LOOKUP_VOPS.
545    This function will overwrite the current SHARED_LOOKUP_VOPS
546    variable.  */
547
548 VEC (tree, gc) *
549 shared_vuses_from_stmt (gimple stmt)
550 {
551   VEC_truncate (tree, shared_lookup_vops, 0);
552   vuses_to_vec (stmt, &shared_lookup_vops);
553
554   return shared_lookup_vops;
555 }
556
557 /* Copy the operations present in load/store REF into RESULT, a vector of
558    vn_reference_op_s's.  */
559
560 void
561 copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
562 {
563   if (TREE_CODE (ref) == TARGET_MEM_REF)
564     {
565       vn_reference_op_s temp;
566
567       memset (&temp, 0, sizeof (temp));
568       /* We do not care for spurious type qualifications.  */
569       temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
570       temp.opcode = TREE_CODE (ref);
571       temp.op0 = TMR_SYMBOL (ref) ? TMR_SYMBOL (ref) : TMR_BASE (ref);
572       temp.op1 = TMR_INDEX (ref);
573       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
574
575       memset (&temp, 0, sizeof (temp));
576       temp.type = NULL_TREE;
577       temp.opcode = TREE_CODE (ref);
578       temp.op0 = TMR_STEP (ref);
579       temp.op1 = TMR_OFFSET (ref);
580       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
581       return;
582     }
583
584   /* For non-calls, store the information that makes up the address.  */
585
586   while (ref)
587     {
588       vn_reference_op_s temp;
589
590       memset (&temp, 0, sizeof (temp));
591       /* We do not care for spurious type qualifications.  */
592       temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
593       temp.opcode = TREE_CODE (ref);
594
595       switch (temp.opcode)
596         {
597         case ALIGN_INDIRECT_REF:
598         case MISALIGNED_INDIRECT_REF:
599         case INDIRECT_REF:
600           /* The only operand is the address, which gets its own
601              vn_reference_op_s structure.  */
602           break;
603         case BIT_FIELD_REF:
604           /* Record bits and position.  */
605           temp.op0 = TREE_OPERAND (ref, 1);
606           temp.op1 = TREE_OPERAND (ref, 2);
607           break;
608         case COMPONENT_REF:
609           /* The field decl is enough to unambiguously specify the field,
610              a matching type is not necessary and a mismatching type
611              is always a spurious difference.  */
612           temp.type = NULL_TREE;
613 #if FIXME
614           /* If this is a reference to a union member, record the union
615              member size as operand.  Do so only if we are doing
616              expression insertion (during FRE), as PRE currently gets
617              confused with this.  */
618           if (may_insert
619               && TREE_CODE (DECL_CONTEXT (TREE_OPERAND (ref, 1))) == UNION_TYPE
620               && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (ref, 1)))
621               && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1))))
622             temp.op0 = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)));
623           else
624 #endif
625             /* Record field as operand.  */
626             temp.op0 = TREE_OPERAND (ref, 1);
627             temp.op1 = TREE_OPERAND (ref, 2);     
628           break;
629         case ARRAY_RANGE_REF:
630         case ARRAY_REF:
631           /* Record index as operand.  */
632           temp.op0 = TREE_OPERAND (ref, 1);
633           temp.op1 = TREE_OPERAND (ref, 2);
634           temp.op2 = TREE_OPERAND (ref, 3);
635           break;
636         case STRING_CST:
637         case INTEGER_CST:
638         case COMPLEX_CST:
639         case VECTOR_CST:
640         case REAL_CST:
641         case CONSTRUCTOR:
642         case VAR_DECL:
643         case PARM_DECL:
644         case CONST_DECL:
645         case RESULT_DECL:
646         case SSA_NAME:
647           temp.op0 = ref;
648           break;
649         case ADDR_EXPR:
650           if (is_gimple_min_invariant (ref))
651             {
652               temp.op0 = ref;
653               break;
654             }
655           /* Fallthrough.  */
656           /* These are only interesting for their operands, their
657              existence, and their type.  They will never be the last
658              ref in the chain of references (IE they require an
659              operand), so we don't have to put anything
660              for op* as it will be handled by the iteration  */
661         case IMAGPART_EXPR:
662         case REALPART_EXPR:
663         case VIEW_CONVERT_EXPR:
664           break;
665         default:
666           gcc_unreachable ();
667         }
668       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
669
670       if (REFERENCE_CLASS_P (ref)
671           || (TREE_CODE (ref) == ADDR_EXPR
672               && !is_gimple_min_invariant (ref)))
673         ref = TREE_OPERAND (ref, 0);
674       else
675         ref = NULL_TREE;
676     }
677 }
678
679 /* Copy the operations present in load/store/call REF into RESULT, a vector of
680    vn_reference_op_s's.  */
681
682 void
683 copy_reference_ops_from_call (gimple call,
684                               VEC(vn_reference_op_s, heap) **result)
685 {
686   vn_reference_op_s temp;
687   unsigned i;
688
689   /* Copy the type, opcode, function being called and static chain.  */
690   memset (&temp, 0, sizeof (temp));
691   temp.type = gimple_call_return_type (call);
692   temp.opcode = CALL_EXPR;
693   temp.op0 = gimple_call_fn (call);
694   temp.op1 = gimple_call_chain (call);
695   VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
696
697   /* Copy the call arguments.  As they can be references as well,
698      just chain them together.  */
699   for (i = 0; i < gimple_call_num_args (call); ++i)
700     {
701       tree callarg = gimple_call_arg (call, i);
702       copy_reference_ops_from_ref (callarg, result);
703     }
704 }
705
706 /* Create a vector of vn_reference_op_s structures from REF, a
707    REFERENCE_CLASS_P tree.  The vector is not shared. */
708
709 static VEC(vn_reference_op_s, heap) *
710 create_reference_ops_from_ref (tree ref)
711 {
712   VEC (vn_reference_op_s, heap) *result = NULL;
713
714   copy_reference_ops_from_ref (ref, &result);
715   return result;
716 }
717
718 /* Create a vector of vn_reference_op_s structures from CALL, a
719    call statement.  The vector is not shared.  */
720
721 static VEC(vn_reference_op_s, heap) *
722 create_reference_ops_from_call (gimple call)
723 {
724   VEC (vn_reference_op_s, heap) *result = NULL;
725
726   copy_reference_ops_from_call (call, &result);
727   return result;
728 }
729
730 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
731
732 /* Create a vector of vn_reference_op_s structures from REF, a
733    REFERENCE_CLASS_P tree.  The vector is shared among all callers of
734    this function.  */
735
736 static VEC(vn_reference_op_s, heap) *
737 shared_reference_ops_from_ref (tree ref)
738 {
739   if (!ref)
740     return NULL;
741   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
742   copy_reference_ops_from_ref (ref, &shared_lookup_references);
743   return shared_lookup_references;
744 }
745
746 /* Create a vector of vn_reference_op_s structures from CALL, a
747    call statement.  The vector is shared among all callers of
748    this function.  */
749
750 static VEC(vn_reference_op_s, heap) *
751 shared_reference_ops_from_call (gimple call)
752 {
753   if (!call)
754     return NULL;
755   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
756   copy_reference_ops_from_call (call, &shared_lookup_references);
757   return shared_lookup_references;
758 }
759
760
761 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
762    structures into their value numbers.  This is done in-place, and
763    the vector passed in is returned.  */
764
765 static VEC (vn_reference_op_s, heap) *
766 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
767 {
768   vn_reference_op_t vro;
769   int i;
770
771   for (i = 0; VEC_iterate (vn_reference_op_s, orig, i, vro); i++)
772     {
773       if (vro->opcode == SSA_NAME
774           || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
775         {
776           vro->op0 = SSA_VAL (vro->op0);
777           /* If it transforms from an SSA_NAME to a constant, update
778              the opcode.  */
779           if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
780             vro->opcode = TREE_CODE (vro->op0);
781         }
782       /* TODO: Do we want to valueize op2 and op1 of
783          ARRAY_REF/COMPONENT_REF for Ada */
784       
785     }
786
787   return orig;
788 }
789
790 /* Transform any SSA_NAME's in ORIG, a vector of vuse trees, into
791    their value numbers. This is done in-place, and the vector passed
792    in is returned.  */
793
794 static VEC (tree, gc) *
795 valueize_vuses (VEC (tree, gc) *orig)
796 {
797   bool made_replacement = false;
798   tree vuse;
799   int i;
800
801   for (i = 0; VEC_iterate (tree, orig, i, vuse); i++)
802     {
803       if (vuse != SSA_VAL (vuse))
804         {
805           made_replacement = true;
806           VEC_replace (tree, orig, i, SSA_VAL (vuse));
807         }
808     }
809
810   if (made_replacement && VEC_length (tree, orig) > 1)
811     sort_vuses (orig);
812
813   return orig;
814 }
815
816 /* Return the single reference statement defining all virtual uses
817    in VUSES or NULL_TREE, if there are multiple defining statements.
818    Take into account only definitions that alias REF if following
819    back-edges.  */
820
821 static gimple
822 get_def_ref_stmt_vuses (tree ref, VEC (tree, gc) *vuses)
823 {
824   gimple def_stmt;
825   tree vuse;
826   unsigned int i;
827
828   gcc_assert (VEC_length (tree, vuses) >= 1);
829
830   def_stmt = SSA_NAME_DEF_STMT (VEC_index (tree, vuses, 0));
831   if (gimple_code (def_stmt) == GIMPLE_PHI)
832     {
833       /* We can only handle lookups over PHI nodes for a single
834          virtual operand.  */
835       if (VEC_length (tree, vuses) == 1)
836         {
837           def_stmt = get_single_def_stmt_from_phi (ref, def_stmt);
838           goto cont;
839         }
840       else
841         return NULL;
842     }
843
844   /* Verify each VUSE reaches the same defining stmt.  */
845   for (i = 1; VEC_iterate (tree, vuses, i, vuse); ++i)
846     {
847       gimple tmp = SSA_NAME_DEF_STMT (vuse);
848       if (tmp != def_stmt)
849         return NULL;
850     }
851
852   /* Now see if the definition aliases ref, and loop until it does.  */
853 cont:
854   while (def_stmt
855          && is_gimple_assign (def_stmt)
856          && !refs_may_alias_p (ref, gimple_get_lhs (def_stmt)))
857     def_stmt = get_single_def_stmt_with_phi (ref, def_stmt);
858
859   return def_stmt;
860 }
861
862 /* Lookup a SCCVN reference operation VR in the current hash table.
863    Returns the resulting value number if it exists in the hash table,
864    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
865    vn_reference_t stored in the hashtable if something is found.  */
866
867 static tree
868 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
869 {
870   void **slot;
871   hashval_t hash;
872
873   hash = vr->hashcode;
874   slot = htab_find_slot_with_hash (current_info->references, vr,
875                                    hash, NO_INSERT);
876   if (!slot && current_info == optimistic_info)
877     slot = htab_find_slot_with_hash (valid_info->references, vr,
878                                      hash, NO_INSERT);
879   if (slot)
880     {
881       if (vnresult)
882         *vnresult = (vn_reference_t)*slot;
883       return ((vn_reference_t)*slot)->result;
884     }
885   
886   return NULL_TREE;
887 }
888
889
890 /* Lookup a reference operation by it's parts, in the current hash table.
891    Returns the resulting value number if it exists in the hash table,
892    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
893    vn_reference_t stored in the hashtable if something is found.  */
894
895 tree
896 vn_reference_lookup_pieces (VEC (tree, gc) *vuses,
897                             VEC (vn_reference_op_s, heap) *operands,
898                             vn_reference_t *vnresult) 
899 {
900   struct vn_reference_s vr1;
901   tree result;
902   if (vnresult)
903     *vnresult = NULL;
904   
905   vr1.vuses = valueize_vuses (vuses);
906   vr1.operands = valueize_refs (operands);
907   vr1.hashcode = vn_reference_compute_hash (&vr1);
908   result = vn_reference_lookup_1 (&vr1, vnresult);
909
910   return result;
911 }
912
913 /* Lookup OP in the current hash table, and return the resulting value
914    number if it exists in the hash table.  Return NULL_TREE if it does
915    not exist in the hash table or if the result field of the structure
916    was NULL..  VNRESULT will be filled in with the vn_reference_t
917    stored in the hashtable if one exists.  */
918
919 tree
920 vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk,
921                      vn_reference_t *vnresult)
922 {
923   struct vn_reference_s vr1;
924   tree result;
925   gimple def_stmt;
926   if (vnresult)
927     *vnresult = NULL;
928
929   vr1.vuses = valueize_vuses (vuses);
930   vr1.operands = valueize_refs (shared_reference_ops_from_ref (op));
931   vr1.hashcode = vn_reference_compute_hash (&vr1);
932   result = vn_reference_lookup_1 (&vr1, vnresult);
933
934   /* If there is a single defining statement for all virtual uses, we can
935      use that, following virtual use-def chains.  */
936   if (!result
937       && maywalk
938       && vr1.vuses
939       && VEC_length (tree, vr1.vuses) >= 1
940       && (def_stmt = get_def_ref_stmt_vuses (op, vr1.vuses))
941       && is_gimple_assign (def_stmt))
942     {
943       /* We are now at an aliasing definition for the vuses we want to
944          look up.  Re-do the lookup with the vdefs for this stmt.  */
945       vdefs_to_vec (def_stmt, &vuses);
946       vr1.vuses = valueize_vuses (vuses);
947       vr1.hashcode = vn_reference_compute_hash (&vr1);
948       result = vn_reference_lookup_1 (&vr1, vnresult);
949     }
950
951   return result;
952 }
953
954
955 /* Insert OP into the current hash table with a value number of
956    RESULT, and return the resulting reference structure we created.  */
957
958 vn_reference_t
959 vn_reference_insert (tree op, tree result, VEC (tree, gc) *vuses)
960 {
961   void **slot;
962   vn_reference_t vr1;
963
964   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
965   if (TREE_CODE (result) == SSA_NAME)
966     vr1->value_id = VN_INFO (result)->value_id;
967   else
968     vr1->value_id = get_or_alloc_constant_value_id (result);
969   vr1->vuses = valueize_vuses (vuses);
970   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
971   vr1->hashcode = vn_reference_compute_hash (vr1);
972   vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
973
974   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
975                                    INSERT);
976
977   /* Because we lookup stores using vuses, and value number failures
978      using the vdefs (see visit_reference_op_store for how and why),
979      it's possible that on failure we may try to insert an already
980      inserted store.  This is not wrong, there is no ssa name for a
981      store that we could use as a differentiator anyway.  Thus, unlike
982      the other lookup functions, you cannot gcc_assert (!*slot)
983      here.  */
984
985   /* But free the old slot in case of a collision.  */
986   if (*slot)
987     free_reference (*slot);
988
989   *slot = vr1;
990   return vr1;
991 }
992
993 /* Insert a reference by it's pieces into the current hash table with
994    a value number of RESULT.  Return the resulting reference
995    structure we created.  */
996
997 vn_reference_t
998 vn_reference_insert_pieces (VEC (tree, gc) *vuses,
999                             VEC (vn_reference_op_s, heap) *operands,
1000                             tree result, unsigned int value_id)
1001
1002 {
1003   void **slot;
1004   vn_reference_t vr1;
1005
1006   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1007   vr1->value_id =  value_id;
1008   vr1->vuses = valueize_vuses (vuses);
1009   vr1->operands = valueize_refs (operands);
1010   vr1->hashcode = vn_reference_compute_hash (vr1);
1011   if (result && TREE_CODE (result) == SSA_NAME)
1012     result = SSA_VAL (result);
1013   vr1->result = result;
1014
1015   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1016                                    INSERT);
1017   
1018   /* At this point we should have all the things inserted that we have
1019   seen before, and we should never try inserting something that
1020   already exists.  */
1021   gcc_assert (!*slot);
1022   if (*slot)
1023     free_reference (*slot);
1024
1025   *slot = vr1;
1026   return vr1;
1027 }
1028
1029 /* Compute and return the hash value for nary operation VBO1.  */
1030
1031 inline hashval_t
1032 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
1033 {
1034   hashval_t hash = 0;
1035   unsigned i;
1036
1037   for (i = 0; i < vno1->length; ++i)
1038     if (TREE_CODE (vno1->op[i]) == SSA_NAME)
1039       vno1->op[i] = SSA_VAL (vno1->op[i]);
1040
1041   if (vno1->length == 2
1042       && commutative_tree_code (vno1->opcode)
1043       && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
1044     {
1045       tree temp = vno1->op[0];
1046       vno1->op[0] = vno1->op[1];
1047       vno1->op[1] = temp;
1048     }
1049
1050   for (i = 0; i < vno1->length; ++i)
1051     hash += iterative_hash_expr (vno1->op[i], vno1->opcode);
1052
1053   return hash;
1054 }
1055
1056 /* Return the computed hashcode for nary operation P1.  */
1057
1058 static hashval_t
1059 vn_nary_op_hash (const void *p1)
1060 {
1061   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1062   return vno1->hashcode;
1063 }
1064
1065 /* Compare nary operations P1 and P2 and return true if they are
1066    equivalent.  */
1067
1068 int
1069 vn_nary_op_eq (const void *p1, const void *p2)
1070 {
1071   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1072   const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
1073   unsigned i;
1074
1075   if (vno1->opcode != vno2->opcode
1076       || vno1->type != vno2->type)
1077     return false;
1078
1079   for (i = 0; i < vno1->length; ++i)
1080     if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
1081       return false;
1082
1083   return true;
1084 }
1085
1086 /* Lookup a n-ary operation by its pieces and return the resulting value
1087    number if it exists in the hash table.  Return NULL_TREE if it does
1088    not exist in the hash table or if the result field of the operation
1089    is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1090    if it exists.  */
1091
1092 tree
1093 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
1094                           tree type, tree op0, tree op1, tree op2,
1095                           tree op3, vn_nary_op_t *vnresult) 
1096 {
1097   void **slot;
1098   struct vn_nary_op_s vno1;
1099   if (vnresult)
1100     *vnresult = NULL;
1101   vno1.opcode = code;
1102   vno1.length = length;
1103   vno1.type = type;
1104   vno1.op[0] = op0;
1105   vno1.op[1] = op1;
1106   vno1.op[2] = op2;
1107   vno1.op[3] = op3;
1108   vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1109   slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1110                                    NO_INSERT);
1111   if (!slot && current_info == optimistic_info)
1112     slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1113                                      NO_INSERT);
1114   if (!slot)
1115     return NULL_TREE;
1116   if (vnresult)
1117     *vnresult = (vn_nary_op_t)*slot;
1118   return ((vn_nary_op_t)*slot)->result;
1119 }
1120
1121 /* Lookup OP in the current hash table, and return the resulting value
1122    number if it exists in the hash table.  Return NULL_TREE if it does
1123    not exist in the hash table or if the result field of the operation
1124    is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1125    if it exists.  */
1126
1127 tree
1128 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
1129 {
1130   void **slot;
1131   struct vn_nary_op_s vno1;
1132   unsigned i;
1133
1134   if (vnresult)
1135     *vnresult = NULL;
1136   vno1.opcode = TREE_CODE (op);
1137   vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
1138   vno1.type = TREE_TYPE (op);
1139   for (i = 0; i < vno1.length; ++i)
1140     vno1.op[i] = TREE_OPERAND (op, i);
1141   vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1142   slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1143                                    NO_INSERT);
1144   if (!slot && current_info == optimistic_info)
1145     slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1146                                      NO_INSERT);
1147   if (!slot)
1148     return NULL_TREE;
1149   if (vnresult)
1150     *vnresult = (vn_nary_op_t)*slot;
1151   return ((vn_nary_op_t)*slot)->result;
1152 }
1153
1154 /* Lookup the rhs of STMT in the current hash table, and return the resulting
1155    value number if it exists in the hash table.  Return NULL_TREE if
1156    it does not exist in the hash table.  VNRESULT will contain the
1157    vn_nary_op_t from the hashtable if it exists.  */
1158
1159 tree
1160 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
1161 {
1162   void **slot;
1163   struct vn_nary_op_s vno1;
1164   unsigned i;
1165
1166   if (vnresult)
1167     *vnresult = NULL;
1168   vno1.opcode = gimple_assign_rhs_code (stmt);
1169   vno1.length = gimple_num_ops (stmt) - 1;
1170   vno1.type = TREE_TYPE (gimple_assign_lhs (stmt));
1171   for (i = 0; i < vno1.length; ++i)
1172     vno1.op[i] = gimple_op (stmt, i + 1);
1173   vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1174   slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1175                                    NO_INSERT);
1176   if (!slot && current_info == optimistic_info)
1177     slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1178                                      NO_INSERT);
1179   if (!slot)
1180     return NULL_TREE;
1181   if (vnresult)
1182     *vnresult = (vn_nary_op_t)*slot;
1183   return ((vn_nary_op_t)*slot)->result;
1184 }
1185
1186 /* Insert a n-ary operation into the current hash table using it's
1187    pieces.  Return the vn_nary_op_t structure we created and put in
1188    the hashtable.  */
1189
1190 vn_nary_op_t
1191 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
1192                           tree type, tree op0,
1193                           tree op1, tree op2, tree op3,
1194                           tree result,
1195                           unsigned int value_id) 
1196 {
1197   void **slot;
1198   vn_nary_op_t vno1;
1199
1200   vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1201                                        (sizeof (struct vn_nary_op_s)
1202                                         - sizeof (tree) * (4 - length)));
1203   vno1->value_id = value_id;
1204   vno1->opcode = code;
1205   vno1->length = length;
1206   vno1->type = type;
1207   if (length >= 1)
1208     vno1->op[0] = op0;
1209   if (length >= 2)
1210     vno1->op[1] = op1;
1211   if (length >= 3)
1212     vno1->op[2] = op2;
1213   if (length >= 4)
1214     vno1->op[3] = op3;
1215   vno1->result = result;
1216   vno1->hashcode = vn_nary_op_compute_hash (vno1);
1217   slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1218                                    INSERT);
1219   gcc_assert (!*slot);
1220
1221   *slot = vno1;
1222   return vno1;
1223   
1224 }
1225
1226 /* Insert OP into the current hash table with a value number of
1227    RESULT.  Return the vn_nary_op_t structure we created and put in
1228    the hashtable.  */
1229
1230 vn_nary_op_t
1231 vn_nary_op_insert (tree op, tree result)
1232 {
1233   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
1234   void **slot;
1235   vn_nary_op_t vno1;
1236   unsigned i;
1237
1238   vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1239                         (sizeof (struct vn_nary_op_s)
1240                          - sizeof (tree) * (4 - length)));
1241   vno1->value_id = VN_INFO (result)->value_id;
1242   vno1->opcode = TREE_CODE (op);
1243   vno1->length = length;
1244   vno1->type = TREE_TYPE (op);
1245   for (i = 0; i < vno1->length; ++i)
1246     vno1->op[i] = TREE_OPERAND (op, i);
1247   vno1->result = result;
1248   vno1->hashcode = vn_nary_op_compute_hash (vno1);
1249   slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1250                                    INSERT);
1251   gcc_assert (!*slot);
1252
1253   *slot = vno1;
1254   return vno1;
1255 }
1256
1257 /* Insert the rhs of STMT into the current hash table with a value number of
1258    RESULT.  */
1259
1260 vn_nary_op_t
1261 vn_nary_op_insert_stmt (gimple stmt, tree result)
1262 {
1263   unsigned length = gimple_num_ops (stmt) - 1;
1264   void **slot;
1265   vn_nary_op_t vno1;
1266   unsigned i;
1267
1268   vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1269                                        (sizeof (struct vn_nary_op_s)
1270                                         - sizeof (tree) * (4 - length)));
1271   vno1->value_id = VN_INFO (result)->value_id;
1272   vno1->opcode = gimple_assign_rhs_code (stmt);
1273   vno1->length = length;
1274   vno1->type = TREE_TYPE (gimple_assign_lhs (stmt));
1275   for (i = 0; i < vno1->length; ++i)
1276     vno1->op[i] = gimple_op (stmt, i + 1);
1277   vno1->result = result;
1278   vno1->hashcode = vn_nary_op_compute_hash (vno1);
1279   slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1280                                    INSERT);
1281   gcc_assert (!*slot);
1282
1283   *slot = vno1;
1284   return vno1;
1285 }
1286
1287 /* Compute a hashcode for PHI operation VP1 and return it.  */
1288
1289 static inline hashval_t
1290 vn_phi_compute_hash (vn_phi_t vp1)
1291 {
1292   hashval_t result = 0;
1293   int i;
1294   tree phi1op;
1295
1296   result = vp1->block->index;
1297
1298   for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
1299     {
1300       if (phi1op == VN_TOP)
1301         continue;
1302       result += iterative_hash_expr (phi1op, result);
1303     }
1304
1305   return result;
1306 }
1307
1308 /* Return the computed hashcode for phi operation P1.  */
1309
1310 static hashval_t
1311 vn_phi_hash (const void *p1)
1312 {
1313   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1314   return vp1->hashcode;
1315 }
1316
1317 /* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
1318
1319 static int
1320 vn_phi_eq (const void *p1, const void *p2)
1321 {
1322   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1323   const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
1324
1325   if (vp1->block == vp2->block)
1326     {
1327       int i;
1328       tree phi1op;
1329
1330       /* Any phi in the same block will have it's arguments in the
1331          same edge order, because of how we store phi nodes.  */
1332       for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
1333         {
1334           tree phi2op = VEC_index (tree, vp2->phiargs, i);
1335           if (phi1op == VN_TOP || phi2op == VN_TOP)
1336             continue;
1337           if (!expressions_equal_p (phi1op, phi2op))
1338             return false;
1339         }
1340       return true;
1341     }
1342   return false;
1343 }
1344
1345 static VEC(tree, heap) *shared_lookup_phiargs;
1346
1347 /* Lookup PHI in the current hash table, and return the resulting
1348    value number if it exists in the hash table.  Return NULL_TREE if
1349    it does not exist in the hash table. */
1350
1351 static tree
1352 vn_phi_lookup (gimple phi)
1353 {
1354   void **slot;
1355   struct vn_phi_s vp1;
1356   unsigned i;
1357
1358   VEC_truncate (tree, shared_lookup_phiargs, 0);
1359
1360   /* Canonicalize the SSA_NAME's to their value number.  */
1361   for (i = 0; i < gimple_phi_num_args (phi); i++)
1362     {
1363       tree def = PHI_ARG_DEF (phi, i);
1364       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1365       VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
1366     }
1367   vp1.phiargs = shared_lookup_phiargs;
1368   vp1.block = gimple_bb (phi);
1369   vp1.hashcode = vn_phi_compute_hash (&vp1);
1370   slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
1371                                    NO_INSERT);
1372   if (!slot && current_info == optimistic_info)
1373     slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
1374                                      NO_INSERT);
1375   if (!slot)
1376     return NULL_TREE;
1377   return ((vn_phi_t)*slot)->result;
1378 }
1379
1380 /* Insert PHI into the current hash table with a value number of
1381    RESULT.  */
1382
1383 static vn_phi_t
1384 vn_phi_insert (gimple phi, tree result)
1385 {
1386   void **slot;
1387   vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
1388   unsigned i;
1389   VEC (tree, heap) *args = NULL;
1390
1391   /* Canonicalize the SSA_NAME's to their value number.  */
1392   for (i = 0; i < gimple_phi_num_args (phi); i++)
1393     {
1394       tree def = PHI_ARG_DEF (phi, i);
1395       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1396       VEC_safe_push (tree, heap, args, def);
1397     }
1398   vp1->value_id = VN_INFO (result)->value_id;
1399   vp1->phiargs = args;
1400   vp1->block = gimple_bb (phi);
1401   vp1->result = result;
1402   vp1->hashcode = vn_phi_compute_hash (vp1);
1403
1404   slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
1405                                    INSERT);
1406
1407   /* Because we iterate over phi operations more than once, it's
1408      possible the slot might already exist here, hence no assert.*/
1409   *slot = vp1;
1410   return vp1;
1411 }
1412
1413
1414 /* Print set of components in strongly connected component SCC to OUT. */
1415
1416 static void
1417 print_scc (FILE *out, VEC (tree, heap) *scc)
1418 {
1419   tree var;
1420   unsigned int i;
1421
1422   fprintf (out, "SCC consists of: ");
1423   for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1424     {
1425       print_generic_expr (out, var, 0);
1426       fprintf (out, " ");
1427     }
1428   fprintf (out, "\n");
1429 }
1430
1431 /* Set the value number of FROM to TO, return true if it has changed
1432    as a result.  */
1433
1434 static inline bool
1435 set_ssa_val_to (tree from, tree to)
1436 {
1437   tree currval;
1438
1439   if (from != to
1440       && TREE_CODE (to) == SSA_NAME
1441       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
1442     to = from;
1443
1444   /* The only thing we allow as value numbers are VN_TOP, ssa_names
1445      and invariants.  So assert that here.  */
1446   gcc_assert (to != NULL_TREE
1447               && (to == VN_TOP
1448                   || TREE_CODE (to) == SSA_NAME
1449                   || is_gimple_min_invariant (to)));
1450
1451   if (dump_file && (dump_flags & TDF_DETAILS))
1452     {
1453       fprintf (dump_file, "Setting value number of ");
1454       print_generic_expr (dump_file, from, 0);
1455       fprintf (dump_file, " to ");
1456       print_generic_expr (dump_file, to, 0);
1457       fprintf (dump_file, "\n");
1458     }
1459
1460   currval = SSA_VAL (from);
1461
1462   if (currval != to  && !operand_equal_p (currval, to, OEP_PURE_SAME))
1463     {
1464       SSA_VAL (from) = to;
1465       return true;
1466     }
1467   return false;
1468 }
1469
1470 /* Set all definitions in STMT to value number to themselves.
1471    Return true if a value number changed. */
1472
1473 static bool
1474 defs_to_varying (gimple stmt)
1475 {
1476   bool changed = false;
1477   ssa_op_iter iter;
1478   def_operand_p defp;
1479
1480   FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
1481     {
1482       tree def = DEF_FROM_PTR (defp);
1483
1484       VN_INFO (def)->use_processed = true;
1485       changed |= set_ssa_val_to (def, def);
1486     }
1487   return changed;
1488 }
1489
1490 static bool expr_has_constants (tree expr);
1491 static tree try_to_simplify (gimple stmt);
1492
1493 /* Visit a copy between LHS and RHS, return true if the value number
1494    changed.  */
1495
1496 static bool
1497 visit_copy (tree lhs, tree rhs)
1498 {
1499   /* Follow chains of copies to their destination.  */
1500   while (SSA_VAL (rhs) != rhs && TREE_CODE (SSA_VAL (rhs)) == SSA_NAME)
1501     rhs = SSA_VAL (rhs);
1502
1503   /* The copy may have a more interesting constant filled expression
1504      (we don't, since we know our RHS is just an SSA name).  */
1505   VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
1506   VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
1507
1508   return set_ssa_val_to (lhs, rhs);
1509 }
1510
1511 /* Visit a unary operator RHS, value number it, and return true if the
1512    value number of LHS has changed as a result.  */
1513
1514 static bool
1515 visit_unary_op (tree lhs, gimple stmt)
1516 {
1517   bool changed = false;
1518   tree result = vn_nary_op_lookup_stmt (stmt, NULL);
1519
1520   if (result)
1521     {
1522       changed = set_ssa_val_to (lhs, result);
1523     }
1524   else
1525     {
1526       changed = set_ssa_val_to (lhs, lhs);
1527       vn_nary_op_insert_stmt (stmt, lhs);
1528     }
1529
1530   return changed;
1531 }
1532
1533 /* Visit a binary operator RHS, value number it, and return true if the
1534    value number of LHS has changed as a result.  */
1535
1536 static bool
1537 visit_binary_op (tree lhs, gimple stmt)
1538 {
1539   bool changed = false;
1540   tree result = vn_nary_op_lookup_stmt (stmt, NULL);
1541
1542   if (result)
1543     {
1544       changed = set_ssa_val_to (lhs, result);
1545     }
1546   else
1547     {
1548       changed = set_ssa_val_to (lhs, lhs);
1549       vn_nary_op_insert_stmt (stmt, lhs);
1550     }
1551
1552   return changed;
1553 }
1554
1555 /* Visit a call STMT storing into LHS.  Return true if the value number
1556    of the LHS has changed as a result.  */
1557
1558 static bool
1559 visit_reference_op_call (tree lhs, gimple stmt)
1560 {
1561   bool changed = false;
1562   struct vn_reference_s vr1;
1563   tree result;
1564
1565   vr1.vuses = valueize_vuses (shared_vuses_from_stmt (stmt));
1566   vr1.operands = valueize_refs (shared_reference_ops_from_call (stmt));
1567   vr1.hashcode = vn_reference_compute_hash (&vr1);
1568   result = vn_reference_lookup_1 (&vr1, NULL);
1569   if (result)
1570     {
1571       changed = set_ssa_val_to (lhs, result);
1572       if (TREE_CODE (result) == SSA_NAME
1573           && VN_INFO (result)->has_constants)
1574         VN_INFO (lhs)->has_constants = true;
1575     }
1576   else
1577     {
1578       void **slot;
1579       vn_reference_t vr2;
1580       changed = set_ssa_val_to (lhs, lhs);
1581       vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
1582       vr2->vuses = valueize_vuses (copy_vuses_from_stmt (stmt));
1583       vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
1584       vr2->hashcode = vr1.hashcode;
1585       vr2->result = lhs;
1586       slot = htab_find_slot_with_hash (current_info->references,
1587                                        vr2, vr2->hashcode, INSERT);
1588       if (*slot)
1589         free_reference (*slot);
1590       *slot = vr2;
1591     }
1592
1593   return changed;
1594 }
1595
1596 /* Visit a load from a reference operator RHS, part of STMT, value number it,
1597    and return true if the value number of the LHS has changed as a result.  */
1598
1599 static bool
1600 visit_reference_op_load (tree lhs, tree op, gimple stmt)
1601 {
1602   bool changed = false;
1603   tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true,
1604                                      NULL);
1605
1606   /* We handle type-punning through unions by value-numbering based
1607      on offset and size of the access.  Be prepared to handle a
1608      type-mismatch here via creating a VIEW_CONVERT_EXPR.  */
1609   if (result
1610       && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
1611     {
1612       /* We will be setting the value number of lhs to the value number
1613          of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
1614          So first simplify and lookup this expression to see if it
1615          is already available.  */
1616       tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
1617       if (stmt
1618           && !is_gimple_min_invariant (val)
1619           && TREE_CODE (val) != SSA_NAME)
1620         {
1621           tree tem = try_to_simplify (stmt);
1622           if (tem)
1623             val = tem;
1624         }
1625       result = val;
1626       if (!is_gimple_min_invariant (val)
1627           && TREE_CODE (val) != SSA_NAME)
1628         result = vn_nary_op_lookup (val, NULL);
1629       /* If the expression is not yet available, value-number lhs to
1630          a new SSA_NAME we create.  */
1631       if (!result && may_insert)
1632         {
1633           result = make_ssa_name (SSA_NAME_VAR (lhs), NULL);
1634           /* Initialize value-number information properly.  */
1635           VN_INFO_GET (result)->valnum = result;
1636           VN_INFO (result)->value_id = get_next_value_id ();
1637           VN_INFO (result)->expr = val;
1638           VN_INFO (result)->has_constants = expr_has_constants (val);
1639           VN_INFO (result)->needs_insertion = true;
1640           /* As all "inserted" statements are singleton SCCs, insert
1641              to the valid table.  This is strictly needed to
1642              avoid re-generating new value SSA_NAMEs for the same
1643              expression during SCC iteration over and over (the
1644              optimistic table gets cleared after each iteration).
1645              We do not need to insert into the optimistic table, as
1646              lookups there will fall back to the valid table.  */
1647           if (current_info == optimistic_info)
1648             {
1649               current_info = valid_info;
1650               vn_nary_op_insert (val, result);
1651               current_info = optimistic_info;
1652             }
1653           else
1654             vn_nary_op_insert (val, result);
1655           if (dump_file && (dump_flags & TDF_DETAILS))
1656             {
1657               fprintf (dump_file, "Inserting name ");
1658               print_generic_expr (dump_file, result, 0);
1659               fprintf (dump_file, " for expression ");
1660               print_generic_expr (dump_file, val, 0);
1661               fprintf (dump_file, "\n");
1662             }
1663         }
1664     }
1665
1666   if (result)
1667     {
1668       changed = set_ssa_val_to (lhs, result);
1669       if (TREE_CODE (result) == SSA_NAME
1670           && VN_INFO (result)->has_constants)
1671         {
1672           VN_INFO (lhs)->expr = VN_INFO (result)->expr;
1673           VN_INFO (lhs)->has_constants = true;
1674         }
1675     }
1676   else
1677     {
1678       changed = set_ssa_val_to (lhs, lhs);
1679       vn_reference_insert (op, lhs, copy_vuses_from_stmt (stmt));
1680     }
1681
1682   return changed;
1683 }
1684
1685
1686 /* Visit a store to a reference operator LHS, part of STMT, value number it,
1687    and return true if the value number of the LHS has changed as a result.  */
1688
1689 static bool
1690 visit_reference_op_store (tree lhs, tree op, gimple stmt)
1691 {
1692   bool changed = false;
1693   tree result;
1694   bool resultsame = false;
1695
1696   /* First we want to lookup using the *vuses* from the store and see
1697      if there the last store to this location with the same address
1698      had the same value.
1699
1700      The vuses represent the memory state before the store.  If the
1701      memory state, address, and value of the store is the same as the
1702      last store to this location, then this store will produce the
1703      same memory state as that store.
1704
1705      In this case the vdef versions for this store are value numbered to those
1706      vuse versions, since they represent the same memory state after
1707      this store.
1708
1709      Otherwise, the vdefs for the store are used when inserting into
1710      the table, since the store generates a new memory state.  */
1711
1712   result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false,
1713                                 NULL);
1714
1715   if (result)
1716     {
1717       if (TREE_CODE (result) == SSA_NAME)
1718         result = SSA_VAL (result);
1719       if (TREE_CODE (op) == SSA_NAME)
1720         op = SSA_VAL (op);
1721       resultsame = expressions_equal_p (result, op);
1722     }
1723
1724   if (!result || !resultsame)
1725     {
1726       VEC(tree, gc) *vdefs = copy_vdefs_from_stmt (stmt);
1727       int i;
1728       tree vdef;
1729
1730       if (dump_file && (dump_flags & TDF_DETAILS))
1731         {
1732           fprintf (dump_file, "No store match\n");
1733           fprintf (dump_file, "Value numbering store ");
1734           print_generic_expr (dump_file, lhs, 0);
1735           fprintf (dump_file, " to ");
1736           print_generic_expr (dump_file, op, 0);
1737           fprintf (dump_file, "\n");
1738         }
1739       /* Have to set value numbers before insert, since insert is
1740          going to valueize the references in-place.  */
1741       for (i = 0; VEC_iterate (tree, vdefs, i, vdef); i++)
1742         {
1743           VN_INFO (vdef)->use_processed = true;
1744           changed |= set_ssa_val_to (vdef, vdef);
1745         }
1746
1747       /* Do not insert structure copies into the tables.  */
1748       if (is_gimple_min_invariant (op)
1749           || is_gimple_reg (op))
1750         vn_reference_insert (lhs, op, vdefs);
1751     }
1752   else
1753     {
1754       /* We had a match, so value number the vdefs to have the value
1755          number of the vuses they came from.  */
1756       ssa_op_iter op_iter;
1757       def_operand_p var;
1758       vuse_vec_p vv;
1759
1760       if (dump_file && (dump_flags & TDF_DETAILS))
1761         fprintf (dump_file, "Store matched earlier value,"
1762                  "value numbering store vdefs to matching vuses.\n");
1763
1764       FOR_EACH_SSA_VDEF_OPERAND (var, vv, stmt, op_iter)
1765         {
1766           tree def = DEF_FROM_PTR (var);
1767           tree use;
1768
1769           /* Uh, if the vuse is a multiuse, we can't really do much
1770              here, sadly, since we don't know which value number of
1771              which vuse to use.  */
1772           if (VUSE_VECT_NUM_ELEM (*vv) != 1)
1773             use = def;
1774           else
1775             use = VUSE_ELEMENT_VAR (*vv, 0);
1776
1777           VN_INFO (def)->use_processed = true;
1778           changed |= set_ssa_val_to (def, SSA_VAL (use));
1779         }
1780     }
1781
1782   return changed;
1783 }
1784
1785 /* Visit and value number PHI, return true if the value number
1786    changed.  */
1787
1788 static bool
1789 visit_phi (gimple phi)
1790 {
1791   bool changed = false;
1792   tree result;
1793   tree sameval = VN_TOP;
1794   bool allsame = true;
1795   unsigned i;
1796
1797   /* TODO: We could check for this in init_sccvn, and replace this
1798      with a gcc_assert.  */
1799   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
1800     return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1801
1802   /* See if all non-TOP arguments have the same value.  TOP is
1803      equivalent to everything, so we can ignore it.  */
1804   for (i = 0; i < gimple_phi_num_args (phi); i++)
1805     {
1806       tree def = PHI_ARG_DEF (phi, i);
1807
1808       if (TREE_CODE (def) == SSA_NAME)
1809         def = SSA_VAL (def);
1810       if (def == VN_TOP)
1811         continue;
1812       if (sameval == VN_TOP)
1813         {
1814           sameval = def;
1815         }
1816       else
1817         {
1818           if (!expressions_equal_p (def, sameval))
1819             {
1820               allsame = false;
1821               break;
1822             }
1823         }
1824     }
1825
1826   /* If all value numbered to the same value, the phi node has that
1827      value.  */
1828   if (allsame)
1829     {
1830       if (is_gimple_min_invariant (sameval))
1831         {
1832           VN_INFO (PHI_RESULT (phi))->has_constants = true;
1833           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1834         }
1835       else
1836         {
1837           VN_INFO (PHI_RESULT (phi))->has_constants = false;
1838           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1839         }
1840
1841       if (TREE_CODE (sameval) == SSA_NAME)
1842         return visit_copy (PHI_RESULT (phi), sameval);
1843
1844       return set_ssa_val_to (PHI_RESULT (phi), sameval);
1845     }
1846
1847   /* Otherwise, see if it is equivalent to a phi node in this block.  */
1848   result = vn_phi_lookup (phi);
1849   if (result)
1850     {
1851       if (TREE_CODE (result) == SSA_NAME)
1852         changed = visit_copy (PHI_RESULT (phi), result);
1853       else
1854         changed = set_ssa_val_to (PHI_RESULT (phi), result);
1855     }
1856   else
1857     {
1858       vn_phi_insert (phi, PHI_RESULT (phi));
1859       VN_INFO (PHI_RESULT (phi))->has_constants = false;
1860       VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
1861       changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1862     }
1863
1864   return changed;
1865 }
1866
1867 /* Return true if EXPR contains constants.  */
1868
1869 static bool
1870 expr_has_constants (tree expr)
1871 {
1872   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1873     {
1874     case tcc_unary:
1875       return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
1876
1877     case tcc_binary:
1878       return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
1879         || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
1880       /* Constants inside reference ops are rarely interesting, but
1881          it can take a lot of looking to find them.  */
1882     case tcc_reference:
1883     case tcc_declaration:
1884       return false;
1885     default:
1886       return is_gimple_min_invariant (expr);
1887     }
1888   return false;
1889 }
1890
1891 /* Return true if STMT contains constants.  */
1892
1893 static bool
1894 stmt_has_constants (gimple stmt)
1895 {
1896   if (gimple_code (stmt) != GIMPLE_ASSIGN)
1897     return false;
1898
1899   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
1900     {
1901     case GIMPLE_UNARY_RHS:
1902       return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
1903
1904     case GIMPLE_BINARY_RHS:
1905       return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
1906               || is_gimple_min_invariant (gimple_assign_rhs2 (stmt)));
1907     case GIMPLE_SINGLE_RHS:
1908       /* Constants inside reference ops are rarely interesting, but
1909          it can take a lot of looking to find them.  */
1910       return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
1911     default:
1912       gcc_unreachable ();
1913     }
1914   return false;
1915 }
1916
1917 /* Replace SSA_NAMES in expr with their value numbers, and return the
1918    result.
1919    This is performed in place. */
1920
1921 static tree
1922 valueize_expr (tree expr)
1923 {
1924   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1925     {
1926     case tcc_unary:
1927       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1928           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1929         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1930       break;
1931     case tcc_binary:
1932       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1933           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1934         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1935       if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
1936           && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
1937         TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
1938       break;
1939     default:
1940       break;
1941     }
1942   return expr;
1943 }
1944
1945 /* Simplify the binary expression RHS, and return the result if
1946    simplified. */
1947
1948 static tree
1949 simplify_binary_expression (gimple stmt)
1950 {
1951   tree result = NULL_TREE;
1952   tree op0 = gimple_assign_rhs1 (stmt);
1953   tree op1 = gimple_assign_rhs2 (stmt);
1954
1955   /* This will not catch every single case we could combine, but will
1956      catch those with constants.  The goal here is to simultaneously
1957      combine constants between expressions, but avoid infinite
1958      expansion of expressions during simplification.  */
1959   if (TREE_CODE (op0) == SSA_NAME)
1960     {
1961       if (VN_INFO (op0)->has_constants
1962           || TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
1963         op0 = valueize_expr (vn_get_expr_for (op0));
1964       else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
1965         op0 = SSA_VAL (op0);
1966     }
1967
1968   if (TREE_CODE (op1) == SSA_NAME)
1969     {
1970       if (VN_INFO (op1)->has_constants)
1971         op1 = valueize_expr (vn_get_expr_for (op1));
1972       else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
1973         op1 = SSA_VAL (op1);
1974     }
1975
1976   /* Avoid folding if nothing changed.  */
1977   if (op0 == gimple_assign_rhs1 (stmt)
1978       && op1 == gimple_assign_rhs2 (stmt))
1979     return NULL_TREE;
1980
1981   fold_defer_overflow_warnings ();
1982
1983   result = fold_binary (gimple_assign_rhs_code (stmt),
1984                         TREE_TYPE (gimple_get_lhs (stmt)), op0, op1);
1985
1986   fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
1987                                   stmt, 0);
1988
1989   /* Make sure result is not a complex expression consisting
1990      of operators of operators (IE (a + b) + (a + c))
1991      Otherwise, we will end up with unbounded expressions if
1992      fold does anything at all.  */
1993   if (result && valid_gimple_rhs_p (result))
1994     return result;
1995
1996   return NULL_TREE;
1997 }
1998
1999 /* Simplify the unary expression RHS, and return the result if
2000    simplified. */
2001
2002 static tree
2003 simplify_unary_expression (gimple stmt)
2004 {
2005   tree result = NULL_TREE;
2006   tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
2007
2008   /* We handle some tcc_reference codes here that are all
2009      GIMPLE_ASSIGN_SINGLE codes.  */
2010   if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
2011       || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2012       || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2013     op0 = TREE_OPERAND (op0, 0);
2014
2015   if (TREE_CODE (op0) != SSA_NAME)
2016     return NULL_TREE;
2017
2018   orig_op0 = op0;
2019   if (VN_INFO (op0)->has_constants)
2020     op0 = valueize_expr (vn_get_expr_for (op0));
2021   else if (gimple_assign_cast_p (stmt)
2022            || gimple_assign_rhs_code (stmt) == REALPART_EXPR
2023            || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2024            || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2025     {
2026       /* We want to do tree-combining on conversion-like expressions.
2027          Make sure we feed only SSA_NAMEs or constants to fold though.  */
2028       tree tem = valueize_expr (vn_get_expr_for (op0));
2029       if (UNARY_CLASS_P (tem)
2030           || BINARY_CLASS_P (tem)
2031           || TREE_CODE (tem) == VIEW_CONVERT_EXPR
2032           || TREE_CODE (tem) == SSA_NAME
2033           || is_gimple_min_invariant (tem))
2034         op0 = tem;
2035     }
2036
2037   /* Avoid folding if nothing changed, but remember the expression.  */
2038   if (op0 == orig_op0)
2039     return NULL_TREE;
2040
2041   result = fold_unary (gimple_assign_rhs_code (stmt),
2042                        gimple_expr_type (stmt), op0);
2043   if (result)
2044     {
2045       STRIP_USELESS_TYPE_CONVERSION (result);
2046       if (valid_gimple_rhs_p (result))
2047         return result;
2048     }
2049
2050   return NULL_TREE;
2051 }
2052
2053 /* Try to simplify RHS using equivalences and constant folding.  */
2054
2055 static tree
2056 try_to_simplify (gimple stmt)
2057 {
2058   tree tem;
2059
2060   /* For stores we can end up simplifying a SSA_NAME rhs.  Just return
2061      in this case, there is no point in doing extra work.  */
2062   if (gimple_assign_copy_p (stmt)
2063       && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2064     return NULL_TREE;
2065
2066   switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2067     {
2068     case tcc_declaration:
2069       tem = get_symbol_constant_value (gimple_assign_rhs1 (stmt));
2070       if (tem)
2071         return tem;
2072       break;
2073
2074     case tcc_reference:
2075       /* Do not do full-blown reference lookup here, but simplify
2076          reads from constant aggregates.  */
2077       tem = fold_const_aggregate_ref (gimple_assign_rhs1 (stmt));
2078       if (tem)
2079         return tem;
2080
2081       /* Fallthrough for some codes that can operate on registers.  */
2082       if (!(TREE_CODE (gimple_assign_rhs1 (stmt)) == REALPART_EXPR
2083             || TREE_CODE (gimple_assign_rhs1 (stmt)) == IMAGPART_EXPR
2084             || TREE_CODE (gimple_assign_rhs1 (stmt)) == VIEW_CONVERT_EXPR))
2085         break;
2086       /* We could do a little more with unary ops, if they expand
2087          into binary ops, but it's debatable whether it is worth it. */
2088     case tcc_unary:
2089       return simplify_unary_expression (stmt);
2090       break;
2091     case tcc_comparison:
2092     case tcc_binary:
2093       return simplify_binary_expression (stmt);
2094       break;
2095     default:
2096       break;
2097     }
2098
2099   return NULL_TREE;
2100 }
2101
2102 /* Visit and value number USE, return true if the value number
2103    changed. */
2104
2105 static bool
2106 visit_use (tree use)
2107 {
2108   bool changed = false;
2109   gimple stmt = SSA_NAME_DEF_STMT (use);
2110
2111   VN_INFO (use)->use_processed = true;
2112
2113   gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
2114   if (dump_file && (dump_flags & TDF_DETAILS)
2115       && !SSA_NAME_IS_DEFAULT_DEF (use))
2116     {
2117       fprintf (dump_file, "Value numbering ");
2118       print_generic_expr (dump_file, use, 0);
2119       fprintf (dump_file, " stmt = ");
2120       print_gimple_stmt (dump_file, stmt, 0, 0);
2121     }
2122
2123   /* Handle uninitialized uses.  */
2124   if (SSA_NAME_IS_DEFAULT_DEF (use))
2125     changed = set_ssa_val_to (use, use);
2126   else
2127     {
2128       if (gimple_code (stmt) == GIMPLE_PHI)
2129         changed = visit_phi (stmt);
2130       else if (!gimple_has_lhs (stmt)
2131                || gimple_has_volatile_ops (stmt)
2132                || stmt_could_throw_p (stmt))
2133         changed = defs_to_varying (stmt);
2134       else if (is_gimple_assign (stmt))
2135         {
2136           tree lhs = gimple_assign_lhs (stmt);
2137           tree simplified;
2138
2139           /* Shortcut for copies. Simplifying copies is pointless,
2140              since we copy the expression and value they represent.  */
2141           if (gimple_assign_copy_p (stmt)
2142               && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2143               && TREE_CODE (lhs) == SSA_NAME)
2144             {
2145               changed = visit_copy (lhs, gimple_assign_rhs1 (stmt));
2146               goto done;
2147             }
2148           simplified = try_to_simplify (stmt);
2149           if (simplified)
2150             {
2151               if (dump_file && (dump_flags & TDF_DETAILS))
2152                 {
2153                   fprintf (dump_file, "RHS ");
2154                   print_gimple_expr (dump_file, stmt, 0, 0);
2155                   fprintf (dump_file, " simplified to ");
2156                   print_generic_expr (dump_file, simplified, 0);
2157                   if (TREE_CODE (lhs) == SSA_NAME)
2158                     fprintf (dump_file, " has constants %d\n",
2159                              expr_has_constants (simplified));
2160                   else
2161                     fprintf (dump_file, "\n");
2162                 }
2163             }
2164           /* Setting value numbers to constants will occasionally
2165              screw up phi congruence because constants are not
2166              uniquely associated with a single ssa name that can be
2167              looked up.  */
2168           if (simplified
2169               && is_gimple_min_invariant (simplified)
2170               && TREE_CODE (lhs) == SSA_NAME)
2171             {
2172               VN_INFO (lhs)->expr = simplified;
2173               VN_INFO (lhs)->has_constants = true;
2174               changed = set_ssa_val_to (lhs, simplified);
2175               goto done;
2176             }
2177           else if (simplified
2178                    && TREE_CODE (simplified) == SSA_NAME
2179                    && TREE_CODE (lhs) == SSA_NAME)
2180             {
2181               changed = visit_copy (lhs, simplified);
2182               goto done;
2183             }
2184           else if (simplified)
2185             {
2186               if (TREE_CODE (lhs) == SSA_NAME)
2187                 {
2188                   VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
2189                   /* We have to unshare the expression or else
2190                      valuizing may change the IL stream.  */
2191                   VN_INFO (lhs)->expr = unshare_expr (simplified);
2192                 }
2193             }
2194           else if (stmt_has_constants (stmt)
2195                    && TREE_CODE (lhs) == SSA_NAME)
2196             VN_INFO (lhs)->has_constants = true;
2197           else if (TREE_CODE (lhs) == SSA_NAME)
2198             {
2199               /* We reset expr and constantness here because we may
2200                  have been value numbering optimistically, and
2201                  iterating. They may become non-constant in this case,
2202                  even if they were optimistically constant. */
2203
2204               VN_INFO (lhs)->has_constants = false;
2205               VN_INFO (lhs)->expr = NULL_TREE;
2206             }
2207
2208           if (TREE_CODE (lhs) == SSA_NAME
2209               /* We can substitute SSA_NAMEs that are live over
2210                  abnormal edges with their constant value.  */
2211               && !(gimple_assign_copy_p (stmt)
2212                    && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2213               && !(simplified
2214                    && is_gimple_min_invariant (simplified))
2215               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2216             changed = defs_to_varying (stmt);
2217           else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
2218             {
2219               changed = visit_reference_op_store (lhs, gimple_assign_rhs1 (stmt), stmt);
2220             }
2221           else if (TREE_CODE (lhs) == SSA_NAME)
2222             {
2223               if ((gimple_assign_copy_p (stmt)
2224                    && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2225                   || (simplified
2226                       && is_gimple_min_invariant (simplified)))
2227                 {
2228                   VN_INFO (lhs)->has_constants = true;
2229                   if (simplified)
2230                     changed = set_ssa_val_to (lhs, simplified);
2231                   else
2232                     changed = set_ssa_val_to (lhs, gimple_assign_rhs1 (stmt));
2233                 }
2234               else
2235                 {
2236                   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2237                     {
2238                     case GIMPLE_UNARY_RHS:
2239                       changed = visit_unary_op (lhs, stmt);
2240                       break;
2241                     case GIMPLE_BINARY_RHS:
2242                       changed = visit_binary_op (lhs, stmt);
2243                       break;
2244                     case GIMPLE_SINGLE_RHS:
2245                       switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2246                         {
2247                         case tcc_declaration:
2248                         case tcc_reference:
2249                           changed = visit_reference_op_load
2250                               (lhs, gimple_assign_rhs1 (stmt), stmt);
2251                           break;
2252                         case tcc_expression:
2253                           if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
2254                             {
2255                               changed = visit_unary_op (lhs, stmt);
2256                               break;
2257                             }
2258                           /* Fallthrough.  */
2259                         default:
2260                           changed = defs_to_varying (stmt);
2261                         }
2262                       break;
2263                     default:
2264                       changed = defs_to_varying (stmt);
2265                       break;
2266                     }
2267                 }
2268             }
2269           else
2270             changed = defs_to_varying (stmt);
2271         }
2272       else if (is_gimple_call (stmt))
2273         {
2274           tree lhs = gimple_call_lhs (stmt);
2275
2276           /* ???  We could try to simplify calls.  */
2277
2278           if (stmt_has_constants (stmt)
2279               && TREE_CODE (lhs) == SSA_NAME)
2280             VN_INFO (lhs)->has_constants = true;
2281           else if (TREE_CODE (lhs) == SSA_NAME)
2282             {
2283               /* We reset expr and constantness here because we may
2284                  have been value numbering optimistically, and
2285                  iterating. They may become non-constant in this case,
2286                  even if they were optimistically constant. */
2287               VN_INFO (lhs)->has_constants = false;
2288               VN_INFO (lhs)->expr = NULL_TREE;
2289             }
2290
2291           if (TREE_CODE (lhs) == SSA_NAME
2292               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2293             changed = defs_to_varying (stmt);
2294           /* ???  We should handle stores from calls.  */
2295           else if (TREE_CODE (lhs) == SSA_NAME)
2296             {
2297               if (gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST))
2298                 changed = visit_reference_op_call (lhs, stmt);
2299               else
2300                 changed = defs_to_varying (stmt);
2301             }
2302           else
2303             changed = defs_to_varying (stmt);
2304         }
2305     }
2306  done:
2307   return changed;
2308 }
2309
2310 /* Compare two operands by reverse postorder index */
2311
2312 static int
2313 compare_ops (const void *pa, const void *pb)
2314 {
2315   const tree opa = *((const tree *)pa);
2316   const tree opb = *((const tree *)pb);
2317   gimple opstmta = SSA_NAME_DEF_STMT (opa);
2318   gimple opstmtb = SSA_NAME_DEF_STMT (opb);
2319   basic_block bba;
2320   basic_block bbb;
2321
2322   if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
2323     return 0;
2324   else if (gimple_nop_p (opstmta))
2325     return -1;
2326   else if (gimple_nop_p (opstmtb))
2327     return 1;
2328
2329   bba = gimple_bb (opstmta);
2330   bbb = gimple_bb (opstmtb);
2331
2332   if (!bba && !bbb)
2333     return 0;
2334   else if (!bba)
2335     return -1;
2336   else if (!bbb)
2337     return 1;
2338
2339   if (bba == bbb)
2340     {
2341       if (gimple_code (opstmta) == GIMPLE_PHI
2342           && gimple_code (opstmtb) == GIMPLE_PHI)
2343         return 0;
2344       else if (gimple_code (opstmta) == GIMPLE_PHI)
2345         return -1;
2346       else if (gimple_code (opstmtb) == GIMPLE_PHI)
2347         return 1;
2348       return gimple_uid (opstmta) - gimple_uid (opstmtb);
2349     }
2350   return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
2351 }
2352
2353 /* Sort an array containing members of a strongly connected component
2354    SCC so that the members are ordered by RPO number.
2355    This means that when the sort is complete, iterating through the
2356    array will give you the members in RPO order.  */
2357
2358 static void
2359 sort_scc (VEC (tree, heap) *scc)
2360 {
2361   qsort (VEC_address (tree, scc),
2362          VEC_length (tree, scc),
2363          sizeof (tree),
2364          compare_ops);
2365 }
2366
2367 /* Process a strongly connected component in the SSA graph.  */
2368
2369 static void
2370 process_scc (VEC (tree, heap) *scc)
2371 {
2372   /* If the SCC has a single member, just visit it.  */
2373
2374   if (VEC_length (tree, scc) == 1)
2375     {
2376       tree use = VEC_index (tree, scc, 0);
2377       if (!VN_INFO (use)->use_processed)
2378         visit_use (use);
2379     }
2380   else
2381     {
2382       tree var;
2383       unsigned int i;
2384       unsigned int iterations = 0;
2385       bool changed = true;
2386
2387       /* Iterate over the SCC with the optimistic table until it stops
2388          changing.  */
2389       current_info = optimistic_info;
2390       while (changed)
2391         {
2392           changed = false;
2393           iterations++;
2394           /* As we are value-numbering optimistically we have to
2395              clear the expression tables and the simplified expressions
2396              in each iteration until we converge.  */
2397           htab_empty (optimistic_info->nary);
2398           htab_empty (optimistic_info->phis);
2399           htab_empty (optimistic_info->references);
2400           obstack_free (&optimistic_info->nary_obstack, NULL);
2401           gcc_obstack_init (&optimistic_info->nary_obstack);
2402           empty_alloc_pool (optimistic_info->phis_pool);
2403           empty_alloc_pool (optimistic_info->references_pool);
2404           for (i = 0; VEC_iterate (tree, scc, i, var); i++)
2405             VN_INFO (var)->expr = NULL_TREE;
2406           for (i = 0; VEC_iterate (tree, scc, i, var); i++)
2407             changed |= visit_use (var);
2408         }
2409
2410       statistics_histogram_event (cfun, "SCC iterations", iterations);
2411
2412       /* Finally, visit the SCC once using the valid table.  */
2413       current_info = valid_info;
2414       for (i = 0; VEC_iterate (tree, scc, i, var); i++)
2415         visit_use (var);
2416     }
2417 }
2418
2419 DEF_VEC_O(ssa_op_iter);
2420 DEF_VEC_ALLOC_O(ssa_op_iter,heap);
2421
2422 /* Pop the components of the found SCC for NAME off the SCC stack
2423    and process them.  Returns true if all went well, false if
2424    we run into resource limits.  */
2425
2426 static bool
2427 extract_and_process_scc_for_name (tree name)
2428 {
2429   VEC (tree, heap) *scc = NULL;
2430   tree x;
2431
2432   /* Found an SCC, pop the components off the SCC stack and
2433      process them.  */
2434   do
2435     {
2436       x = VEC_pop (tree, sccstack);
2437
2438       VN_INFO (x)->on_sccstack = false;
2439       VEC_safe_push (tree, heap, scc, x);
2440     } while (x != name);
2441
2442   /* Bail out of SCCVN in case a SCC turns out to be incredibly large.  */
2443   if (VEC_length (tree, scc)
2444       > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
2445     {
2446       if (dump_file)
2447         fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
2448                  "SCC size %u exceeding %u\n", VEC_length (tree, scc),
2449                  (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
2450       return false;
2451     }
2452
2453   if (VEC_length (tree, scc) > 1)
2454     sort_scc (scc);
2455
2456   if (dump_file && (dump_flags & TDF_DETAILS))
2457     print_scc (dump_file, scc);
2458
2459   process_scc (scc);
2460
2461   VEC_free (tree, heap, scc);
2462
2463   return true;
2464 }
2465
2466 /* Depth first search on NAME to discover and process SCC's in the SSA
2467    graph.
2468    Execution of this algorithm relies on the fact that the SCC's are
2469    popped off the stack in topological order.
2470    Returns true if successful, false if we stopped processing SCC's due
2471    to resource constraints.  */
2472
2473 static bool
2474 DFS (tree name)
2475 {
2476   VEC(ssa_op_iter, heap) *itervec = NULL;
2477   VEC(tree, heap) *namevec = NULL;
2478   use_operand_p usep = NULL;
2479   gimple defstmt;
2480   tree use;
2481   ssa_op_iter iter;
2482
2483 start_over:
2484   /* SCC info */
2485   VN_INFO (name)->dfsnum = next_dfs_num++;
2486   VN_INFO (name)->visited = true;
2487   VN_INFO (name)->low = VN_INFO (name)->dfsnum;
2488
2489   VEC_safe_push (tree, heap, sccstack, name);
2490   VN_INFO (name)->on_sccstack = true;
2491   defstmt = SSA_NAME_DEF_STMT (name);
2492
2493   /* Recursively DFS on our operands, looking for SCC's.  */
2494   if (!gimple_nop_p (defstmt))
2495     {
2496       /* Push a new iterator.  */
2497       if (gimple_code (defstmt) == GIMPLE_PHI)
2498         usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
2499       else
2500         usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
2501     }
2502   else
2503     iter.done = true;
2504
2505   while (1)
2506     {
2507       /* If we are done processing uses of a name, go up the stack
2508          of iterators and process SCCs as we found them.  */
2509       if (op_iter_done (&iter))
2510         {
2511           /* See if we found an SCC.  */
2512           if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
2513             if (!extract_and_process_scc_for_name (name))
2514               {
2515                 VEC_free (tree, heap, namevec);
2516                 VEC_free (ssa_op_iter, heap, itervec);
2517                 return false;
2518               }
2519
2520           /* Check if we are done.  */
2521           if (VEC_empty (tree, namevec))
2522             {
2523               VEC_free (tree, heap, namevec);
2524               VEC_free (ssa_op_iter, heap, itervec);
2525               return true;
2526             }
2527
2528           /* Restore the last use walker and continue walking there.  */
2529           use = name;
2530           name = VEC_pop (tree, namevec);
2531           memcpy (&iter, VEC_last (ssa_op_iter, itervec),
2532                   sizeof (ssa_op_iter));
2533           VEC_pop (ssa_op_iter, itervec);
2534           goto continue_walking;
2535         }
2536
2537       use = USE_FROM_PTR (usep);
2538
2539       /* Since we handle phi nodes, we will sometimes get
2540          invariants in the use expression.  */
2541       if (TREE_CODE (use) == SSA_NAME)
2542         {
2543           if (! (VN_INFO (use)->visited))
2544             {
2545               /* Recurse by pushing the current use walking state on
2546                  the stack and starting over.  */
2547               VEC_safe_push(ssa_op_iter, heap, itervec, &iter);
2548               VEC_safe_push(tree, heap, namevec, name);
2549               name = use;
2550               goto start_over;
2551
2552 continue_walking:
2553               VN_INFO (name)->low = MIN (VN_INFO (name)->low,
2554                                          VN_INFO (use)->low);
2555             }
2556           if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
2557               && VN_INFO (use)->on_sccstack)
2558             {
2559               VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
2560                                          VN_INFO (name)->low);
2561             }
2562         }
2563
2564       usep = op_iter_next_use (&iter);
2565     }
2566 }
2567
2568 /* Allocate a value number table.  */
2569
2570 static void
2571 allocate_vn_table (vn_tables_t table)
2572 {
2573   table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
2574   table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
2575   table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
2576                                    free_reference);
2577
2578   gcc_obstack_init (&table->nary_obstack);
2579   table->phis_pool = create_alloc_pool ("VN phis",
2580                                         sizeof (struct vn_phi_s),
2581                                         30);
2582   table->references_pool = create_alloc_pool ("VN references",
2583                                               sizeof (struct vn_reference_s),
2584                                               30);
2585 }
2586
2587 /* Free a value number table.  */
2588
2589 static void
2590 free_vn_table (vn_tables_t table)
2591 {
2592   htab_delete (table->phis);
2593   htab_delete (table->nary);
2594   htab_delete (table->references);
2595   obstack_free (&table->nary_obstack, NULL);
2596   free_alloc_pool (table->phis_pool);
2597   free_alloc_pool (table->references_pool);
2598 }
2599
2600 static void
2601 init_scc_vn (void)
2602 {
2603   size_t i;
2604   int j;
2605   int *rpo_numbers_temp;
2606
2607   calculate_dominance_info (CDI_DOMINATORS);
2608   sccstack = NULL;
2609   constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
2610                                   free);
2611   
2612   constant_value_ids = BITMAP_ALLOC (NULL);
2613   
2614   next_dfs_num = 1;
2615   next_value_id = 1;
2616   
2617   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
2618   /* VEC_alloc doesn't actually grow it to the right size, it just
2619      preallocates the space to do so.  */
2620   VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
2621   gcc_obstack_init (&vn_ssa_aux_obstack);
2622
2623   shared_lookup_phiargs = NULL;
2624   shared_lookup_vops = NULL;
2625   shared_lookup_references = NULL;
2626   rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2627   rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2628   pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
2629
2630   /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
2631      the i'th block in RPO order is bb.  We want to map bb's to RPO
2632      numbers, so we need to rearrange this array.  */
2633   for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
2634     rpo_numbers[rpo_numbers_temp[j]] = j;
2635
2636   XDELETE (rpo_numbers_temp);
2637
2638   VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
2639
2640   /* Create the VN_INFO structures, and initialize value numbers to
2641      TOP.  */
2642   for (i = 0; i < num_ssa_names; i++)
2643     {
2644       tree name = ssa_name (i);
2645       if (name)
2646         {
2647           VN_INFO_GET (name)->valnum = VN_TOP;
2648           VN_INFO (name)->expr = NULL_TREE;
2649           VN_INFO (name)->value_id = 0;
2650         }
2651     }
2652
2653   renumber_gimple_stmt_uids ();
2654
2655   /* Create the valid and optimistic value numbering tables.  */
2656   valid_info = XCNEW (struct vn_tables_s);
2657   allocate_vn_table (valid_info);
2658   optimistic_info = XCNEW (struct vn_tables_s);
2659   allocate_vn_table (optimistic_info);
2660 }
2661
2662 void
2663 free_scc_vn (void)
2664 {
2665   size_t i;
2666
2667   htab_delete (constant_to_value_id);
2668   BITMAP_FREE (constant_value_ids);
2669   VEC_free (tree, heap, shared_lookup_phiargs);
2670   VEC_free (tree, gc, shared_lookup_vops);
2671   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
2672   XDELETEVEC (rpo_numbers);
2673
2674   for (i = 0; i < num_ssa_names; i++)
2675     {
2676       tree name = ssa_name (i);
2677       if (name
2678           && VN_INFO (name)->needs_insertion)
2679         release_ssa_name (name);
2680     }
2681   obstack_free (&vn_ssa_aux_obstack, NULL);
2682   VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
2683
2684   VEC_free (tree, heap, sccstack);
2685   free_vn_table (valid_info);
2686   XDELETE (valid_info);
2687   free_vn_table (optimistic_info);
2688   XDELETE (optimistic_info);
2689 }
2690
2691 /* Set the value ids in the valid hash tables.  */
2692
2693 static void
2694 set_hashtable_value_ids (void)
2695 {
2696   htab_iterator hi;
2697   vn_nary_op_t vno;
2698   vn_reference_t vr;
2699   vn_phi_t vp;
2700
2701   /* Now set the value ids of the things we had put in the hash
2702      table.  */
2703
2704   FOR_EACH_HTAB_ELEMENT (valid_info->nary,
2705                          vno, vn_nary_op_t, hi) 
2706     {
2707       if (vno->result)
2708         {
2709           if (TREE_CODE (vno->result) == SSA_NAME)
2710             vno->value_id = VN_INFO (vno->result)->value_id;
2711           else if (is_gimple_min_invariant (vno->result))
2712             vno->value_id = get_or_alloc_constant_value_id (vno->result);
2713         }
2714     }
2715
2716   FOR_EACH_HTAB_ELEMENT (valid_info->phis,
2717                          vp, vn_phi_t, hi) 
2718     {
2719       if (vp->result)
2720         {
2721           if (TREE_CODE (vp->result) == SSA_NAME)
2722             vp->value_id = VN_INFO (vp->result)->value_id;
2723           else if (is_gimple_min_invariant (vp->result))
2724             vp->value_id = get_or_alloc_constant_value_id (vp->result);
2725         }
2726     }
2727
2728   FOR_EACH_HTAB_ELEMENT (valid_info->references,
2729                          vr, vn_reference_t, hi) 
2730     {
2731       if (vr->result)
2732         {
2733           if (TREE_CODE (vr->result) == SSA_NAME)
2734             vr->value_id = VN_INFO (vr->result)->value_id;
2735           else if (is_gimple_min_invariant (vr->result))
2736             vr->value_id = get_or_alloc_constant_value_id (vr->result);
2737         }
2738     }
2739 }
2740
2741 /* Do SCCVN.  Returns true if it finished, false if we bailed out
2742    due to resource constraints.  */
2743
2744 bool
2745 run_scc_vn (bool may_insert_arg)
2746 {
2747   size_t i;
2748   tree param;
2749   bool changed = true;
2750   
2751   may_insert = may_insert_arg;
2752
2753   init_scc_vn ();
2754   current_info = valid_info;
2755
2756   for (param = DECL_ARGUMENTS (current_function_decl);
2757        param;
2758        param = TREE_CHAIN (param))
2759     {
2760       if (gimple_default_def (cfun, param) != NULL)
2761         {
2762           tree def = gimple_default_def (cfun, param);
2763           SSA_VAL (def) = def;
2764         }
2765     }
2766
2767   for (i = 1; i < num_ssa_names; ++i)
2768     {
2769       tree name = ssa_name (i);
2770       if (name
2771           && VN_INFO (name)->visited == false
2772           && !has_zero_uses (name))
2773         if (!DFS (name))
2774           {
2775             free_scc_vn ();
2776             may_insert = false;
2777             return false;
2778           }
2779     }
2780
2781   /* Initialize the value ids.  */
2782       
2783   for (i = 1; i < num_ssa_names; ++i)
2784     {
2785       tree name = ssa_name (i);
2786       vn_ssa_aux_t info;
2787       if (!name)
2788         continue;
2789       info = VN_INFO (name);
2790       if (info->valnum == name)
2791         info->value_id = get_next_value_id ();
2792       else if (is_gimple_min_invariant (info->valnum))
2793         info->value_id = get_or_alloc_constant_value_id (info->valnum);
2794     }
2795   
2796   /* Propagate until they stop changing.  */
2797   while (changed)
2798     {
2799       changed = false;
2800       for (i = 1; i < num_ssa_names; ++i)
2801         {
2802           tree name = ssa_name (i);
2803           vn_ssa_aux_t info;
2804           if (!name)
2805             continue;
2806           info = VN_INFO (name);
2807           if (TREE_CODE (info->valnum) == SSA_NAME
2808               && info->valnum != name
2809               && info->value_id != VN_INFO (info->valnum)->value_id)
2810             {
2811               changed = true;
2812               info->value_id = VN_INFO (info->valnum)->value_id;
2813             }
2814         }
2815     }
2816   
2817   set_hashtable_value_ids ();
2818   
2819   if (dump_file && (dump_flags & TDF_DETAILS))
2820     {
2821       fprintf (dump_file, "Value numbers:\n");
2822       for (i = 0; i < num_ssa_names; i++)
2823         {
2824           tree name = ssa_name (i);
2825           if (name
2826               && VN_INFO (name)->visited
2827               && SSA_VAL (name) != name)
2828             {
2829               print_generic_expr (dump_file, name, 0);
2830               fprintf (dump_file, " = ");
2831               print_generic_expr (dump_file, SSA_VAL (name), 0);
2832               fprintf (dump_file, "\n");
2833             }
2834         }
2835     }
2836
2837   may_insert = false;
2838   return true;
2839 }
2840
2841 /* Return the maximum value id we have ever seen.  */
2842
2843 unsigned int
2844 get_max_value_id (void) 
2845 {
2846   return next_value_id;
2847 }
2848
2849 /* Return the next unique value id.  */
2850
2851 unsigned int
2852 get_next_value_id (void)
2853 {
2854   return next_value_id++;
2855 }
2856
2857
2858 /* Compare two expressions E1 and E2 and return true if they are equal.  */
2859
2860 bool
2861 expressions_equal_p (tree e1, tree e2)
2862 {
2863   /* The obvious case.  */
2864   if (e1 == e2)
2865     return true;
2866
2867   /* If only one of them is null, they cannot be equal.  */
2868   if (!e1 || !e2)
2869     return false;
2870
2871   /* Recurse on elements of lists.  */
2872   if (TREE_CODE (e1) == TREE_LIST && TREE_CODE (e2) == TREE_LIST)
2873     {
2874       tree lop1 = e1;
2875       tree lop2 = e2;
2876       for (lop1 = e1, lop2 = e2;
2877            lop1 || lop2;
2878            lop1 = TREE_CHAIN (lop1), lop2 = TREE_CHAIN (lop2))
2879         {
2880           if (!lop1 || !lop2)
2881             return false;
2882           if (!expressions_equal_p (TREE_VALUE (lop1), TREE_VALUE (lop2)))
2883             return false;
2884         }
2885       return true;
2886     }
2887
2888   /* Now perform the actual comparison.  */
2889   if (TREE_CODE (e1) == TREE_CODE (e2)
2890       && operand_equal_p (e1, e2, OEP_PURE_SAME))
2891     return true;
2892
2893   return false;
2894 }
2895
2896 /* Sort the VUSE array so that we can do equality comparisons
2897    quicker on two vuse vecs.  */
2898
2899 void
2900 sort_vuses (VEC (tree,gc) *vuses)
2901 {
2902   if (VEC_length (tree, vuses) > 1)
2903     qsort (VEC_address (tree, vuses),
2904            VEC_length (tree, vuses),
2905            sizeof (tree),
2906            operand_build_cmp);
2907 }
2908
2909 /* Sort the VUSE array so that we can do equality comparisons
2910    quicker on two vuse vecs.  */
2911
2912 void
2913 sort_vuses_heap (VEC (tree,heap) *vuses)
2914 {
2915   if (VEC_length (tree, vuses) > 1)
2916     qsort (VEC_address (tree, vuses),
2917            VEC_length (tree, vuses),
2918            sizeof (tree),
2919            operand_build_cmp);
2920 }