OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-sccvn.c
1 /* SCC value numbering for trees
2    Copyright (C) 2006, 2007
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 "tree-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 /* Nary operations in the hashtable consist of length operands, an
119    opcode, and a type.  Result is the value number of the operation,
120    and hashcode is stored to avoid having to calculate it
121    repeatedly.  */
122
123 typedef struct vn_nary_op_s
124 {
125   ENUM_BITFIELD(tree_code) opcode : 16;
126   unsigned length : 16;
127   hashval_t hashcode;
128   tree result;
129   tree type;
130   tree op[4];
131 } *vn_nary_op_t;
132 typedef const struct vn_nary_op_s *const_vn_nary_op_t;
133
134 /* Phi nodes in the hashtable consist of their non-VN_TOP phi
135    arguments, and the basic block the phi is in. Result is the value
136    number of the operation, and hashcode is stored to avoid having to
137    calculate it repeatedly.  Phi nodes not in the same block are never
138    considered equivalent.  */
139
140 typedef struct vn_phi_s
141 {
142   VEC (tree, heap) *phiargs;
143   basic_block block;
144   hashval_t hashcode;
145   tree result;
146 } *vn_phi_t;
147 typedef const struct vn_phi_s *const_vn_phi_t;
148
149 /* Reference operands only exist in reference operations structures.
150    They consist of an opcode, type, and some number of operands.  For
151    a given opcode, some, all, or none of the operands may be used.
152    The operands are there to store the information that makes up the
153    portion of the addressing calculation that opcode performs.  */
154
155 typedef struct vn_reference_op_struct
156 {
157   enum tree_code opcode;
158   tree type;
159   tree op0;
160   tree op1;
161 } vn_reference_op_s;
162 typedef vn_reference_op_s *vn_reference_op_t;
163 typedef const vn_reference_op_s *const_vn_reference_op_t;
164
165 DEF_VEC_O(vn_reference_op_s);
166 DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
167
168 /* A reference operation in the hashtable is representation as a
169    collection of vuses, representing the memory state at the time of
170    the operation, and a collection of operands that make up the
171    addressing calculation.  If two vn_reference_t's have the same set
172    of operands, they access the same memory location. We also store
173    the resulting value number, and the hashcode.  The vuses are
174    always stored in order sorted by ssa name version.  */
175
176 typedef struct vn_reference_s
177 {
178   VEC (tree, gc) *vuses;
179   VEC (vn_reference_op_s, heap) *operands;
180   hashval_t hashcode;
181   tree result;
182 } *vn_reference_t;
183 typedef const struct vn_reference_s *const_vn_reference_t;
184
185 /* Valid hashtables storing information we have proven to be
186    correct.  */
187
188 static vn_tables_t valid_info;
189
190 /* Optimistic hashtables storing information we are making assumptions about
191    during iterations.  */
192
193 static vn_tables_t optimistic_info;
194
195 /* PRE hashtables storing information about mapping from expressions to
196    value handles.  */
197
198 static vn_tables_t pre_info;
199
200 /* Pointer to the set of hashtables that is currently being used.
201    Should always point to either the optimistic_info, or the
202    valid_info.  */
203
204 static vn_tables_t current_info;
205
206
207 /* Reverse post order index for each basic block.  */
208
209 static int *rpo_numbers;
210
211 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
212
213 /* This represents the top of the VN lattice, which is the universal
214    value.  */
215
216 tree VN_TOP;
217
218 /* Next DFS number and the stack for strongly connected component
219    detection. */
220
221 static unsigned int next_dfs_num;
222 static VEC (tree, heap) *sccstack;
223
224 static bool may_insert;
225
226
227 DEF_VEC_P(vn_ssa_aux_t);
228 DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
229
230 /* Table of vn_ssa_aux_t's, one per ssa_name.  The vn_ssa_aux_t objects
231    are allocated on an obstack for locality reasons, and to free them
232    without looping over the VEC.  */
233
234 static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
235 static struct obstack vn_ssa_aux_obstack;
236
237 /* Return the value numbering information for a given SSA name.  */
238
239 vn_ssa_aux_t
240 VN_INFO (tree name)
241 {
242   return VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
243                     SSA_NAME_VERSION (name));
244 }
245
246 /* Set the value numbering info for a given SSA name to a given
247    value.  */
248
249 static inline void
250 VN_INFO_SET (tree name, vn_ssa_aux_t value)
251 {
252   VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
253                SSA_NAME_VERSION (name), value);
254 }
255
256 /* Initialize the value numbering info for a given SSA name.
257    This should be called just once for every SSA name.  */
258
259 vn_ssa_aux_t
260 VN_INFO_GET (tree name)
261 {
262   vn_ssa_aux_t newinfo;
263
264   newinfo = obstack_alloc (&vn_ssa_aux_obstack, sizeof (struct vn_ssa_aux));
265   memset (newinfo, 0, sizeof (struct vn_ssa_aux));
266   if (SSA_NAME_VERSION (name) >= VEC_length (vn_ssa_aux_t, vn_ssa_aux_table))
267     VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table,
268                    SSA_NAME_VERSION (name) + 1);
269   VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
270                SSA_NAME_VERSION (name), newinfo);
271   return newinfo;
272 }
273
274
275 /* Free a phi operation structure VP.  */
276
277 static void
278 free_phi (void *vp)
279 {
280   vn_phi_t phi = vp;
281   VEC_free (tree, heap, phi->phiargs);
282 }
283
284 /* Free a reference operation structure VP.  */
285
286 static void
287 free_reference (void *vp)
288 {
289   vn_reference_t vr = vp;
290   VEC_free (vn_reference_op_s, heap, vr->operands);
291 }
292
293 /* Compare two reference operands P1 and P2 for equality.  return true if
294    they are equal, and false otherwise.  */
295
296 static int
297 vn_reference_op_eq (const void *p1, const void *p2)
298 {
299   const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
300   const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
301   return vro1->opcode == vro2->opcode
302     && vro1->type == vro2->type
303     && expressions_equal_p (vro1->op0, vro2->op0)
304     && expressions_equal_p (vro1->op1, vro2->op1);
305 }
306
307 /* Compute the hash for a reference operand VRO1  */
308
309 static hashval_t
310 vn_reference_op_compute_hash (const vn_reference_op_t vro1)
311 {
312   return iterative_hash_expr (vro1->op0, vro1->opcode)
313     + iterative_hash_expr (vro1->op1, vro1->opcode);
314 }
315
316 /* Return the hashcode for a given reference operation P1.  */
317
318 static hashval_t
319 vn_reference_hash (const void *p1)
320 {
321   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
322   return vr1->hashcode;
323 }
324
325 /* Compute a hash for the reference operation VR1 and return it.  */
326
327 static inline hashval_t
328 vn_reference_compute_hash (const vn_reference_t vr1)
329 {
330   hashval_t result = 0;
331   tree v;
332   int i;
333   vn_reference_op_t vro;
334
335   for (i = 0; VEC_iterate (tree, vr1->vuses, i, v); i++)
336     result += iterative_hash_expr (v, 0);
337   for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
338     result += vn_reference_op_compute_hash (vro);
339
340   return result;
341 }
342
343 /* Return true if reference operations P1 and P2 are equivalent.  This
344    means they have the same set of operands and vuses.  */
345
346 static int
347 vn_reference_eq (const void *p1, const void *p2)
348 {
349   tree v;
350   int i;
351   vn_reference_op_t vro;
352
353   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
354   const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
355
356   if (vr1->vuses == vr2->vuses
357       && vr1->operands == vr2->operands)
358     return true;
359
360   /* Impossible for them to be equivalent if they have different
361      number of vuses.  */
362   if (VEC_length (tree, vr1->vuses) != VEC_length (tree, vr2->vuses))
363     return false;
364
365   /* We require that address operands be canonicalized in a way that
366      two memory references will have the same operands if they are
367      equivalent.  */
368   if (VEC_length (vn_reference_op_s, vr1->operands)
369       != VEC_length (vn_reference_op_s, vr2->operands))
370     return false;
371
372   /* The memory state is more often different than the address of the
373      store/load, so check it first.  */
374   for (i = 0; VEC_iterate (tree, vr1->vuses, i, v); i++)
375     {
376       if (VEC_index (tree, vr2->vuses, i) != v)
377         return false;
378     }
379
380   for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
381     {
382       if (!vn_reference_op_eq (VEC_index (vn_reference_op_s, vr2->operands, i),
383                                vro))
384         return false;
385     }
386   return true;
387 }
388
389 /* Place the vuses from STMT into *result */
390
391 static inline void
392 vuses_to_vec (tree stmt, VEC (tree, gc) **result)
393 {
394   ssa_op_iter iter;
395   tree vuse;
396
397   if (!stmt)
398     return;
399
400   VEC_reserve_exact (tree, gc, *result,
401                      num_ssa_operands (stmt, SSA_OP_VIRTUAL_USES));
402
403   FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VIRTUAL_USES)
404     VEC_quick_push (tree, *result, vuse);
405 }
406
407
408 /* Copy the VUSE names in STMT into a vector, and return
409    the vector.  */
410
411 VEC (tree, gc) *
412 copy_vuses_from_stmt (tree stmt)
413 {
414   VEC (tree, gc) *vuses = NULL;
415
416   vuses_to_vec (stmt, &vuses);
417
418   return vuses;
419 }
420
421 /* Place the vdefs from STMT into *result */
422
423 static inline void
424 vdefs_to_vec (tree stmt, VEC (tree, gc) **result)
425 {
426   ssa_op_iter iter;
427   tree vdef;
428
429   if (!stmt)
430     return;
431
432   *result = VEC_alloc (tree, gc, num_ssa_operands (stmt, SSA_OP_VIRTUAL_DEFS));
433
434   FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, iter, SSA_OP_VIRTUAL_DEFS)
435     VEC_quick_push (tree, *result, vdef);
436 }
437
438 /* Copy the names of vdef results in STMT into a vector, and return
439    the vector.  */
440
441 static VEC (tree, gc) *
442 copy_vdefs_from_stmt (tree stmt)
443 {
444   VEC (tree, gc) *vdefs = NULL;
445
446   vdefs_to_vec (stmt, &vdefs);
447
448   return vdefs;
449 }
450
451 /* Place for shared_v{uses/defs}_from_stmt to shove vuses/vdefs.  */
452 static VEC (tree, gc) *shared_lookup_vops;
453
454 /* Copy the virtual uses from STMT into SHARED_LOOKUP_VOPS.
455    This function will overwrite the current SHARED_LOOKUP_VOPS
456    variable.  */
457
458 VEC (tree, gc) *
459 shared_vuses_from_stmt (tree stmt)
460 {
461   VEC_truncate (tree, shared_lookup_vops, 0);
462   vuses_to_vec (stmt, &shared_lookup_vops);
463
464   return shared_lookup_vops;
465 }
466
467 /* Copy the operations present in load/store/call REF into RESULT, a vector of
468    vn_reference_op_s's.  */
469
470 static void
471 copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
472 {
473   /* Calls are different from all other reference operations.  */
474   if (TREE_CODE (ref) == CALL_EXPR)
475     {
476       vn_reference_op_s temp;
477       tree callfn;
478       call_expr_arg_iterator iter;
479       tree callarg;
480
481       /* Copy the call_expr opcode, type, function being called, and
482          arguments.  */
483       memset (&temp, 0, sizeof (temp));
484       temp.type = TREE_TYPE (ref);
485       temp.opcode = CALL_EXPR;
486       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
487
488       callfn = get_callee_fndecl (ref);
489       if (!callfn)
490         callfn = CALL_EXPR_FN (ref);
491       temp.type = TREE_TYPE (callfn);
492       temp.opcode = TREE_CODE (callfn);
493       temp.op0 = callfn;
494       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
495
496       FOR_EACH_CALL_EXPR_ARG (callarg, iter, ref)
497         {
498           memset (&temp, 0, sizeof (temp));
499           temp.type = TREE_TYPE (callarg);
500           temp.opcode = TREE_CODE (callarg);
501           temp.op0 = callarg;
502           VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
503         }
504       return;
505     }
506
507   /* For non-calls, store the information that makes up the address.  */
508
509   while (ref)
510     {
511       vn_reference_op_s temp;
512
513       memset (&temp, 0, sizeof (temp));
514       /* We do not care for spurious type qualifications.  */
515       temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
516       temp.opcode = TREE_CODE (ref);
517
518       switch (temp.opcode)
519         {
520         case ALIGN_INDIRECT_REF:
521         case MISALIGNED_INDIRECT_REF:
522         case INDIRECT_REF:
523           /* The only operand is the address, which gets its own
524              vn_reference_op_s structure.  */
525           break;
526         case BIT_FIELD_REF:
527           /* Record bits and position.  */
528           temp.op0 = TREE_OPERAND (ref, 1);
529           temp.op1 = TREE_OPERAND (ref, 2);
530           break;
531         case COMPONENT_REF:
532           /* The field decl is enough to unambiguously specify the field,
533              a matching type is not necessary and a mismatching type
534              is always a spurious difference.  */
535           temp.type = NULL_TREE;
536           /* If this is a reference to a union member, record the union
537              member size as operand.  Do so only if we are doing
538              expression insertion (during FRE), as PRE currently gets
539              confused with this.  */
540           if (may_insert
541               && TREE_CODE (DECL_CONTEXT (TREE_OPERAND (ref, 1))) == UNION_TYPE
542               && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (ref, 1)))
543               && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1))))
544             temp.op0 = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)));
545           else
546             /* Record field as operand.  */
547             temp.op0 = TREE_OPERAND (ref, 1);
548           break;
549         case ARRAY_RANGE_REF:
550         case ARRAY_REF:
551           /* Record index as operand.  */
552           temp.op0 = TREE_OPERAND (ref, 1);
553           temp.op1 = TREE_OPERAND (ref, 3);
554           break;
555         case STRING_CST:
556         case INTEGER_CST:
557         case COMPLEX_CST:
558         case VECTOR_CST:
559         case REAL_CST:
560         case CONSTRUCTOR:
561         case VALUE_HANDLE:
562         case VAR_DECL:
563         case PARM_DECL:
564         case CONST_DECL:
565         case RESULT_DECL:
566         case SSA_NAME:
567           temp.op0 = ref;
568           break;
569           /* These are only interesting for their operands, their
570              existence, and their type.  They will never be the last
571              ref in the chain of references (IE they require an
572              operand), so we don't have to put anything
573              for op* as it will be handled by the iteration  */
574         case IMAGPART_EXPR:
575         case REALPART_EXPR:
576         case VIEW_CONVERT_EXPR:
577         case ADDR_EXPR:
578           break;
579         default:
580           gcc_unreachable ();
581
582         }
583       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
584
585       if (REFERENCE_CLASS_P (ref) || TREE_CODE (ref) == ADDR_EXPR)
586         ref = TREE_OPERAND (ref, 0);
587       else
588         ref = NULL_TREE;
589     }
590 }
591
592 /* Create a vector of vn_reference_op_s structures from REF, a
593    REFERENCE_CLASS_P tree.  The vector is not shared. */
594
595 static VEC(vn_reference_op_s, heap) *
596 create_reference_ops_from_ref (tree ref)
597 {
598   VEC (vn_reference_op_s, heap) *result = NULL;
599
600   copy_reference_ops_from_ref (ref, &result);
601   return result;
602 }
603
604 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
605
606 /* Create a vector of vn_reference_op_s structures from REF, a
607    REFERENCE_CLASS_P tree.  The vector is shared among all callers of
608    this function.  */
609
610 static VEC(vn_reference_op_s, heap) *
611 shared_reference_ops_from_ref (tree ref)
612 {
613   if (!ref)
614     return NULL;
615   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
616   copy_reference_ops_from_ref (ref, &shared_lookup_references);
617   return shared_lookup_references;
618 }
619
620
621 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
622    structures into their value numbers.  This is done in-place, and
623    the vector passed in is returned.  */
624
625 static VEC (vn_reference_op_s, heap) *
626 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
627 {
628   vn_reference_op_t vro;
629   int i;
630
631   for (i = 0; VEC_iterate (vn_reference_op_s, orig, i, vro); i++)
632     {
633       if (vro->opcode == SSA_NAME
634           || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
635         vro->op0 = SSA_VAL (vro->op0);
636     }
637
638   return orig;
639 }
640
641 /* Transform any SSA_NAME's in ORIG, a vector of vuse trees, into
642    their value numbers. This is done in-place, and the vector passed
643    in is returned.  */
644
645 static VEC (tree, gc) *
646 valueize_vuses (VEC (tree, gc) *orig)
647 {
648   bool made_replacement = false;
649   tree vuse;
650   int i;
651
652   for (i = 0; VEC_iterate (tree, orig, i, vuse); i++)
653     {
654       if (vuse != SSA_VAL (vuse))
655         {
656           made_replacement = true;
657           VEC_replace (tree, orig, i, SSA_VAL (vuse));
658         }
659     }
660
661   if (made_replacement && VEC_length (tree, orig) > 1)
662     sort_vuses (orig);
663
664   return orig;
665 }
666
667 /* Return the single reference statement defining all virtual uses
668    in VUSES or NULL_TREE, if there are multiple defining statements.
669    Take into account only definitions that alias REF if following
670    back-edges.  */
671
672 static tree
673 get_def_ref_stmt_vuses (tree ref, VEC (tree, gc) *vuses)
674 {
675   tree def_stmt, vuse;
676   unsigned int i;
677
678   gcc_assert (VEC_length (tree, vuses) >= 1);
679
680   def_stmt = SSA_NAME_DEF_STMT (VEC_index (tree, vuses, 0));
681   if (TREE_CODE (def_stmt) == PHI_NODE)
682     {
683       /* We can only handle lookups over PHI nodes for a single
684          virtual operand.  */
685       if (VEC_length (tree, vuses) == 1)
686         {
687           def_stmt = get_single_def_stmt_from_phi (ref, def_stmt);
688           goto cont;
689         }
690       else
691         return NULL_TREE;
692     }
693
694   /* Verify each VUSE reaches the same defining stmt.  */
695   for (i = 1; VEC_iterate (tree, vuses, i, vuse); ++i)
696     {
697       tree tmp = SSA_NAME_DEF_STMT (vuse);
698       if (tmp != def_stmt)
699         return NULL_TREE;
700     }
701
702   /* Now see if the definition aliases ref, and loop until it does.  */
703 cont:
704   while (def_stmt
705          && TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
706          && !get_call_expr_in (def_stmt)
707          && !refs_may_alias_p (ref, GIMPLE_STMT_OPERAND (def_stmt, 0)))
708     def_stmt = get_single_def_stmt_with_phi (ref, def_stmt);
709
710   return def_stmt;
711 }
712
713 /* Lookup a SCCVN reference operation VR in the current hash table.
714    Returns the resulting value number if it exists in the hash table,
715    NULL_TREE otherwise.  */
716
717 static tree
718 vn_reference_lookup_1 (vn_reference_t vr)
719 {
720   void **slot;
721   hashval_t hash;
722
723   hash = vr->hashcode;
724   slot = htab_find_slot_with_hash (current_info->references, vr,
725                                    hash, NO_INSERT);
726   if (!slot && current_info == optimistic_info)
727     slot = htab_find_slot_with_hash (valid_info->references, vr,
728                                      hash, NO_INSERT);
729   if (slot)
730     return ((vn_reference_t)*slot)->result;
731
732   return NULL_TREE;
733 }
734
735 /* Lookup OP in the current hash table, and return the resulting
736    value number if it exists in the hash table.  Return NULL_TREE if
737    it does not exist in the hash table. */
738
739 tree
740 vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk)
741 {
742   struct vn_reference_s vr1;
743   tree result, def_stmt;
744
745   vr1.vuses = valueize_vuses (vuses);
746   vr1.operands = valueize_refs (shared_reference_ops_from_ref (op));
747   vr1.hashcode = vn_reference_compute_hash (&vr1);
748   result = vn_reference_lookup_1 (&vr1);
749
750   /* If there is a single defining statement for all virtual uses, we can
751      use that, following virtual use-def chains.  */
752   if (!result
753       && maywalk
754       && vr1.vuses
755       && VEC_length (tree, vr1.vuses) >= 1
756       && !get_call_expr_in (op)
757       && (def_stmt = get_def_ref_stmt_vuses (op, vr1.vuses))
758       && TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
759       /* If there is a call involved, op must be assumed to
760          be clobbered.  */
761       && !get_call_expr_in (def_stmt))
762     {
763       /* We are now at an aliasing definition for the vuses we want to
764          look up.  Re-do the lookup with the vdefs for this stmt.  */
765       vdefs_to_vec (def_stmt, &vuses);
766       vr1.vuses = valueize_vuses (vuses);
767       vr1.hashcode = vn_reference_compute_hash (&vr1);
768       result = vn_reference_lookup_1 (&vr1);
769     }
770
771   return result;
772 }
773
774 /* Insert OP into the current hash table with a value number of
775    RESULT.  */
776
777 void
778 vn_reference_insert (tree op, tree result, VEC (tree, gc) *vuses)
779 {
780   void **slot;
781   vn_reference_t vr1;
782
783   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
784
785   vr1->vuses = valueize_vuses (vuses);
786   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
787   vr1->hashcode = vn_reference_compute_hash (vr1);
788   vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
789
790   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
791                                    INSERT);
792
793   /* Because we lookup stores using vuses, and value number failures
794      using the vdefs (see visit_reference_op_store for how and why),
795      it's possible that on failure we may try to insert an already
796      inserted store.  This is not wrong, there is no ssa name for a
797      store that we could use as a differentiator anyway.  Thus, unlike
798      the other lookup functions, you cannot gcc_assert (!*slot)
799      here.  */
800
801   /* But free the old slot in case of a collision.  */
802   if (*slot)
803     free_reference (*slot);
804
805   *slot = vr1;
806 }
807
808 /* Compute and return the hash value for nary operation VBO1.  */
809
810 static inline hashval_t
811 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
812 {
813   hashval_t hash = 0;
814   unsigned i;
815
816   for (i = 0; i < vno1->length; ++i)
817     if (TREE_CODE (vno1->op[i]) == SSA_NAME)
818       vno1->op[i] = SSA_VAL (vno1->op[i]);
819
820   if (vno1->length == 2
821       && commutative_tree_code (vno1->opcode)
822       && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
823     {
824       tree temp = vno1->op[0];
825       vno1->op[0] = vno1->op[1];
826       vno1->op[1] = temp;
827     }
828
829   for (i = 0; i < vno1->length; ++i)
830     hash += iterative_hash_expr (vno1->op[i], vno1->opcode);
831
832   return hash;
833 }
834
835 /* Return the computed hashcode for nary operation P1.  */
836
837 static hashval_t
838 vn_nary_op_hash (const void *p1)
839 {
840   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
841   return vno1->hashcode;
842 }
843
844 /* Compare nary operations P1 and P2 and return true if they are
845    equivalent.  */
846
847 static int
848 vn_nary_op_eq (const void *p1, const void *p2)
849 {
850   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
851   const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
852   unsigned i;
853
854   if (vno1->opcode != vno2->opcode
855       || vno1->type != vno2->type)
856     return false;
857
858   for (i = 0; i < vno1->length; ++i)
859     if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
860       return false;
861
862   return true;
863 }
864
865 /* Lookup OP in the current hash table, and return the resulting
866    value number if it exists in the hash table.  Return NULL_TREE if
867    it does not exist in the hash table. */
868
869 tree
870 vn_nary_op_lookup (tree op)
871 {
872   void **slot;
873   struct vn_nary_op_s vno1;
874   unsigned i;
875
876   vno1.opcode = TREE_CODE (op);
877   vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
878   vno1.type = TREE_TYPE (op);
879   for (i = 0; i < vno1.length; ++i)
880     vno1.op[i] = TREE_OPERAND (op, i);
881   vno1.hashcode = vn_nary_op_compute_hash (&vno1);
882   slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
883                                    NO_INSERT);
884   if (!slot && current_info == optimistic_info)
885     slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
886                                      NO_INSERT);
887   if (!slot)
888     return NULL_TREE;
889   return ((vn_nary_op_t)*slot)->result;
890 }
891
892 /* Insert OP into the current hash table with a value number of
893    RESULT.  */
894
895 void
896 vn_nary_op_insert (tree op, tree result)
897 {
898   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
899   void **slot;
900   vn_nary_op_t vno1;
901   unsigned i;
902
903   vno1 = obstack_alloc (&current_info->nary_obstack,
904                         (sizeof (struct vn_nary_op_s)
905                          - sizeof (tree) * (4 - length)));
906   vno1->opcode = TREE_CODE (op);
907   vno1->length = length;
908   vno1->type = TREE_TYPE (op);
909   for (i = 0; i < vno1->length; ++i)
910     vno1->op[i] = TREE_OPERAND (op, i);
911   vno1->result = result;
912   vno1->hashcode = vn_nary_op_compute_hash (vno1);
913   slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
914                                    INSERT);
915   gcc_assert (!*slot);
916
917   *slot = vno1;
918 }
919
920 /* Compute a hashcode for PHI operation VP1 and return it.  */
921
922 static inline hashval_t
923 vn_phi_compute_hash (vn_phi_t vp1)
924 {
925   hashval_t result = 0;
926   int i;
927   tree phi1op;
928
929   result = vp1->block->index;
930
931   for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
932     {
933       if (phi1op == VN_TOP)
934         continue;
935       result += iterative_hash_expr (phi1op, result);
936     }
937
938   return result;
939 }
940
941 /* Return the computed hashcode for phi operation P1.  */
942
943 static hashval_t
944 vn_phi_hash (const void *p1)
945 {
946   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
947   return vp1->hashcode;
948 }
949
950 /* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
951
952 static int
953 vn_phi_eq (const void *p1, const void *p2)
954 {
955   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
956   const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
957
958   if (vp1->block == vp2->block)
959     {
960       int i;
961       tree phi1op;
962
963       /* Any phi in the same block will have it's arguments in the
964          same edge order, because of how we store phi nodes.  */
965       for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
966         {
967           tree phi2op = VEC_index (tree, vp2->phiargs, i);
968           if (phi1op == VN_TOP || phi2op == VN_TOP)
969             continue;
970           if (!expressions_equal_p (phi1op, phi2op))
971             return false;
972         }
973       return true;
974     }
975   return false;
976 }
977
978 static VEC(tree, heap) *shared_lookup_phiargs;
979
980 /* Lookup PHI in the current hash table, and return the resulting
981    value number if it exists in the hash table.  Return NULL_TREE if
982    it does not exist in the hash table. */
983
984 static tree
985 vn_phi_lookup (tree phi)
986 {
987   void **slot;
988   struct vn_phi_s vp1;
989   int i;
990
991   VEC_truncate (tree, shared_lookup_phiargs, 0);
992
993   /* Canonicalize the SSA_NAME's to their value number.  */
994   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
995     {
996       tree def = PHI_ARG_DEF (phi, i);
997       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
998       VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
999     }
1000   vp1.phiargs = shared_lookup_phiargs;
1001   vp1.block = bb_for_stmt (phi);
1002   vp1.hashcode = vn_phi_compute_hash (&vp1);
1003   slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
1004                                    NO_INSERT);
1005   if (!slot && current_info == optimistic_info)
1006     slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
1007                                      NO_INSERT);
1008   if (!slot)
1009     return NULL_TREE;
1010   return ((vn_phi_t)*slot)->result;
1011 }
1012
1013 /* Insert PHI into the current hash table with a value number of
1014    RESULT.  */
1015
1016 static void
1017 vn_phi_insert (tree phi, tree result)
1018 {
1019   void **slot;
1020   vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
1021   int i;
1022   VEC (tree, heap) *args = NULL;
1023
1024   /* Canonicalize the SSA_NAME's to their value number.  */
1025   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1026     {
1027       tree def = PHI_ARG_DEF (phi, i);
1028       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1029       VEC_safe_push (tree, heap, args, def);
1030     }
1031   vp1->phiargs = args;
1032   vp1->block = bb_for_stmt (phi);
1033   vp1->result = result;
1034   vp1->hashcode = vn_phi_compute_hash (vp1);
1035
1036   slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
1037                                    INSERT);
1038
1039   /* Because we iterate over phi operations more than once, it's
1040      possible the slot might already exist here, hence no assert.*/
1041   *slot = vp1;
1042 }
1043
1044
1045 /* Print set of components in strongly connected component SCC to OUT. */
1046
1047 static void
1048 print_scc (FILE *out, VEC (tree, heap) *scc)
1049 {
1050   tree var;
1051   unsigned int i;
1052
1053   fprintf (out, "SCC consists of: ");
1054   for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1055     {
1056       print_generic_expr (out, var, 0);
1057       fprintf (out, " ");
1058     }
1059   fprintf (out, "\n");
1060 }
1061
1062 /* Set the value number of FROM to TO, return true if it has changed
1063    as a result.  */
1064
1065 static inline bool
1066 set_ssa_val_to (tree from, tree to)
1067 {
1068   tree currval;
1069
1070   if (from != to
1071       && TREE_CODE (to) == SSA_NAME
1072       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
1073     to = from;
1074
1075   /* The only thing we allow as value numbers are VN_TOP, ssa_names
1076      and invariants.  So assert that here.  */
1077   gcc_assert (to != NULL_TREE
1078               && (to == VN_TOP
1079                   || TREE_CODE (to) == SSA_NAME
1080                   || is_gimple_min_invariant (to)));
1081
1082   if (dump_file && (dump_flags & TDF_DETAILS))
1083     {
1084       fprintf (dump_file, "Setting value number of ");
1085       print_generic_expr (dump_file, from, 0);
1086       fprintf (dump_file, " to ");
1087       print_generic_expr (dump_file, to, 0);
1088       fprintf (dump_file, "\n");
1089     }
1090
1091   currval = SSA_VAL (from);
1092
1093   if (currval != to  && !operand_equal_p (currval, to, OEP_PURE_SAME))
1094     {
1095       SSA_VAL (from) = to;
1096       return true;
1097     }
1098   return false;
1099 }
1100
1101 /* Set all definitions in STMT to value number to themselves.
1102    Return true if a value number changed. */
1103
1104 static bool
1105 defs_to_varying (tree stmt)
1106 {
1107   bool changed = false;
1108   ssa_op_iter iter;
1109   def_operand_p defp;
1110
1111   FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
1112     {
1113       tree def = DEF_FROM_PTR (defp);
1114
1115       VN_INFO (def)->use_processed = true;
1116       changed |= set_ssa_val_to (def, def);
1117     }
1118   return changed;
1119 }
1120
1121 static tree
1122 try_to_simplify (tree stmt, tree rhs);
1123
1124 /* Visit a copy between LHS and RHS, return true if the value number
1125    changed.  */
1126
1127 static bool
1128 visit_copy (tree lhs, tree rhs)
1129 {
1130
1131   /* Follow chains of copies to their destination.  */
1132   while (SSA_VAL (rhs) != rhs && TREE_CODE (SSA_VAL (rhs)) == SSA_NAME)
1133     rhs = SSA_VAL (rhs);
1134
1135   /* The copy may have a more interesting constant filled expression
1136      (we don't, since we know our RHS is just an SSA name).  */
1137   VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
1138   VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
1139
1140   return set_ssa_val_to (lhs, rhs);
1141 }
1142
1143 /* Visit a unary operator RHS, value number it, and return true if the
1144    value number of LHS has changed as a result.  */
1145
1146 static bool
1147 visit_unary_op (tree lhs, tree op)
1148 {
1149   bool changed = false;
1150   tree result = vn_nary_op_lookup (op);
1151
1152   if (result)
1153     {
1154       changed = set_ssa_val_to (lhs, result);
1155     }
1156   else
1157     {
1158       changed = set_ssa_val_to (lhs, lhs);
1159       vn_nary_op_insert (op, lhs);
1160     }
1161
1162   return changed;
1163 }
1164
1165 /* Visit a binary operator RHS, value number it, and return true if the
1166    value number of LHS has changed as a result.  */
1167
1168 static bool
1169 visit_binary_op (tree lhs, tree op)
1170 {
1171   bool changed = false;
1172   tree result = vn_nary_op_lookup (op);
1173
1174   if (result)
1175     {
1176       changed = set_ssa_val_to (lhs, result);
1177     }
1178   else
1179     {
1180       changed = set_ssa_val_to (lhs, lhs);
1181       vn_nary_op_insert (op, lhs);
1182     }
1183
1184   return changed;
1185 }
1186
1187 /* Visit a load from a reference operator RHS, part of STMT, value number it,
1188    and return true if the value number of the LHS has changed as a result.  */
1189
1190 static bool
1191 visit_reference_op_load (tree lhs, tree op, tree stmt)
1192 {
1193   bool changed = false;
1194   tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true);
1195
1196   /* We handle type-punning through unions by value-numbering based
1197      on offset and size of the access.  Be prepared to handle a
1198      type-mismatch here via creating a VIEW_CONVERT_EXPR.  */
1199   if (result
1200       && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
1201     {
1202       /* We will be setting the value number of lhs to the value number
1203          of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
1204          So first simplify and lookup this expression to see if it
1205          is already available.  */
1206       tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
1207       if (stmt
1208           && !is_gimple_min_invariant (val)
1209           && TREE_CODE (val) != SSA_NAME)
1210         {
1211           tree tem = try_to_simplify (stmt, val);
1212           if (tem)
1213             val = tem;
1214         }
1215       result = val;
1216       if (!is_gimple_min_invariant (val)
1217           && TREE_CODE (val) != SSA_NAME)
1218         result = vn_nary_op_lookup (val);
1219       /* If the expression is not yet available, value-number lhs to
1220          a new SSA_NAME we create.  */
1221       if (!result && may_insert)
1222         {
1223           result = make_ssa_name (SSA_NAME_VAR (lhs), NULL_TREE);
1224           /* Initialize value-number information properly.  */
1225           VN_INFO_GET (result)->valnum = result;
1226           VN_INFO (result)->expr = val;
1227           VN_INFO (result)->needs_insertion = true;
1228           /* As all "inserted" statements are singleton SCCs, insert
1229              to the valid table.  This is strictly needed to
1230              avoid re-generating new value SSA_NAMEs for the same
1231              expression during SCC iteration over and over (the
1232              optimistic table gets cleared after each iteration).
1233              We do not need to insert into the optimistic table, as
1234              lookups there will fall back to the valid table.  */
1235           if (current_info == optimistic_info)
1236             {
1237               current_info = valid_info;
1238               vn_nary_op_insert (val, result);
1239               current_info = optimistic_info;
1240             }
1241           else
1242             vn_nary_op_insert (val, result);
1243           if (dump_file && (dump_flags & TDF_DETAILS))
1244             {
1245               fprintf (dump_file, "Inserting name ");
1246               print_generic_expr (dump_file, result, 0);
1247               fprintf (dump_file, " for expression ");
1248               print_generic_expr (dump_file, val, 0);
1249               fprintf (dump_file, "\n");
1250             }
1251         }
1252     }
1253
1254   if (result)
1255     {
1256       changed = set_ssa_val_to (lhs, result);
1257       if (TREE_CODE (result) == SSA_NAME
1258           && VN_INFO (result)->has_constants)
1259         {
1260           VN_INFO (lhs)->expr = VN_INFO (result)->expr;
1261           VN_INFO (lhs)->has_constants = true;
1262         }
1263     }
1264   else
1265     {
1266       changed = set_ssa_val_to (lhs, lhs);
1267       vn_reference_insert (op, lhs, copy_vuses_from_stmt (stmt));
1268     }
1269
1270   return changed;
1271 }
1272
1273
1274 /* Visit a store to a reference operator LHS, part of STMT, value number it,
1275    and return true if the value number of the LHS has changed as a result.  */
1276
1277 static bool
1278 visit_reference_op_store (tree lhs, tree op, tree stmt)
1279 {
1280   bool changed = false;
1281   tree result;
1282   bool resultsame = false;
1283
1284   /* First we want to lookup using the *vuses* from the store and see
1285      if there the last store to this location with the same address
1286      had the same value.
1287
1288      The vuses represent the memory state before the store.  If the
1289      memory state, address, and value of the store is the same as the
1290      last store to this location, then this store will produce the
1291      same memory state as that store.
1292
1293      In this case the vdef versions for this store are value numbered to those
1294      vuse versions, since they represent the same memory state after
1295      this store.
1296
1297      Otherwise, the vdefs for the store are used when inserting into
1298      the table, since the store generates a new memory state.  */
1299
1300   result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false);
1301
1302   if (result)
1303     {
1304       if (TREE_CODE (result) == SSA_NAME)
1305         result = SSA_VAL (result);
1306       if (TREE_CODE (op) == SSA_NAME)
1307         op = SSA_VAL (op);
1308       resultsame = expressions_equal_p (result, op);
1309     }
1310
1311   if (!result || !resultsame)
1312     {
1313       VEC(tree, gc) *vdefs = copy_vdefs_from_stmt (stmt);
1314       int i;
1315       tree vdef;
1316
1317       if (dump_file && (dump_flags & TDF_DETAILS))
1318         {
1319           fprintf (dump_file, "No store match\n");
1320           fprintf (dump_file, "Value numbering store ");
1321           print_generic_expr (dump_file, lhs, 0);
1322           fprintf (dump_file, " to ");
1323           print_generic_expr (dump_file, op, 0);
1324           fprintf (dump_file, "\n");
1325         }
1326       /* Have to set value numbers before insert, since insert is
1327          going to valueize the references in-place.  */
1328       for (i = 0; VEC_iterate (tree, vdefs, i, vdef); i++)
1329         {
1330           VN_INFO (vdef)->use_processed = true;
1331           changed |= set_ssa_val_to (vdef, vdef);
1332         }
1333
1334       /* Do not insert structure copies into the tables.  */
1335       if (is_gimple_min_invariant (op)
1336           || is_gimple_reg (op))
1337         vn_reference_insert (lhs, op, vdefs);
1338     }
1339   else
1340     {
1341       /* We had a match, so value number the vdefs to have the value
1342          number of the vuses they came from.  */
1343       ssa_op_iter op_iter;
1344       def_operand_p var;
1345       vuse_vec_p vv;
1346
1347       if (dump_file && (dump_flags & TDF_DETAILS))
1348         fprintf (dump_file, "Store matched earlier value,"
1349                  "value numbering store vdefs to matching vuses.\n");
1350
1351       FOR_EACH_SSA_VDEF_OPERAND (var, vv, stmt, op_iter)
1352         {
1353           tree def = DEF_FROM_PTR (var);
1354           tree use;
1355
1356           /* Uh, if the vuse is a multiuse, we can't really do much
1357              here, sadly, since we don't know which value number of
1358              which vuse to use.  */
1359           if (VUSE_VECT_NUM_ELEM (*vv) != 1)
1360             use = def;
1361           else
1362             use = VUSE_ELEMENT_VAR (*vv, 0);
1363
1364           VN_INFO (def)->use_processed = true;
1365           changed |= set_ssa_val_to (def, SSA_VAL (use));
1366         }
1367     }
1368
1369   return changed;
1370 }
1371
1372 /* Visit and value number PHI, return true if the value number
1373    changed.  */
1374
1375 static bool
1376 visit_phi (tree phi)
1377 {
1378   bool changed = false;
1379   tree result;
1380   tree sameval = VN_TOP;
1381   bool allsame = true;
1382   int i;
1383
1384   /* TODO: We could check for this in init_sccvn, and replace this
1385      with a gcc_assert.  */
1386   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
1387     return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1388
1389   /* See if all non-TOP arguments have the same value.  TOP is
1390      equivalent to everything, so we can ignore it.  */
1391   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1392     {
1393       tree def = PHI_ARG_DEF (phi, i);
1394
1395       if (TREE_CODE (def) == SSA_NAME)
1396         def = SSA_VAL (def);
1397       if (def == VN_TOP)
1398         continue;
1399       if (sameval == VN_TOP)
1400         {
1401           sameval = def;
1402         }
1403       else
1404         {
1405           if (!expressions_equal_p (def, sameval))
1406             {
1407               allsame = false;
1408               break;
1409             }
1410         }
1411     }
1412
1413   /* If all value numbered to the same value, the phi node has that
1414      value.  */
1415   if (allsame)
1416     {
1417       if (is_gimple_min_invariant (sameval))
1418         {
1419           VN_INFO (PHI_RESULT (phi))->has_constants = true;
1420           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1421         }
1422       else
1423         {
1424           VN_INFO (PHI_RESULT (phi))->has_constants = false;
1425           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1426         }
1427
1428       if (TREE_CODE (sameval) == SSA_NAME)
1429         return visit_copy (PHI_RESULT (phi), sameval);
1430
1431       return set_ssa_val_to (PHI_RESULT (phi), sameval);
1432     }
1433
1434   /* Otherwise, see if it is equivalent to a phi node in this block.  */
1435   result = vn_phi_lookup (phi);
1436   if (result)
1437     {
1438       if (TREE_CODE (result) == SSA_NAME)
1439         changed = visit_copy (PHI_RESULT (phi), result);
1440       else
1441         changed = set_ssa_val_to (PHI_RESULT (phi), result);
1442     }
1443   else
1444     {
1445       vn_phi_insert (phi, PHI_RESULT (phi));
1446       VN_INFO (PHI_RESULT (phi))->has_constants = false;
1447       VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
1448       changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1449     }
1450
1451   return changed;
1452 }
1453
1454 /* Return true if EXPR contains constants.  */
1455
1456 static bool
1457 expr_has_constants (tree expr)
1458 {
1459   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1460     {
1461     case tcc_unary:
1462       return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
1463
1464     case tcc_binary:
1465       return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
1466         || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
1467       /* Constants inside reference ops are rarely interesting, but
1468          it can take a lot of looking to find them.  */
1469     case tcc_reference:
1470     case tcc_declaration:
1471       return false;
1472     default:
1473       return is_gimple_min_invariant (expr);
1474     }
1475   return false;
1476 }
1477
1478 /* Replace SSA_NAMES in expr with their value numbers, and return the
1479    result.
1480    This is performed in place. */
1481
1482 static tree
1483 valueize_expr (tree expr)
1484 {
1485   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1486     {
1487     case tcc_unary:
1488       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1489           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1490         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1491       break;
1492     case tcc_binary:
1493       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1494           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1495         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1496       if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
1497           && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
1498         TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
1499       break;
1500     default:
1501       break;
1502     }
1503   return expr;
1504 }
1505
1506 /* Simplify the binary expression RHS, and return the result if
1507    simplified. */
1508
1509 static tree
1510 simplify_binary_expression (tree stmt, tree rhs)
1511 {
1512   tree result = NULL_TREE;
1513   tree op0 = TREE_OPERAND (rhs, 0);
1514   tree op1 = TREE_OPERAND (rhs, 1);
1515
1516   /* This will not catch every single case we could combine, but will
1517      catch those with constants.  The goal here is to simultaneously
1518      combine constants between expressions, but avoid infinite
1519      expansion of expressions during simplification.  */
1520   if (TREE_CODE (op0) == SSA_NAME)
1521     {
1522       if (VN_INFO (op0)->has_constants)
1523         op0 = valueize_expr (VN_INFO (op0)->expr);
1524       else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
1525         op0 = SSA_VAL (op0);
1526     }
1527
1528   if (TREE_CODE (op1) == SSA_NAME)
1529     {
1530       if (VN_INFO (op1)->has_constants)
1531         op1 = valueize_expr (VN_INFO (op1)->expr);
1532       else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
1533         op1 = SSA_VAL (op1);
1534     }
1535
1536   /* Avoid folding if nothing changed.  */
1537   if (op0 == TREE_OPERAND (rhs, 0)
1538       && op1 == TREE_OPERAND (rhs, 1))
1539     return NULL_TREE;
1540
1541   fold_defer_overflow_warnings ();
1542
1543   result = fold_binary (TREE_CODE (rhs), TREE_TYPE (rhs), op0, op1);
1544
1545   fold_undefer_overflow_warnings (result && valid_gimple_expression_p (result),
1546                                   stmt, 0);
1547
1548   /* Make sure result is not a complex expression consisting
1549      of operators of operators (IE (a + b) + (a + c))
1550      Otherwise, we will end up with unbounded expressions if
1551      fold does anything at all.  */
1552   if (result && valid_gimple_expression_p (result))
1553     return result;
1554
1555   return NULL_TREE;
1556 }
1557
1558 /* Simplify the unary expression RHS, and return the result if
1559    simplified. */
1560
1561 static tree
1562 simplify_unary_expression (tree rhs)
1563 {
1564   tree result = NULL_TREE;
1565   tree op0 = TREE_OPERAND (rhs, 0);
1566
1567   if (TREE_CODE (op0) != SSA_NAME)
1568     return NULL_TREE;
1569
1570   if (VN_INFO (op0)->has_constants)
1571     op0 = valueize_expr (VN_INFO (op0)->expr);
1572   else if (CONVERT_EXPR_P (rhs)
1573            || TREE_CODE (rhs) == REALPART_EXPR
1574            || TREE_CODE (rhs) == IMAGPART_EXPR
1575            || TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
1576     {
1577       /* We want to do tree-combining on conversion-like expressions.
1578          Make sure we feed only SSA_NAMEs or constants to fold though.  */
1579       tree tem = valueize_expr (VN_INFO (op0)->expr);
1580       if (UNARY_CLASS_P (tem)
1581           || BINARY_CLASS_P (tem)
1582           || TREE_CODE (tem) == VIEW_CONVERT_EXPR
1583           || TREE_CODE (tem) == SSA_NAME
1584           || is_gimple_min_invariant (tem))
1585         op0 = tem;
1586     }
1587
1588   /* Avoid folding if nothing changed, but remember the expression.  */
1589   if (op0 == TREE_OPERAND (rhs, 0))
1590     return rhs;
1591
1592   result = fold_unary (TREE_CODE (rhs), TREE_TYPE (rhs), op0);
1593   if (result)
1594     {
1595       STRIP_USELESS_TYPE_CONVERSION (result);
1596       if (valid_gimple_expression_p (result))
1597         return result;
1598     }
1599
1600   return rhs;
1601 }
1602
1603 /* Try to simplify RHS using equivalences and constant folding.  */
1604
1605 static tree
1606 try_to_simplify (tree stmt, tree rhs)
1607 {
1608   tree tem;
1609
1610   /* For stores we can end up simplifying a SSA_NAME rhs.  Just return
1611      in this case, there is no point in doing extra work.  */
1612   if (TREE_CODE (rhs) == SSA_NAME)
1613     return rhs;
1614
1615   switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1616     {
1617     case tcc_declaration:
1618       tem = get_symbol_constant_value (rhs);
1619       if (tem)
1620         return tem;
1621       break;
1622
1623     case tcc_reference:
1624       /* Do not do full-blown reference lookup here, but simplify
1625          reads from constant aggregates.  */
1626       tem = fold_const_aggregate_ref (rhs);
1627       if (tem)
1628         return tem;
1629
1630       /* Fallthrough for some codes that can operate on registers.  */
1631       if (!(TREE_CODE (rhs) == REALPART_EXPR
1632             || TREE_CODE (rhs) == IMAGPART_EXPR
1633             || TREE_CODE (rhs) == VIEW_CONVERT_EXPR))
1634         break;
1635       /* We could do a little more with unary ops, if they expand
1636          into binary ops, but it's debatable whether it is worth it. */
1637     case tcc_unary:
1638       return simplify_unary_expression (rhs);
1639       break;
1640     case tcc_comparison:
1641     case tcc_binary:
1642       return simplify_binary_expression (stmt, rhs);
1643       break;
1644     default:
1645       break;
1646     }
1647
1648   return rhs;
1649 }
1650
1651 /* Visit and value number USE, return true if the value number
1652    changed. */
1653
1654 static bool
1655 visit_use (tree use)
1656 {
1657   bool changed = false;
1658   tree stmt = SSA_NAME_DEF_STMT (use);
1659   stmt_ann_t ann;
1660
1661   VN_INFO (use)->use_processed = true;
1662
1663   gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
1664   if (dump_file && (dump_flags & TDF_DETAILS)
1665       && !IS_EMPTY_STMT (stmt))
1666     {
1667       fprintf (dump_file, "Value numbering ");
1668       print_generic_expr (dump_file, use, 0);
1669       fprintf (dump_file, " stmt = ");
1670       print_generic_stmt (dump_file, stmt, 0);
1671     }
1672
1673   /* RETURN_EXPR may have an embedded MODIFY_STMT.  */
1674   if (TREE_CODE (stmt) == RETURN_EXPR
1675       && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
1676     stmt = TREE_OPERAND (stmt, 0);
1677
1678   ann = stmt_ann (stmt);
1679
1680   /* Handle uninitialized uses.  */
1681   if (IS_EMPTY_STMT (stmt))
1682     {
1683       changed = set_ssa_val_to (use, use);
1684     }
1685   else
1686     {
1687       if (TREE_CODE (stmt) == PHI_NODE)
1688         {
1689           changed = visit_phi (stmt);
1690         }
1691       else if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT
1692                || (ann && ann->has_volatile_ops)
1693                || tree_could_throw_p (stmt))
1694         {
1695           changed = defs_to_varying (stmt);
1696         }
1697       else
1698         {
1699           tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1700           tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1701           tree simplified;
1702
1703           STRIP_USELESS_TYPE_CONVERSION (rhs);
1704
1705           /* Shortcut for copies. Simplifying copies is pointless,
1706              since we copy the expression and value they represent.  */
1707           if (TREE_CODE (rhs) == SSA_NAME && TREE_CODE (lhs) == SSA_NAME)
1708             {
1709               changed = visit_copy (lhs, rhs);
1710               goto done;
1711             }
1712           simplified = try_to_simplify (stmt, rhs);
1713           if (simplified && simplified != rhs)
1714             {
1715               if (dump_file && (dump_flags & TDF_DETAILS))
1716                 {
1717                   fprintf (dump_file, "RHS ");
1718                   print_generic_expr (dump_file, rhs, 0);
1719                   fprintf (dump_file, " simplified to ");
1720                   print_generic_expr (dump_file, simplified, 0);
1721                   if (TREE_CODE (lhs) == SSA_NAME)
1722                     fprintf (dump_file, " has constants %d\n",
1723                              expr_has_constants (simplified));
1724                   else
1725                     fprintf (dump_file, "\n");
1726                 }
1727             }
1728           /* Setting value numbers to constants will occasionally
1729              screw up phi congruence because constants are not
1730              uniquely associated with a single ssa name that can be
1731              looked up.  */
1732           if (simplified && is_gimple_min_invariant (simplified)
1733               && TREE_CODE (lhs) == SSA_NAME
1734               && simplified != rhs)
1735             {
1736               VN_INFO (lhs)->expr = simplified;
1737               VN_INFO (lhs)->has_constants = true;
1738               changed = set_ssa_val_to (lhs, simplified);
1739               goto done;
1740             }
1741           else if (simplified && TREE_CODE (simplified) == SSA_NAME
1742                    && TREE_CODE (lhs) == SSA_NAME)
1743             {
1744               changed = visit_copy (lhs, simplified);
1745               goto done;
1746             }
1747           else if (simplified)
1748             {
1749               if (TREE_CODE (lhs) == SSA_NAME)
1750                 {
1751                   VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
1752                   /* We have to unshare the expression or else
1753                      valuizing may change the IL stream.  */
1754                   VN_INFO (lhs)->expr = unshare_expr (simplified);
1755                 }
1756               rhs = simplified;
1757             }
1758           else if (expr_has_constants (rhs) && TREE_CODE (lhs) == SSA_NAME)
1759             {
1760               VN_INFO (lhs)->has_constants = true;
1761               VN_INFO (lhs)->expr = unshare_expr (rhs);
1762             }
1763           else if (TREE_CODE (lhs) == SSA_NAME)
1764             {
1765               /* We reset expr and constantness here because we may
1766                  have been value numbering optimistically, and
1767                  iterating. They may become non-constant in this case,
1768                  even if they were optimistically constant. */
1769
1770               VN_INFO (lhs)->has_constants = false;
1771               VN_INFO (lhs)->expr = lhs;
1772             }
1773
1774           if (TREE_CODE (lhs) == SSA_NAME
1775               /* We can substitute SSA_NAMEs that are live over
1776                  abnormal edges with their constant value.  */
1777               && !is_gimple_min_invariant (rhs)
1778               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1779             changed = defs_to_varying (stmt);
1780           else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
1781             {
1782               changed = visit_reference_op_store (lhs, rhs, stmt);
1783             }
1784           else if (TREE_CODE (lhs) == SSA_NAME)
1785             {
1786               if (is_gimple_min_invariant (rhs))
1787                 {
1788                   VN_INFO (lhs)->has_constants = true;
1789                   VN_INFO (lhs)->expr = rhs;
1790                   changed = set_ssa_val_to (lhs, rhs);
1791                 }
1792               else
1793                 {
1794                   switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1795                     {
1796                     case tcc_unary:
1797                       changed = visit_unary_op (lhs, rhs);
1798                       break;
1799                     case tcc_binary:
1800                       changed = visit_binary_op (lhs, rhs);
1801                       break;
1802                       /* If tcc_vl_expr ever encompasses more than
1803                          CALL_EXPR, this will need to be changed.  */
1804                     case tcc_vl_exp:
1805                       if (call_expr_flags (rhs)  & (ECF_PURE | ECF_CONST))
1806                         changed = visit_reference_op_load (lhs, rhs, stmt);
1807                       else
1808                         changed = defs_to_varying (stmt);
1809                       break;
1810                     case tcc_declaration:
1811                     case tcc_reference:
1812                       changed = visit_reference_op_load (lhs, rhs, stmt);
1813                       break;
1814                     case tcc_expression:
1815                       if (TREE_CODE (rhs) == ADDR_EXPR)
1816                         {
1817                           changed = visit_unary_op (lhs, rhs);
1818                           goto done;
1819                         }
1820                       /* Fallthrough.  */
1821                     default:
1822                       changed = defs_to_varying (stmt);
1823                       break;
1824                     }
1825                 }
1826             }
1827           else
1828             changed = defs_to_varying (stmt);
1829         }
1830     }
1831  done:
1832   return changed;
1833 }
1834
1835 /* Compare two operands by reverse postorder index */
1836
1837 static int
1838 compare_ops (const void *pa, const void *pb)
1839 {
1840   const tree opa = *((const tree *)pa);
1841   const tree opb = *((const tree *)pb);
1842   tree opstmta = SSA_NAME_DEF_STMT (opa);
1843   tree opstmtb = SSA_NAME_DEF_STMT (opb);
1844   basic_block bba;
1845   basic_block bbb;
1846
1847   if (IS_EMPTY_STMT (opstmta) && IS_EMPTY_STMT (opstmtb))
1848     return 0;
1849   else if (IS_EMPTY_STMT (opstmta))
1850     return -1;
1851   else if (IS_EMPTY_STMT (opstmtb))
1852     return 1;
1853
1854   bba = bb_for_stmt (opstmta);
1855   bbb = bb_for_stmt (opstmtb);
1856
1857   if (!bba && !bbb)
1858     return 0;
1859   else if (!bba)
1860     return -1;
1861   else if (!bbb)
1862     return 1;
1863
1864   if (bba == bbb)
1865     {
1866       if (TREE_CODE (opstmta) == PHI_NODE && TREE_CODE (opstmtb) == PHI_NODE)
1867         return 0;
1868       else if (TREE_CODE (opstmta) == PHI_NODE)
1869         return -1;
1870       else if (TREE_CODE (opstmtb) == PHI_NODE)
1871         return 1;
1872       return stmt_ann (opstmta)->uid - stmt_ann (opstmtb)->uid;
1873     }
1874   return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
1875 }
1876
1877 /* Sort an array containing members of a strongly connected component
1878    SCC so that the members are ordered by RPO number.
1879    This means that when the sort is complete, iterating through the
1880    array will give you the members in RPO order.  */
1881
1882 static void
1883 sort_scc (VEC (tree, heap) *scc)
1884 {
1885   qsort (VEC_address (tree, scc),
1886          VEC_length (tree, scc),
1887          sizeof (tree),
1888          compare_ops);
1889 }
1890
1891 /* Process a strongly connected component in the SSA graph.  */
1892
1893 static void
1894 process_scc (VEC (tree, heap) *scc)
1895 {
1896   /* If the SCC has a single member, just visit it.  */
1897
1898   if (VEC_length (tree, scc) == 1)
1899     {
1900       tree use = VEC_index (tree, scc, 0);
1901       if (!VN_INFO (use)->use_processed)
1902         visit_use (use);
1903     }
1904   else
1905     {
1906       tree var;
1907       unsigned int i;
1908       unsigned int iterations = 0;
1909       bool changed = true;
1910
1911       /* Iterate over the SCC with the optimistic table until it stops
1912          changing.  */
1913       current_info = optimistic_info;
1914       while (changed)
1915         {
1916           changed = false;
1917           iterations++;
1918           htab_empty (optimistic_info->nary);
1919           htab_empty (optimistic_info->phis);
1920           htab_empty (optimistic_info->references);
1921           obstack_free (&optimistic_info->nary_obstack, NULL);
1922           gcc_obstack_init (&optimistic_info->nary_obstack);
1923           empty_alloc_pool (optimistic_info->phis_pool);
1924           empty_alloc_pool (optimistic_info->references_pool);
1925           for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1926             changed |= visit_use (var);
1927         }
1928
1929       if (dump_file && (dump_flags & TDF_STATS))
1930         fprintf (dump_file, "Processing SCC required %d iterations\n",
1931                  iterations);
1932
1933       /* Finally, visit the SCC once using the valid table.  */
1934       current_info = valid_info;
1935       for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1936         visit_use (var);
1937     }
1938 }
1939
1940 /* Depth first search on NAME to discover and process SCC's in the SSA
1941    graph.
1942    Execution of this algorithm relies on the fact that the SCC's are
1943    popped off the stack in topological order.
1944    Returns true if successful, false if we stopped processing SCC's due
1945    to ressource constraints.  */
1946
1947 static bool
1948 DFS (tree name)
1949 {
1950   ssa_op_iter iter;
1951   use_operand_p usep;
1952   tree defstmt;
1953
1954   /* SCC info */
1955   VN_INFO (name)->dfsnum = next_dfs_num++;
1956   VN_INFO (name)->visited = true;
1957   VN_INFO (name)->low = VN_INFO (name)->dfsnum;
1958
1959   VEC_safe_push (tree, heap, sccstack, name);
1960   VN_INFO (name)->on_sccstack = true;
1961   defstmt = SSA_NAME_DEF_STMT (name);
1962
1963   /* Recursively DFS on our operands, looking for SCC's.  */
1964   if (!IS_EMPTY_STMT (defstmt))
1965     {
1966       FOR_EACH_PHI_OR_STMT_USE (usep, SSA_NAME_DEF_STMT (name), iter,
1967                                 SSA_OP_ALL_USES)
1968         {
1969           tree use = USE_FROM_PTR (usep);
1970
1971           /* Since we handle phi nodes, we will sometimes get
1972              invariants in the use expression.  */
1973           if (TREE_CODE (use) != SSA_NAME)
1974             continue;
1975
1976           if (! (VN_INFO (use)->visited))
1977             {
1978               if (!DFS (use))
1979                 return false;
1980               VN_INFO (name)->low = MIN (VN_INFO (name)->low,
1981                                          VN_INFO (use)->low);
1982             }
1983           if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
1984               && VN_INFO (use)->on_sccstack)
1985             {
1986               VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
1987                                          VN_INFO (name)->low);
1988             }
1989         }
1990     }
1991
1992   /* See if we found an SCC.  */
1993   if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
1994     {
1995       VEC (tree, heap) *scc = NULL;
1996       tree x;
1997
1998       /* Found an SCC, pop the components off the SCC stack and
1999          process them.  */
2000       do
2001         {
2002           x = VEC_pop (tree, sccstack);
2003
2004           VN_INFO (x)->on_sccstack = false;
2005           VEC_safe_push (tree, heap, scc, x);
2006         } while (x != name);
2007
2008       /* Bail out of SCCVN in case a SCC turns out to be incredibly large.  */
2009       if (VEC_length (tree, scc)
2010             > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
2011         {
2012           if (dump_file)
2013             fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
2014                      "SCC size %u exceeding %u\n", VEC_length (tree, scc),
2015                      (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
2016           return false;
2017         }
2018
2019       if (VEC_length (tree, scc) > 1)
2020         sort_scc (scc);
2021
2022       if (dump_file && (dump_flags & TDF_DETAILS))
2023         print_scc (dump_file, scc);
2024
2025       process_scc (scc);
2026
2027       VEC_free (tree, heap, scc);
2028     }
2029
2030   return true;
2031 }
2032
2033 /* Allocate a value number table.  */
2034
2035 static void
2036 allocate_vn_table (vn_tables_t table)
2037 {
2038   table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
2039   table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
2040   table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
2041                                    free_reference);
2042
2043   gcc_obstack_init (&table->nary_obstack);
2044   table->phis_pool = create_alloc_pool ("VN phis",
2045                                         sizeof (struct vn_phi_s),
2046                                         30);
2047   table->references_pool = create_alloc_pool ("VN references",
2048                                               sizeof (struct vn_reference_s),
2049                                               30);
2050 }
2051
2052 /* Free a value number table.  */
2053
2054 static void
2055 free_vn_table (vn_tables_t table)
2056 {
2057   htab_delete (table->phis);
2058   htab_delete (table->nary);
2059   htab_delete (table->references);
2060   obstack_free (&table->nary_obstack, NULL);
2061   free_alloc_pool (table->phis_pool);
2062   free_alloc_pool (table->references_pool);
2063 }
2064
2065 static void
2066 init_scc_vn (void)
2067 {
2068   size_t i;
2069   int j;
2070   int *rpo_numbers_temp;
2071   basic_block bb;
2072   size_t id = 0;
2073
2074   calculate_dominance_info (CDI_DOMINATORS);
2075   sccstack = NULL;
2076   next_dfs_num = 1;
2077
2078   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
2079   /* VEC_alloc doesn't actually grow it to the right size, it just
2080      preallocates the space to do so.  */
2081   VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
2082   gcc_obstack_init (&vn_ssa_aux_obstack);
2083
2084   shared_lookup_phiargs = NULL;
2085   shared_lookup_vops = NULL;
2086   shared_lookup_references = NULL;
2087   rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2088   rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2089   pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
2090
2091   /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
2092      the i'th block in RPO order is bb.  We want to map bb's to RPO
2093      numbers, so we need to rearrange this array.  */
2094   for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
2095     rpo_numbers[rpo_numbers_temp[j]] = j;
2096
2097   XDELETE (rpo_numbers_temp);
2098
2099   VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
2100
2101   /* Create the VN_INFO structures, and initialize value numbers to
2102      TOP.  */
2103   for (i = 0; i < num_ssa_names; i++)
2104     {
2105       tree name = ssa_name (i);
2106       if (name)
2107         {
2108           VN_INFO_GET (name)->valnum = VN_TOP;
2109           VN_INFO (name)->expr = name;
2110         }
2111     }
2112
2113   FOR_ALL_BB (bb)
2114     {
2115       block_stmt_iterator bsi;
2116       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2117         {
2118           tree stmt = bsi_stmt (bsi);
2119           stmt_ann (stmt)->uid = id++;
2120         }
2121     }
2122
2123   /* Create the valid and optimistic value numbering tables.  */
2124   valid_info = XCNEW (struct vn_tables_s);
2125   allocate_vn_table (valid_info);
2126   optimistic_info = XCNEW (struct vn_tables_s);
2127   allocate_vn_table (optimistic_info);
2128   pre_info = NULL;
2129 }
2130
2131 void
2132 switch_to_PRE_table (void)
2133 {
2134   pre_info = XCNEW (struct vn_tables_s);
2135   allocate_vn_table (pre_info);
2136   current_info = pre_info;
2137 }
2138
2139 void
2140 free_scc_vn (void)
2141 {
2142   size_t i;
2143
2144   VEC_free (tree, heap, shared_lookup_phiargs);
2145   VEC_free (tree, gc, shared_lookup_vops);
2146   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
2147   XDELETEVEC (rpo_numbers);
2148
2149   for (i = 0; i < num_ssa_names; i++)
2150     {
2151       tree name = ssa_name (i);
2152       if (name
2153           && SSA_NAME_VALUE (name)
2154           && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2155         SSA_NAME_VALUE (name) = NULL;
2156       if (name
2157           && VN_INFO (name)->needs_insertion)
2158         release_ssa_name (name);
2159     }
2160   obstack_free (&vn_ssa_aux_obstack, NULL);
2161   VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
2162
2163   VEC_free (tree, heap, sccstack);
2164   free_vn_table (valid_info);
2165   XDELETE (valid_info);
2166   free_vn_table (optimistic_info);
2167   XDELETE (optimistic_info);
2168   if (pre_info)
2169     {
2170       free_vn_table (pre_info);
2171       XDELETE (pre_info);
2172     }
2173 }
2174
2175 /* Do SCCVN.  Returns true if it finished, false if we bailed out
2176    due to ressource constraints.  */
2177
2178 bool
2179 run_scc_vn (bool may_insert_arg)
2180 {
2181   size_t i;
2182   tree param;
2183
2184   may_insert = may_insert_arg;
2185
2186   init_scc_vn ();
2187   current_info = valid_info;
2188
2189   for (param = DECL_ARGUMENTS (current_function_decl);
2190        param;
2191        param = TREE_CHAIN (param))
2192     {
2193       if (gimple_default_def (cfun, param) != NULL)
2194         {
2195           tree def = gimple_default_def (cfun, param);
2196           SSA_VAL (def) = def;
2197         }
2198     }
2199
2200   for (i = 1; i < num_ssa_names; ++i)
2201     {
2202       tree name = ssa_name (i);
2203       if (name
2204           && VN_INFO (name)->visited == false
2205           && !has_zero_uses (name))
2206         if (!DFS (name))
2207           {
2208             free_scc_vn ();
2209             may_insert = false;
2210             return false;
2211           }
2212     }
2213
2214   if (dump_file && (dump_flags & TDF_DETAILS))
2215     {
2216       fprintf (dump_file, "Value numbers:\n");
2217       for (i = 0; i < num_ssa_names; i++)
2218         {
2219           tree name = ssa_name (i);
2220           if (name && VN_INFO (name)->visited
2221               && (SSA_VAL (name) != name
2222                   || is_gimple_min_invariant (VN_INFO (name)->expr)))
2223             {
2224               print_generic_expr (dump_file, name, 0);
2225               fprintf (dump_file, " = ");
2226               if (is_gimple_min_invariant (VN_INFO (name)->expr))
2227                 print_generic_expr (dump_file, VN_INFO (name)->expr, 0);
2228               else
2229                 print_generic_expr (dump_file, SSA_VAL (name), 0);
2230               fprintf (dump_file, "\n");
2231             }
2232         }
2233     }
2234
2235   may_insert = false;
2236   return true;
2237 }