OSDN Git Service

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