OSDN Git Service

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