OSDN Git Service

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