OSDN Git Service

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