OSDN Git Service

20d1498a2c994365436004a491b26a3c1ffe3e4e
[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       temp.type = TREE_TYPE (ref);
515       temp.opcode = TREE_CODE (ref);
516
517       switch (temp.opcode)
518         {
519         case ALIGN_INDIRECT_REF:
520         case MISALIGNED_INDIRECT_REF:
521         case INDIRECT_REF:
522           /* The only operand is the address, which gets its own
523              vn_reference_op_s structure.  */
524           break;
525         case BIT_FIELD_REF:
526           /* Record bits and position.  */
527           temp.op0 = TREE_OPERAND (ref, 1);
528           temp.op1 = TREE_OPERAND (ref, 2);
529           break;
530         case COMPONENT_REF:
531           /* If this is a reference to a union member, record the union
532              member size as operand.  Do so only if we are doing
533              expression insertion (during FRE), as PRE currently gets
534              confused with this.  */
535           if (may_insert
536               && TREE_CODE (DECL_CONTEXT (TREE_OPERAND (ref, 1))) == UNION_TYPE
537               && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (ref, 1)))
538               && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1))))
539             {
540               temp.type = NULL_TREE;
541               temp.op0 = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)));
542             }
543           else
544             /* Record field as operand.  */
545             temp.op0 = TREE_OPERAND (ref, 1);
546           break;
547         case ARRAY_RANGE_REF:
548         case ARRAY_REF:
549           /* Record index as operand.  */
550           temp.op0 = TREE_OPERAND (ref, 1);
551           temp.op1 = TREE_OPERAND (ref, 3);
552           break;
553         case STRING_CST:
554         case INTEGER_CST:
555         case COMPLEX_CST:
556         case VECTOR_CST:
557         case REAL_CST:
558         case CONSTRUCTOR:
559         case VALUE_HANDLE:
560         case VAR_DECL:
561         case PARM_DECL:
562         case CONST_DECL:
563         case RESULT_DECL:
564         case SSA_NAME:
565           temp.op0 = ref;
566           break;
567           /* These are only interesting for their operands, their
568              existence, and their type.  They will never be the last
569              ref in the chain of references (IE they require an
570              operand), so we don't have to put anything
571              for op* as it will be handled by the iteration  */
572         case IMAGPART_EXPR:
573         case REALPART_EXPR:
574         case VIEW_CONVERT_EXPR:
575         case ADDR_EXPR:
576           break;
577         default:
578           gcc_unreachable ();
579
580         }
581       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
582
583       if (REFERENCE_CLASS_P (ref) || TREE_CODE (ref) == ADDR_EXPR)
584         ref = TREE_OPERAND (ref, 0);
585       else
586         ref = NULL_TREE;
587     }
588 }
589
590 /* Create a vector of vn_reference_op_s structures from REF, a
591    REFERENCE_CLASS_P tree.  The vector is not shared. */
592
593 static VEC(vn_reference_op_s, heap) *
594 create_reference_ops_from_ref (tree ref)
595 {
596   VEC (vn_reference_op_s, heap) *result = NULL;
597
598   copy_reference_ops_from_ref (ref, &result);
599   return result;
600 }
601
602 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
603
604 /* Create a vector of vn_reference_op_s structures from REF, a
605    REFERENCE_CLASS_P tree.  The vector is shared among all callers of
606    this function.  */
607
608 static VEC(vn_reference_op_s, heap) *
609 shared_reference_ops_from_ref (tree ref)
610 {
611   if (!ref)
612     return NULL;
613   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
614   copy_reference_ops_from_ref (ref, &shared_lookup_references);
615   return shared_lookup_references;
616 }
617
618
619 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
620    structures into their value numbers.  This is done in-place, and
621    the vector passed in is returned.  */
622
623 static VEC (vn_reference_op_s, heap) *
624 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
625 {
626   vn_reference_op_t vro;
627   int i;
628
629   for (i = 0; VEC_iterate (vn_reference_op_s, orig, i, vro); i++)
630     {
631       if (vro->opcode == SSA_NAME
632           || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
633         vro->op0 = SSA_VAL (vro->op0);
634     }
635
636   return orig;
637 }
638
639 /* Transform any SSA_NAME's in ORIG, a vector of vuse trees, into
640    their value numbers. This is done in-place, and the vector passed
641    in is returned.  */
642
643 static VEC (tree, gc) *
644 valueize_vuses (VEC (tree, gc) *orig)
645 {
646   bool made_replacement = false;
647   tree vuse;
648   int i;
649
650   for (i = 0; VEC_iterate (tree, orig, i, vuse); i++)
651     {
652       if (vuse != SSA_VAL (vuse))
653         {
654           made_replacement = true;
655           VEC_replace (tree, orig, i, SSA_VAL (vuse));
656         }
657     }
658
659   if (made_replacement && VEC_length (tree, orig) > 1)
660     sort_vuses (orig);
661
662   return orig;
663 }
664
665 /* Lookup OP in the current hash table, and return the resulting
666    value number if it exists in the hash table.  Return NULL_TREE if
667    it does not exist in the hash table. */
668
669 tree
670 vn_reference_lookup (tree op, VEC (tree, gc) *vuses)
671 {
672   void **slot;
673   struct vn_reference_s vr1;
674
675   vr1.vuses = valueize_vuses (vuses);
676   vr1.operands = valueize_refs (shared_reference_ops_from_ref (op));
677   vr1.hashcode = vn_reference_compute_hash (&vr1);
678   slot = htab_find_slot_with_hash (current_info->references, &vr1, vr1.hashcode,
679                                    NO_INSERT);
680   if (!slot && current_info == optimistic_info)
681     slot = htab_find_slot_with_hash (valid_info->references, &vr1, vr1.hashcode,
682                                      NO_INSERT);
683   if (!slot)
684     return NULL_TREE;
685
686   return ((vn_reference_t)*slot)->result;
687 }
688
689 /* Insert OP into the current hash table with a value number of
690    RESULT.  */
691
692 void
693 vn_reference_insert (tree op, tree result, VEC (tree, gc) *vuses)
694 {
695   void **slot;
696   vn_reference_t vr1;
697
698   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
699
700   vr1->vuses = valueize_vuses (vuses);
701   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
702   vr1->hashcode = vn_reference_compute_hash (vr1);
703   vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
704
705   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
706                                    INSERT);
707
708   /* Because we lookup stores using vuses, and value number failures
709      using the vdefs (see visit_reference_op_store for how and why),
710      it's possible that on failure we may try to insert an already
711      inserted store.  This is not wrong, there is no ssa name for a
712      store that we could use as a differentiator anyway.  Thus, unlike
713      the other lookup functions, you cannot gcc_assert (!*slot)
714      here.  */
715
716   /* But free the old slot in case of a collision.  */
717   if (*slot)
718     free_reference (*slot);
719
720   *slot = vr1;
721 }
722
723 /* Compute and return the hash value for nary operation VBO1.  */
724
725 static inline hashval_t
726 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
727 {
728   hashval_t hash = 0;
729   unsigned i;
730
731   for (i = 0; i < vno1->length; ++i)
732     if (TREE_CODE (vno1->op[i]) == SSA_NAME)
733       vno1->op[i] = SSA_VAL (vno1->op[i]);
734
735   if (vno1->length == 2
736       && commutative_tree_code (vno1->opcode)
737       && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
738     {
739       tree temp = vno1->op[0];
740       vno1->op[0] = vno1->op[1];
741       vno1->op[1] = temp;
742     }
743
744   for (i = 0; i < vno1->length; ++i)
745     hash += iterative_hash_expr (vno1->op[i], vno1->opcode);
746
747   return hash;
748 }
749
750 /* Return the computed hashcode for nary operation P1.  */
751
752 static hashval_t
753 vn_nary_op_hash (const void *p1)
754 {
755   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
756   return vno1->hashcode;
757 }
758
759 /* Compare nary operations P1 and P2 and return true if they are
760    equivalent.  */
761
762 static int
763 vn_nary_op_eq (const void *p1, const void *p2)
764 {
765   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
766   const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
767   unsigned i;
768
769   if (vno1->opcode != vno2->opcode
770       || vno1->type != vno2->type)
771     return false;
772
773   for (i = 0; i < vno1->length; ++i)
774     if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
775       return false;
776
777   return true;
778 }
779
780 /* Lookup OP in the current hash table, and return the resulting
781    value number if it exists in the hash table.  Return NULL_TREE if
782    it does not exist in the hash table. */
783
784 tree
785 vn_nary_op_lookup (tree op)
786 {
787   void **slot;
788   struct vn_nary_op_s vno1;
789   unsigned i;
790
791   vno1.opcode = TREE_CODE (op);
792   vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
793   vno1.type = TREE_TYPE (op);
794   for (i = 0; i < vno1.length; ++i)
795     vno1.op[i] = TREE_OPERAND (op, i);
796   vno1.hashcode = vn_nary_op_compute_hash (&vno1);
797   slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
798                                    NO_INSERT);
799   if (!slot && current_info == optimistic_info)
800     slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
801                                      NO_INSERT);
802   if (!slot)
803     return NULL_TREE;
804   return ((vn_nary_op_t)*slot)->result;
805 }
806
807 /* Insert OP into the current hash table with a value number of
808    RESULT.  */
809
810 void
811 vn_nary_op_insert (tree op, tree result)
812 {
813   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
814   void **slot;
815   vn_nary_op_t vno1;
816   unsigned i;
817
818   vno1 = obstack_alloc (&current_info->nary_obstack,
819                         (sizeof (struct vn_nary_op_s)
820                          - sizeof (tree) * (4 - length)));
821   vno1->opcode = TREE_CODE (op);
822   vno1->length = length;
823   vno1->type = TREE_TYPE (op);
824   for (i = 0; i < vno1->length; ++i)
825     vno1->op[i] = TREE_OPERAND (op, i);
826   vno1->result = result;
827   vno1->hashcode = vn_nary_op_compute_hash (vno1);
828   slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
829                                    INSERT);
830   gcc_assert (!*slot);
831
832   *slot = vno1;
833 }
834
835 /* Compute a hashcode for PHI operation VP1 and return it.  */
836
837 static inline hashval_t
838 vn_phi_compute_hash (vn_phi_t vp1)
839 {
840   hashval_t result = 0;
841   int i;
842   tree phi1op;
843
844   result = vp1->block->index;
845
846   for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
847     {
848       if (phi1op == VN_TOP)
849         continue;
850       result += iterative_hash_expr (phi1op, result);
851     }
852
853   return result;
854 }
855
856 /* Return the computed hashcode for phi operation P1.  */
857
858 static hashval_t
859 vn_phi_hash (const void *p1)
860 {
861   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
862   return vp1->hashcode;
863 }
864
865 /* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
866
867 static int
868 vn_phi_eq (const void *p1, const void *p2)
869 {
870   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
871   const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
872
873   if (vp1->block == vp2->block)
874     {
875       int i;
876       tree phi1op;
877
878       /* Any phi in the same block will have it's arguments in the
879          same edge order, because of how we store phi nodes.  */
880       for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
881         {
882           tree phi2op = VEC_index (tree, vp2->phiargs, i);
883           if (phi1op == VN_TOP || phi2op == VN_TOP)
884             continue;
885           if (!expressions_equal_p (phi1op, phi2op))
886             return false;
887         }
888       return true;
889     }
890   return false;
891 }
892
893 static VEC(tree, heap) *shared_lookup_phiargs;
894
895 /* Lookup PHI in the current hash table, and return the resulting
896    value number if it exists in the hash table.  Return NULL_TREE if
897    it does not exist in the hash table. */
898
899 static tree
900 vn_phi_lookup (tree phi)
901 {
902   void **slot;
903   struct vn_phi_s vp1;
904   int i;
905
906   VEC_truncate (tree, shared_lookup_phiargs, 0);
907
908   /* Canonicalize the SSA_NAME's to their value number.  */
909   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
910     {
911       tree def = PHI_ARG_DEF (phi, i);
912       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
913       VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
914     }
915   vp1.phiargs = shared_lookup_phiargs;
916   vp1.block = bb_for_stmt (phi);
917   vp1.hashcode = vn_phi_compute_hash (&vp1);
918   slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
919                                    NO_INSERT);
920   if (!slot && current_info == optimistic_info)
921     slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
922                                      NO_INSERT);
923   if (!slot)
924     return NULL_TREE;
925   return ((vn_phi_t)*slot)->result;
926 }
927
928 /* Insert PHI into the current hash table with a value number of
929    RESULT.  */
930
931 static void
932 vn_phi_insert (tree phi, tree result)
933 {
934   void **slot;
935   vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
936   int i;
937   VEC (tree, heap) *args = NULL;
938
939   /* Canonicalize the SSA_NAME's to their value number.  */
940   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
941     {
942       tree def = PHI_ARG_DEF (phi, i);
943       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
944       VEC_safe_push (tree, heap, args, def);
945     }
946   vp1->phiargs = args;
947   vp1->block = bb_for_stmt (phi);
948   vp1->result = result;
949   vp1->hashcode = vn_phi_compute_hash (vp1);
950
951   slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
952                                    INSERT);
953
954   /* Because we iterate over phi operations more than once, it's
955      possible the slot might already exist here, hence no assert.*/
956   *slot = vp1;
957 }
958
959
960 /* Print set of components in strongly connected component SCC to OUT. */
961
962 static void
963 print_scc (FILE *out, VEC (tree, heap) *scc)
964 {
965   tree var;
966   unsigned int i;
967
968   fprintf (out, "SCC consists of: ");
969   for (i = 0; VEC_iterate (tree, scc, i, var); i++)
970     {
971       print_generic_expr (out, var, 0);
972       fprintf (out, " ");
973     }
974   fprintf (out, "\n");
975 }
976
977 /* Set the value number of FROM to TO, return true if it has changed
978    as a result.  */
979
980 static inline bool
981 set_ssa_val_to (tree from, tree to)
982 {
983   tree currval;
984
985   if (from != to
986       && TREE_CODE (to) == SSA_NAME
987       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
988     to = from;
989
990   /* The only thing we allow as value numbers are VN_TOP, ssa_names
991      and invariants.  So assert that here.  */
992   gcc_assert (to != NULL_TREE
993               && (to == VN_TOP
994                   || TREE_CODE (to) == SSA_NAME
995                   || is_gimple_min_invariant (to)));
996
997   if (dump_file && (dump_flags & TDF_DETAILS))
998     {
999       fprintf (dump_file, "Setting value number of ");
1000       print_generic_expr (dump_file, from, 0);
1001       fprintf (dump_file, " to ");
1002       print_generic_expr (dump_file, to, 0);
1003       fprintf (dump_file, "\n");
1004     }
1005
1006   currval = SSA_VAL (from);
1007
1008   if (currval != to  && !operand_equal_p (currval, to, OEP_PURE_SAME))
1009     {
1010       SSA_VAL (from) = to;
1011       return true;
1012     }
1013   return false;
1014 }
1015
1016 /* Set all definitions in STMT to value number to themselves.
1017    Return true if a value number changed. */
1018
1019 static bool
1020 defs_to_varying (tree stmt)
1021 {
1022   bool changed = false;
1023   ssa_op_iter iter;
1024   def_operand_p defp;
1025
1026   FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
1027     {
1028       tree def = DEF_FROM_PTR (defp);
1029
1030       VN_INFO (def)->use_processed = true;
1031       changed |= set_ssa_val_to (def, def);
1032     }
1033   return changed;
1034 }
1035
1036 static tree
1037 try_to_simplify (tree stmt, tree rhs);
1038
1039 /* Visit a copy between LHS and RHS, return true if the value number
1040    changed.  */
1041
1042 static bool
1043 visit_copy (tree lhs, tree rhs)
1044 {
1045
1046   /* Follow chains of copies to their destination.  */
1047   while (SSA_VAL (rhs) != rhs && TREE_CODE (SSA_VAL (rhs)) == SSA_NAME)
1048     rhs = SSA_VAL (rhs);
1049
1050   /* The copy may have a more interesting constant filled expression
1051      (we don't, since we know our RHS is just an SSA name).  */
1052   VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
1053   VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
1054
1055   return set_ssa_val_to (lhs, rhs);
1056 }
1057
1058 /* Visit a unary operator RHS, value number it, and return true if the
1059    value number of LHS has changed as a result.  */
1060
1061 static bool
1062 visit_unary_op (tree lhs, tree op)
1063 {
1064   bool changed = false;
1065   tree result = vn_nary_op_lookup (op);
1066
1067   if (result)
1068     {
1069       changed = set_ssa_val_to (lhs, result);
1070     }
1071   else
1072     {
1073       changed = set_ssa_val_to (lhs, lhs);
1074       vn_nary_op_insert (op, lhs);
1075     }
1076
1077   return changed;
1078 }
1079
1080 /* Visit a binary operator RHS, value number it, and return true if the
1081    value number of LHS has changed as a result.  */
1082
1083 static bool
1084 visit_binary_op (tree lhs, tree op)
1085 {
1086   bool changed = false;
1087   tree result = vn_nary_op_lookup (op);
1088
1089   if (result)
1090     {
1091       changed = set_ssa_val_to (lhs, result);
1092     }
1093   else
1094     {
1095       changed = set_ssa_val_to (lhs, lhs);
1096       vn_nary_op_insert (op, lhs);
1097     }
1098
1099   return changed;
1100 }
1101
1102 /* Visit a load from a reference operator RHS, part of STMT, value number it,
1103    and return true if the value number of the LHS has changed as a result.  */
1104
1105 static bool
1106 visit_reference_op_load (tree lhs, tree op, tree stmt)
1107 {
1108   bool changed = false;
1109   tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt));
1110
1111   /* We handle type-punning through unions by value-numbering based
1112      on offset and size of the access.  Be prepared to handle a
1113      type-mismatch here via creating a VIEW_CONVERT_EXPR.  */
1114   if (result
1115       && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
1116     {
1117       /* We will be setting the value number of lhs to the value number
1118          of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
1119          So first simplify and lookup this expression to see if it
1120          is already available.  */
1121       tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
1122       if (stmt
1123           && !is_gimple_min_invariant (val)
1124           && TREE_CODE (val) != SSA_NAME)
1125         {
1126           tree tem = try_to_simplify (stmt, val);
1127           if (tem)
1128             val = tem;
1129         }
1130       result = val;
1131       if (!is_gimple_min_invariant (val)
1132           && TREE_CODE (val) != SSA_NAME)
1133         result = vn_nary_op_lookup (val);
1134       /* If the expression is not yet available, value-number lhs to
1135          a new SSA_NAME we create.  */
1136       if (!result && may_insert)
1137         {
1138           result = make_ssa_name (SSA_NAME_VAR (lhs), NULL_TREE);
1139           /* Initialize value-number information properly.  */
1140           VN_INFO_GET (result)->valnum = result;
1141           VN_INFO (result)->expr = val;
1142           VN_INFO (result)->needs_insertion = true;
1143           /* As all "inserted" statements are singleton SCCs, insert
1144              to the valid table.  This is strictly needed to
1145              avoid re-generating new value SSA_NAMEs for the same
1146              expression during SCC iteration over and over (the
1147              optimistic table gets cleared after each iteration).
1148              We do not need to insert into the optimistic table, as
1149              lookups there will fall back to the valid table.  */
1150           if (current_info == optimistic_info)
1151             {
1152               current_info = valid_info;
1153               vn_nary_op_insert (val, result);
1154               current_info = optimistic_info;
1155             }
1156           else
1157             vn_nary_op_insert (val, result);
1158           if (dump_file && (dump_flags & TDF_DETAILS))
1159             {
1160               fprintf (dump_file, "Inserting name ");
1161               print_generic_expr (dump_file, result, 0);
1162               fprintf (dump_file, " for expression ");
1163               print_generic_expr (dump_file, val, 0);
1164               fprintf (dump_file, "\n");
1165             }
1166         }
1167     }
1168
1169   if (result)
1170     {
1171       changed = set_ssa_val_to (lhs, result);
1172     }
1173   else
1174     {
1175       changed = set_ssa_val_to (lhs, lhs);
1176       vn_reference_insert (op, lhs, copy_vuses_from_stmt (stmt));
1177     }
1178
1179   return changed;
1180 }
1181
1182
1183 /* Visit a store to a reference operator LHS, part of STMT, value number it,
1184    and return true if the value number of the LHS has changed as a result.  */
1185
1186 static bool
1187 visit_reference_op_store (tree lhs, tree op, tree stmt)
1188 {
1189   bool changed = false;
1190   tree result;
1191   bool resultsame = false;
1192
1193   /* First we want to lookup using the *vuses* from the store and see
1194      if there the last store to this location with the same address
1195      had the same value.
1196
1197      The vuses represent the memory state before the store.  If the
1198      memory state, address, and value of the store is the same as the
1199      last store to this location, then this store will produce the
1200      same memory state as that store.
1201
1202      In this case the vdef versions for this store are value numbered to those
1203      vuse versions, since they represent the same memory state after
1204      this store.
1205
1206      Otherwise, the vdefs for the store are used when inserting into
1207      the table, since the store generates a new memory state.  */
1208
1209   result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt));
1210
1211   if (result)
1212     {
1213       if (TREE_CODE (result) == SSA_NAME)
1214         result = SSA_VAL (result);
1215       if (TREE_CODE (op) == SSA_NAME)
1216         op = SSA_VAL (op);
1217       resultsame = expressions_equal_p (result, op);
1218     }
1219
1220   if (!result || !resultsame)
1221     {
1222       VEC(tree, gc) *vdefs = copy_vdefs_from_stmt (stmt);
1223       int i;
1224       tree vdef;
1225
1226       if (dump_file && (dump_flags & TDF_DETAILS))
1227         {
1228           fprintf (dump_file, "No store match\n");
1229           fprintf (dump_file, "Value numbering store ");
1230           print_generic_expr (dump_file, lhs, 0);
1231           fprintf (dump_file, " to ");
1232           print_generic_expr (dump_file, op, 0);
1233           fprintf (dump_file, "\n");
1234         }
1235       /* Have to set value numbers before insert, since insert is
1236          going to valueize the references in-place.  */
1237       for (i = 0; VEC_iterate (tree, vdefs, i, vdef); i++)
1238         {
1239           VN_INFO (vdef)->use_processed = true;
1240           changed |= set_ssa_val_to (vdef, vdef);
1241         }
1242
1243       /* Do not insert structure copies into the tables.  */
1244       if (is_gimple_min_invariant (op)
1245           || is_gimple_reg (op))
1246         vn_reference_insert (lhs, op, vdefs);
1247     }
1248   else
1249     {
1250       /* We had a match, so value number the vdefs to have the value
1251          number of the vuses they came from.  */
1252       ssa_op_iter op_iter;
1253       def_operand_p var;
1254       vuse_vec_p vv;
1255
1256       if (dump_file && (dump_flags & TDF_DETAILS))
1257         fprintf (dump_file, "Store matched earlier value,"
1258                  "value numbering store vdefs to matching vuses.\n");
1259
1260       FOR_EACH_SSA_VDEF_OPERAND (var, vv, stmt, op_iter)
1261         {
1262           tree def = DEF_FROM_PTR (var);
1263           tree use;
1264
1265           /* Uh, if the vuse is a multiuse, we can't really do much
1266              here, sadly, since we don't know which value number of
1267              which vuse to use.  */
1268           if (VUSE_VECT_NUM_ELEM (*vv) != 1)
1269             use = def;
1270           else
1271             use = VUSE_ELEMENT_VAR (*vv, 0);
1272
1273           VN_INFO (def)->use_processed = true;
1274           changed |= set_ssa_val_to (def, SSA_VAL (use));
1275         }
1276     }
1277
1278   return changed;
1279 }
1280
1281 /* Visit and value number PHI, return true if the value number
1282    changed.  */
1283
1284 static bool
1285 visit_phi (tree phi)
1286 {
1287   bool changed = false;
1288   tree result;
1289   tree sameval = VN_TOP;
1290   bool allsame = true;
1291   int i;
1292
1293   /* TODO: We could check for this in init_sccvn, and replace this
1294      with a gcc_assert.  */
1295   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
1296     return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1297
1298   /* See if all non-TOP arguments have the same value.  TOP is
1299      equivalent to everything, so we can ignore it.  */
1300   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1301     {
1302       tree def = PHI_ARG_DEF (phi, i);
1303
1304       if (TREE_CODE (def) == SSA_NAME)
1305         def = SSA_VAL (def);
1306       if (def == VN_TOP)
1307         continue;
1308       if (sameval == VN_TOP)
1309         {
1310           sameval = def;
1311         }
1312       else
1313         {
1314           if (!expressions_equal_p (def, sameval))
1315             {
1316               allsame = false;
1317               break;
1318             }
1319         }
1320     }
1321
1322   /* If all value numbered to the same value, the phi node has that
1323      value.  */
1324   if (allsame)
1325     {
1326       if (is_gimple_min_invariant (sameval))
1327         {
1328           VN_INFO (PHI_RESULT (phi))->has_constants = true;
1329           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1330         }
1331       else
1332         {
1333           VN_INFO (PHI_RESULT (phi))->has_constants = false;
1334           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1335         }
1336
1337       if (TREE_CODE (sameval) == SSA_NAME)
1338         return visit_copy (PHI_RESULT (phi), sameval);
1339
1340       return set_ssa_val_to (PHI_RESULT (phi), sameval);
1341     }
1342
1343   /* Otherwise, see if it is equivalent to a phi node in this block.  */
1344   result = vn_phi_lookup (phi);
1345   if (result)
1346     {
1347       if (TREE_CODE (result) == SSA_NAME)
1348         changed = visit_copy (PHI_RESULT (phi), result);
1349       else
1350         changed = set_ssa_val_to (PHI_RESULT (phi), result);
1351     }
1352   else
1353     {
1354       vn_phi_insert (phi, PHI_RESULT (phi));
1355       VN_INFO (PHI_RESULT (phi))->has_constants = false;
1356       VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
1357       changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1358     }
1359
1360   return changed;
1361 }
1362
1363 /* Return true if EXPR contains constants.  */
1364
1365 static bool
1366 expr_has_constants (tree expr)
1367 {
1368   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1369     {
1370     case tcc_unary:
1371       return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
1372
1373     case tcc_binary:
1374       return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
1375         || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
1376       /* Constants inside reference ops are rarely interesting, but
1377          it can take a lot of looking to find them.  */
1378     case tcc_reference:
1379     case tcc_declaration:
1380       return false;
1381     default:
1382       return is_gimple_min_invariant (expr);
1383     }
1384   return false;
1385 }
1386
1387 /* Replace SSA_NAMES in expr with their value numbers, and return the
1388    result.
1389    This is performed in place. */
1390
1391 static tree
1392 valueize_expr (tree expr)
1393 {
1394   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1395     {
1396     case tcc_unary:
1397       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1398           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1399         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1400       break;
1401     case tcc_binary:
1402       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1403           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1404         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1405       if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
1406           && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
1407         TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
1408       break;
1409     default:
1410       break;
1411     }
1412   return expr;
1413 }
1414
1415 /* Simplify the binary expression RHS, and return the result if
1416    simplified. */
1417
1418 static tree
1419 simplify_binary_expression (tree stmt, tree rhs)
1420 {
1421   tree result = NULL_TREE;
1422   tree op0 = TREE_OPERAND (rhs, 0);
1423   tree op1 = TREE_OPERAND (rhs, 1);
1424
1425   /* This will not catch every single case we could combine, but will
1426      catch those with constants.  The goal here is to simultaneously
1427      combine constants between expressions, but avoid infinite
1428      expansion of expressions during simplification.  */
1429   if (TREE_CODE (op0) == SSA_NAME)
1430     {
1431       if (VN_INFO (op0)->has_constants)
1432         op0 = valueize_expr (VN_INFO (op0)->expr);
1433       else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
1434         op0 = SSA_VAL (op0);
1435     }
1436
1437   if (TREE_CODE (op1) == SSA_NAME)
1438     {
1439       if (VN_INFO (op1)->has_constants)
1440         op1 = valueize_expr (VN_INFO (op1)->expr);
1441       else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
1442         op1 = SSA_VAL (op1);
1443     }
1444
1445   /* Avoid folding if nothing changed.  */
1446   if (op0 == TREE_OPERAND (rhs, 0)
1447       && op1 == TREE_OPERAND (rhs, 1))
1448     return NULL_TREE;
1449
1450   fold_defer_overflow_warnings ();
1451
1452   result = fold_binary (TREE_CODE (rhs), TREE_TYPE (rhs), op0, op1);
1453
1454   fold_undefer_overflow_warnings (result && valid_gimple_expression_p (result),
1455                                   stmt, 0);
1456
1457   /* Make sure result is not a complex expression consisting
1458      of operators of operators (IE (a + b) + (a + c))
1459      Otherwise, we will end up with unbounded expressions if
1460      fold does anything at all.  */
1461   if (result && valid_gimple_expression_p (result))
1462     return result;
1463
1464   return NULL_TREE;
1465 }
1466
1467 /* Simplify the unary expression RHS, and return the result if
1468    simplified. */
1469
1470 static tree
1471 simplify_unary_expression (tree rhs)
1472 {
1473   tree result = NULL_TREE;
1474   tree op0 = TREE_OPERAND (rhs, 0);
1475
1476   if (TREE_CODE (op0) != SSA_NAME)
1477     return NULL_TREE;
1478
1479   if (VN_INFO (op0)->has_constants)
1480     op0 = valueize_expr (VN_INFO (op0)->expr);
1481   else if (TREE_CODE (rhs) == NOP_EXPR
1482            || TREE_CODE (rhs) == CONVERT_EXPR
1483            || TREE_CODE (rhs) == REALPART_EXPR
1484            || TREE_CODE (rhs) == IMAGPART_EXPR
1485            || TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
1486     {
1487       /* We want to do tree-combining on conversion-like expressions.
1488          Make sure we feed only SSA_NAMEs or constants to fold though.  */
1489       tree tem = valueize_expr (VN_INFO (op0)->expr);
1490       if (UNARY_CLASS_P (tem)
1491           || BINARY_CLASS_P (tem)
1492           || TREE_CODE (tem) == VIEW_CONVERT_EXPR
1493           || TREE_CODE (tem) == SSA_NAME
1494           || is_gimple_min_invariant (tem))
1495         op0 = tem;
1496     }
1497
1498   /* Avoid folding if nothing changed, but remember the expression.  */
1499   if (op0 == TREE_OPERAND (rhs, 0))
1500     return rhs;
1501
1502   result = fold_unary (TREE_CODE (rhs), TREE_TYPE (rhs), op0);
1503   if (result)
1504     {
1505       STRIP_USELESS_TYPE_CONVERSION (result);
1506       if (valid_gimple_expression_p (result))
1507         return result;
1508     }
1509
1510   return rhs;
1511 }
1512
1513 /* Try to simplify RHS using equivalences and constant folding.  */
1514
1515 static tree
1516 try_to_simplify (tree stmt, tree rhs)
1517 {
1518   /* For stores we can end up simplifying a SSA_NAME rhs.  Just return
1519      in this case, there is no point in doing extra work.  */
1520   if (TREE_CODE (rhs) == SSA_NAME)
1521     return rhs;
1522   else
1523     {
1524       switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1525         {
1526           /* For references, see if we find a result for the lookup,
1527              and use it if we do.  */
1528         case tcc_declaration:
1529           /* Pull out any truly constant values.  */
1530           if (TREE_READONLY (rhs)
1531               && TREE_STATIC (rhs)
1532               && DECL_INITIAL (rhs)
1533               && valid_gimple_expression_p (DECL_INITIAL (rhs)))
1534             return DECL_INITIAL (rhs);
1535
1536             /* Fallthrough. */
1537         case tcc_reference:
1538           /* Do not do full-blown reference lookup here.
1539              ???  But like for tcc_declaration, we should simplify
1540                   from constant initializers.  */
1541
1542           /* Fallthrough for some codes that can operate on registers.  */
1543           if (!(TREE_CODE (rhs) == REALPART_EXPR
1544                 || TREE_CODE (rhs) == IMAGPART_EXPR
1545                 || TREE_CODE (rhs) == VIEW_CONVERT_EXPR))
1546             break;
1547           /* We could do a little more with unary ops, if they expand
1548              into binary ops, but it's debatable whether it is worth it. */
1549         case tcc_unary:
1550           return simplify_unary_expression (rhs);
1551           break;
1552         case tcc_comparison:
1553         case tcc_binary:
1554           return simplify_binary_expression (stmt, rhs);
1555           break;
1556         default:
1557           break;
1558         }
1559     }
1560   return rhs;
1561 }
1562
1563 /* Visit and value number USE, return true if the value number
1564    changed. */
1565
1566 static bool
1567 visit_use (tree use)
1568 {
1569   bool changed = false;
1570   tree stmt = SSA_NAME_DEF_STMT (use);
1571   stmt_ann_t ann;
1572
1573   VN_INFO (use)->use_processed = true;
1574
1575   gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
1576   if (dump_file && (dump_flags & TDF_DETAILS)
1577       && !IS_EMPTY_STMT (stmt))
1578     {
1579       fprintf (dump_file, "Value numbering ");
1580       print_generic_expr (dump_file, use, 0);
1581       fprintf (dump_file, " stmt = ");
1582       print_generic_stmt (dump_file, stmt, 0);
1583     }
1584
1585   /* RETURN_EXPR may have an embedded MODIFY_STMT.  */
1586   if (TREE_CODE (stmt) == RETURN_EXPR
1587       && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
1588     stmt = TREE_OPERAND (stmt, 0);
1589
1590   ann = stmt_ann (stmt);
1591
1592   /* Handle uninitialized uses.  */
1593   if (IS_EMPTY_STMT (stmt))
1594     {
1595       changed = set_ssa_val_to (use, use);
1596     }
1597   else
1598     {
1599       if (TREE_CODE (stmt) == PHI_NODE)
1600         {
1601           changed = visit_phi (stmt);
1602         }
1603       else if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT
1604                || (ann && ann->has_volatile_ops)
1605                || tree_could_throw_p (stmt))
1606         {
1607           changed = defs_to_varying (stmt);
1608         }
1609       else
1610         {
1611           tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1612           tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1613           tree simplified;
1614
1615           STRIP_USELESS_TYPE_CONVERSION (rhs);
1616
1617           /* Shortcut for copies. Simplifying copies is pointless,
1618              since we copy the expression and value they represent.  */
1619           if (TREE_CODE (rhs) == SSA_NAME && TREE_CODE (lhs) == SSA_NAME)
1620             {
1621               changed = visit_copy (lhs, rhs);
1622               goto done;
1623             }
1624           simplified = try_to_simplify (stmt, rhs);
1625           if (simplified && simplified != rhs)
1626             {
1627               if (dump_file && (dump_flags & TDF_DETAILS))
1628                 {
1629                   fprintf (dump_file, "RHS ");
1630                   print_generic_expr (dump_file, rhs, 0);
1631                   fprintf (dump_file, " simplified to ");
1632                   print_generic_expr (dump_file, simplified, 0);
1633                   if (TREE_CODE (lhs) == SSA_NAME)
1634                     fprintf (dump_file, " has constants %d\n",
1635                              VN_INFO (lhs)->has_constants);
1636                   else
1637                     fprintf (dump_file, "\n");
1638
1639                 }
1640             }
1641           /* Setting value numbers to constants will occasionally
1642              screw up phi congruence because constants are not
1643              uniquely associated with a single ssa name that can be
1644              looked up.  */
1645           if (simplified && is_gimple_min_invariant (simplified)
1646               && TREE_CODE (lhs) == SSA_NAME
1647               && simplified != rhs)
1648             {
1649               VN_INFO (lhs)->expr = simplified;
1650               VN_INFO (lhs)->has_constants = true;
1651               changed = set_ssa_val_to (lhs, simplified);
1652               goto done;
1653             }
1654           else if (simplified && TREE_CODE (simplified) == SSA_NAME
1655                    && TREE_CODE (lhs) == SSA_NAME)
1656             {
1657               changed = visit_copy (lhs, simplified);
1658               goto done;
1659             }
1660           else if (simplified)
1661             {
1662               if (TREE_CODE (lhs) == SSA_NAME)
1663                 {
1664                   VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
1665                   /* We have to unshare the expression or else
1666                      valuizing may change the IL stream.  */
1667                   VN_INFO (lhs)->expr = unshare_expr (simplified);
1668                 }
1669               rhs = simplified;
1670             }
1671           else if (expr_has_constants (rhs) && TREE_CODE (lhs) == SSA_NAME)
1672             {
1673               VN_INFO (lhs)->has_constants = true;
1674               VN_INFO (lhs)->expr = unshare_expr (rhs);
1675             }
1676           else if (TREE_CODE (lhs) == SSA_NAME)
1677             {
1678               /* We reset expr and constantness here because we may
1679                  have been value numbering optimistically, and
1680                  iterating. They may become non-constant in this case,
1681                  even if they were optimistically constant. */
1682
1683               VN_INFO (lhs)->has_constants = false;
1684               VN_INFO (lhs)->expr = lhs;
1685             }
1686
1687           if (TREE_CODE (lhs) == SSA_NAME
1688               /* We can substitute SSA_NAMEs that are live over
1689                  abnormal edges with their constant value.  */
1690               && !is_gimple_min_invariant (rhs)
1691               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1692             changed = defs_to_varying (stmt);
1693           else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
1694             {
1695               changed = visit_reference_op_store (lhs, rhs, stmt);
1696             }
1697           else if (TREE_CODE (lhs) == SSA_NAME)
1698             {
1699               if (is_gimple_min_invariant (rhs))
1700                 {
1701                   VN_INFO (lhs)->has_constants = true;
1702                   VN_INFO (lhs)->expr = rhs;
1703                   changed = set_ssa_val_to (lhs, rhs);
1704                 }
1705               else
1706                 {
1707                   switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1708                     {
1709                     case tcc_unary:
1710                       changed = visit_unary_op (lhs, rhs);
1711                       break;
1712                     case tcc_binary:
1713                       changed = visit_binary_op (lhs, rhs);
1714                       break;
1715                       /* If tcc_vl_expr ever encompasses more than
1716                          CALL_EXPR, this will need to be changed.  */
1717                     case tcc_vl_exp:
1718                       if (call_expr_flags (rhs)  & (ECF_PURE | ECF_CONST))
1719                         changed = visit_reference_op_load (lhs, rhs, stmt);
1720                       else
1721                         changed = defs_to_varying (stmt);
1722                       break;
1723                     case tcc_declaration:
1724                     case tcc_reference:
1725                       changed = visit_reference_op_load (lhs, rhs, stmt);
1726                       break;
1727                     case tcc_expression:
1728                       if (TREE_CODE (rhs) == ADDR_EXPR)
1729                         {
1730                           changed = visit_unary_op (lhs, rhs);
1731                           goto done;
1732                         }
1733                       /* Fallthrough.  */
1734                     default:
1735                       changed = defs_to_varying (stmt);
1736                       break;
1737                     }
1738                 }
1739             }
1740           else
1741             changed = defs_to_varying (stmt);
1742         }
1743     }
1744  done:
1745   return changed;
1746 }
1747
1748 /* Compare two operands by reverse postorder index */
1749
1750 static int
1751 compare_ops (const void *pa, const void *pb)
1752 {
1753   const tree opa = *((const tree *)pa);
1754   const tree opb = *((const tree *)pb);
1755   tree opstmta = SSA_NAME_DEF_STMT (opa);
1756   tree opstmtb = SSA_NAME_DEF_STMT (opb);
1757   basic_block bba;
1758   basic_block bbb;
1759
1760   if (IS_EMPTY_STMT (opstmta) && IS_EMPTY_STMT (opstmtb))
1761     return 0;
1762   else if (IS_EMPTY_STMT (opstmta))
1763     return -1;
1764   else if (IS_EMPTY_STMT (opstmtb))
1765     return 1;
1766
1767   bba = bb_for_stmt (opstmta);
1768   bbb = bb_for_stmt (opstmtb);
1769
1770   if (!bba && !bbb)
1771     return 0;
1772   else if (!bba)
1773     return -1;
1774   else if (!bbb)
1775     return 1;
1776
1777   if (bba == bbb)
1778     {
1779       if (TREE_CODE (opstmta) == PHI_NODE && TREE_CODE (opstmtb) == PHI_NODE)
1780         return 0;
1781       else if (TREE_CODE (opstmta) == PHI_NODE)
1782         return -1;
1783       else if (TREE_CODE (opstmtb) == PHI_NODE)
1784         return 1;
1785       return stmt_ann (opstmta)->uid - stmt_ann (opstmtb)->uid;
1786     }
1787   return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
1788 }
1789
1790 /* Sort an array containing members of a strongly connected component
1791    SCC so that the members are ordered by RPO number.
1792    This means that when the sort is complete, iterating through the
1793    array will give you the members in RPO order.  */
1794
1795 static void
1796 sort_scc (VEC (tree, heap) *scc)
1797 {
1798   qsort (VEC_address (tree, scc),
1799          VEC_length (tree, scc),
1800          sizeof (tree),
1801          compare_ops);
1802 }
1803
1804 /* Process a strongly connected component in the SSA graph.  */
1805
1806 static void
1807 process_scc (VEC (tree, heap) *scc)
1808 {
1809   /* If the SCC has a single member, just visit it.  */
1810
1811   if (VEC_length (tree, scc) == 1)
1812     {
1813       tree use = VEC_index (tree, scc, 0);
1814       if (!VN_INFO (use)->use_processed)
1815         visit_use (use);
1816     }
1817   else
1818     {
1819       tree var;
1820       unsigned int i;
1821       unsigned int iterations = 0;
1822       bool changed = true;
1823
1824       /* Iterate over the SCC with the optimistic table until it stops
1825          changing.  */
1826       current_info = optimistic_info;
1827       while (changed)
1828         {
1829           changed = false;
1830           iterations++;
1831           htab_empty (optimistic_info->nary);
1832           htab_empty (optimistic_info->phis);
1833           htab_empty (optimistic_info->references);
1834           obstack_free (&optimistic_info->nary_obstack, NULL);
1835           gcc_obstack_init (&optimistic_info->nary_obstack);
1836           empty_alloc_pool (optimistic_info->phis_pool);
1837           empty_alloc_pool (optimistic_info->references_pool);
1838           for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1839             changed |= visit_use (var);
1840         }
1841
1842       if (dump_file && (dump_flags & TDF_STATS))
1843         fprintf (dump_file, "Processing SCC required %d iterations\n",
1844                  iterations);
1845
1846       /* Finally, visit the SCC once using the valid table.  */
1847       current_info = valid_info;
1848       for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1849         visit_use (var);
1850     }
1851 }
1852
1853 /* Depth first search on NAME to discover and process SCC's in the SSA
1854    graph.
1855    Execution of this algorithm relies on the fact that the SCC's are
1856    popped off the stack in topological order.
1857    Returns true if successful, false if we stopped processing SCC's due
1858    to ressource constraints.  */
1859
1860 static bool
1861 DFS (tree name)
1862 {
1863   ssa_op_iter iter;
1864   use_operand_p usep;
1865   tree defstmt;
1866
1867   /* SCC info */
1868   VN_INFO (name)->dfsnum = next_dfs_num++;
1869   VN_INFO (name)->visited = true;
1870   VN_INFO (name)->low = VN_INFO (name)->dfsnum;
1871
1872   VEC_safe_push (tree, heap, sccstack, name);
1873   VN_INFO (name)->on_sccstack = true;
1874   defstmt = SSA_NAME_DEF_STMT (name);
1875
1876   /* Recursively DFS on our operands, looking for SCC's.  */
1877   if (!IS_EMPTY_STMT (defstmt))
1878     {
1879       FOR_EACH_PHI_OR_STMT_USE (usep, SSA_NAME_DEF_STMT (name), iter,
1880                                 SSA_OP_ALL_USES)
1881         {
1882           tree use = USE_FROM_PTR (usep);
1883
1884           /* Since we handle phi nodes, we will sometimes get
1885              invariants in the use expression.  */
1886           if (TREE_CODE (use) != SSA_NAME)
1887             continue;
1888
1889           if (! (VN_INFO (use)->visited))
1890             {
1891               if (!DFS (use))
1892                 return false;
1893               VN_INFO (name)->low = MIN (VN_INFO (name)->low,
1894                                          VN_INFO (use)->low);
1895             }
1896           if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
1897               && VN_INFO (use)->on_sccstack)
1898             {
1899               VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
1900                                          VN_INFO (name)->low);
1901             }
1902         }
1903     }
1904
1905   /* See if we found an SCC.  */
1906   if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
1907     {
1908       VEC (tree, heap) *scc = NULL;
1909       tree x;
1910
1911       /* Found an SCC, pop the components off the SCC stack and
1912          process them.  */
1913       do
1914         {
1915           x = VEC_pop (tree, sccstack);
1916
1917           VN_INFO (x)->on_sccstack = false;
1918           VEC_safe_push (tree, heap, scc, x);
1919         } while (x != name);
1920
1921       /* Bail out of SCCVN in case a SCC turns out to be incredibly large.  */
1922       if (VEC_length (tree, scc)
1923             > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
1924         {
1925           if (dump_file)
1926             fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
1927                      "SCC size %u exceeding %u\n", VEC_length (tree, scc),
1928                      (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
1929           return false;
1930         }
1931
1932       if (VEC_length (tree, scc) > 1)
1933         sort_scc (scc);
1934
1935       if (dump_file && (dump_flags & TDF_DETAILS))
1936         print_scc (dump_file, scc);
1937
1938       process_scc (scc);
1939
1940       VEC_free (tree, heap, scc);
1941     }
1942
1943   return true;
1944 }
1945
1946 /* Allocate a value number table.  */
1947
1948 static void
1949 allocate_vn_table (vn_tables_t table)
1950 {
1951   table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
1952   table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
1953   table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
1954                                    free_reference);
1955
1956   gcc_obstack_init (&table->nary_obstack);
1957   table->phis_pool = create_alloc_pool ("VN phis",
1958                                         sizeof (struct vn_phi_s),
1959                                         30);
1960   table->references_pool = create_alloc_pool ("VN references",
1961                                               sizeof (struct vn_reference_s),
1962                                               30);
1963 }
1964
1965 /* Free a value number table.  */
1966
1967 static void
1968 free_vn_table (vn_tables_t table)
1969 {
1970   htab_delete (table->phis);
1971   htab_delete (table->nary);
1972   htab_delete (table->references);
1973   obstack_free (&table->nary_obstack, NULL);
1974   free_alloc_pool (table->phis_pool);
1975   free_alloc_pool (table->references_pool);
1976 }
1977
1978 static void
1979 init_scc_vn (void)
1980 {
1981   size_t i;
1982   int j;
1983   int *rpo_numbers_temp;
1984   basic_block bb;
1985   size_t id = 0;
1986
1987   calculate_dominance_info (CDI_DOMINATORS);
1988   sccstack = NULL;
1989   next_dfs_num = 1;
1990
1991   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
1992   /* VEC_alloc doesn't actually grow it to the right size, it just
1993      preallocates the space to do so.  */
1994   VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
1995   gcc_obstack_init (&vn_ssa_aux_obstack);
1996
1997   shared_lookup_phiargs = NULL;
1998   shared_lookup_vops = NULL;
1999   shared_lookup_references = NULL;
2000   rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2001   rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2002   pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
2003
2004   /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
2005      the i'th block in RPO order is bb.  We want to map bb's to RPO
2006      numbers, so we need to rearrange this array.  */
2007   for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
2008     rpo_numbers[rpo_numbers_temp[j]] = j;
2009
2010   XDELETE (rpo_numbers_temp);
2011
2012   VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
2013
2014   /* Create the VN_INFO structures, and initialize value numbers to
2015      TOP.  */
2016   for (i = 0; i < num_ssa_names; i++)
2017     {
2018       tree name = ssa_name (i);
2019       if (name)
2020         {
2021           VN_INFO_GET (name)->valnum = VN_TOP;
2022           VN_INFO (name)->expr = name;
2023         }
2024     }
2025
2026   FOR_ALL_BB (bb)
2027     {
2028       block_stmt_iterator bsi;
2029       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2030         {
2031           tree stmt = bsi_stmt (bsi);
2032           stmt_ann (stmt)->uid = id++;
2033         }
2034     }
2035
2036   /* Create the valid and optimistic value numbering tables.  */
2037   valid_info = XCNEW (struct vn_tables_s);
2038   allocate_vn_table (valid_info);
2039   optimistic_info = XCNEW (struct vn_tables_s);
2040   allocate_vn_table (optimistic_info);
2041   pre_info = NULL;
2042 }
2043
2044 void
2045 switch_to_PRE_table (void)
2046 {
2047   pre_info = XCNEW (struct vn_tables_s);
2048   allocate_vn_table (pre_info);
2049   current_info = pre_info;
2050 }
2051
2052 void
2053 free_scc_vn (void)
2054 {
2055   size_t i;
2056
2057   VEC_free (tree, heap, shared_lookup_phiargs);
2058   VEC_free (tree, gc, shared_lookup_vops);
2059   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
2060   XDELETEVEC (rpo_numbers);
2061
2062   for (i = 0; i < num_ssa_names; i++)
2063     {
2064       tree name = ssa_name (i);
2065       if (name
2066           && SSA_NAME_VALUE (name)
2067           && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2068         SSA_NAME_VALUE (name) = NULL;
2069       if (name
2070           && VN_INFO (name)->needs_insertion)
2071         release_ssa_name (name);
2072     }
2073   obstack_free (&vn_ssa_aux_obstack, NULL);
2074   VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
2075
2076   VEC_free (tree, heap, sccstack);
2077   free_vn_table (valid_info);
2078   XDELETE (valid_info);
2079   free_vn_table (optimistic_info);
2080   XDELETE (optimistic_info);
2081   if (pre_info)
2082     {
2083       free_vn_table (pre_info);
2084       XDELETE (pre_info);
2085     }
2086 }
2087
2088 /* Do SCCVN.  Returns true if it finished, false if we bailed out
2089    due to ressource constraints.  */
2090
2091 bool
2092 run_scc_vn (bool may_insert_arg)
2093 {
2094   size_t i;
2095   tree param;
2096
2097   may_insert = may_insert_arg;
2098
2099   init_scc_vn ();
2100   current_info = valid_info;
2101
2102   for (param = DECL_ARGUMENTS (current_function_decl);
2103        param;
2104        param = TREE_CHAIN (param))
2105     {
2106       if (gimple_default_def (cfun, param) != NULL)
2107         {
2108           tree def = gimple_default_def (cfun, param);
2109           SSA_VAL (def) = def;
2110         }
2111     }
2112
2113   for (i = 1; i < num_ssa_names; ++i)
2114     {
2115       tree name = ssa_name (i);
2116       if (name
2117           && VN_INFO (name)->visited == false
2118           && !has_zero_uses (name))
2119         if (!DFS (name))
2120           {
2121             free_scc_vn ();
2122             may_insert = false;
2123             return false;
2124           }
2125     }
2126
2127   if (dump_file && (dump_flags & TDF_DETAILS))
2128     {
2129       fprintf (dump_file, "Value numbers:\n");
2130       for (i = 0; i < num_ssa_names; i++)
2131         {
2132           tree name = ssa_name (i);
2133           if (name && VN_INFO (name)->visited
2134               && (SSA_VAL (name) != name
2135                   || is_gimple_min_invariant (VN_INFO (name)->expr)))
2136             {
2137               print_generic_expr (dump_file, name, 0);
2138               fprintf (dump_file, " = ");
2139               if (is_gimple_min_invariant (VN_INFO (name)->expr))
2140                 print_generic_expr (dump_file, VN_INFO (name)->expr, 0);
2141               else
2142                 print_generic_expr (dump_file, SSA_VAL (name), 0);
2143               fprintf (dump_file, "\n");
2144             }
2145         }
2146     }
2147
2148   may_insert = false;
2149   return true;
2150 }