OSDN Git Service

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