OSDN Git Service

2007-07-18 Daniel Berlin <dberlin@dberlin.org>
[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
1021   /* The only thing we allow as value numbers are VN_TOP, ssa_names
1022      and invariants.  So assert that here.  */
1023   gcc_assert (to != NULL_TREE
1024               && (to == VN_TOP
1025                   || TREE_CODE (to) == SSA_NAME
1026                   || is_gimple_min_invariant (to)));
1027
1028   if (dump_file && (dump_flags & TDF_DETAILS))
1029     {
1030       fprintf (dump_file, "Setting value number of ");
1031       print_generic_expr (dump_file, from, 0);
1032       fprintf (dump_file, " to ");
1033       print_generic_expr (dump_file, to, 0);
1034       fprintf (dump_file, "\n");
1035     }
1036
1037   currval = SSA_VAL (from);
1038
1039   if (currval != to  && !operand_equal_p (currval, to, OEP_PURE_SAME))
1040     {
1041       SSA_VAL (from) = to;
1042       return true;
1043     }
1044   return false;
1045 }
1046
1047 /* Set all definitions in STMT to value number to themselves.
1048    Return true if a value number changed. */
1049
1050 static bool
1051 defs_to_varying (tree stmt)
1052 {
1053   bool changed = false;
1054   ssa_op_iter iter;
1055   def_operand_p defp;
1056
1057   FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
1058     {
1059       tree def = DEF_FROM_PTR (defp);
1060
1061       VN_INFO (def)->use_processed = true;
1062       changed |= set_ssa_val_to (def, def);
1063     }
1064   return changed;
1065 }
1066
1067 /* Visit a copy between LHS and RHS, return true if the value number
1068    changed.  */
1069
1070 static bool
1071 visit_copy (tree lhs, tree rhs)
1072 {
1073
1074   /* Follow chains of copies to their destination.  */
1075   while (SSA_VAL (rhs) != rhs && TREE_CODE (SSA_VAL (rhs)) == SSA_NAME)
1076     rhs = SSA_VAL (rhs);
1077   
1078   /* The copy may have a more interesting constant filled expression
1079      (we don't, since we know our RHS is just an SSA name).  */
1080   VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
1081   VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
1082
1083   return set_ssa_val_to (lhs, rhs);
1084 }
1085
1086 /* Visit a unary operator RHS, value number it, and return true if the
1087    value number of LHS has changed as a result.  */
1088
1089 static bool
1090 visit_unary_op (tree lhs, tree op)
1091 {
1092   bool changed = false;
1093   tree result = vn_unary_op_lookup (op);
1094
1095   if (result)
1096     {
1097       changed = set_ssa_val_to (lhs, result);
1098     }
1099   else
1100     {
1101       changed = set_ssa_val_to (lhs, lhs);
1102       vn_unary_op_insert (op, lhs);
1103     }
1104
1105   return changed;
1106 }
1107
1108 /* Visit a binary operator RHS, value number it, and return true if the
1109    value number of LHS has changed as a result.  */
1110
1111 static bool
1112 visit_binary_op (tree lhs, tree op)
1113 {
1114   bool changed = false;
1115   tree result = vn_binary_op_lookup (op);
1116
1117   if (result)
1118     {
1119       changed = set_ssa_val_to (lhs, result);
1120     }
1121   else
1122     {
1123       changed = set_ssa_val_to (lhs, lhs);
1124       vn_binary_op_insert (op, lhs);
1125     }
1126
1127   return changed;
1128 }
1129
1130 /* Visit a load from a reference operator RHS, part of STMT, value number it,
1131    and return true if the value number of the LHS has changed as a result.  */
1132
1133 static bool
1134 visit_reference_op_load (tree lhs, tree op, tree stmt)
1135 {
1136   bool changed = false;
1137   tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt));
1138
1139   if (result)
1140     {
1141       changed = set_ssa_val_to (lhs, result);
1142     }
1143   else
1144     {
1145       changed = set_ssa_val_to (lhs, lhs);
1146       vn_reference_insert (op, lhs, copy_vuses_from_stmt (stmt));
1147     }
1148
1149   return changed;
1150 }
1151
1152
1153 /* Visit a store to a reference operator LHS, part of STMT, value number it,
1154    and return true if the value number of the LHS has changed as a result.  */
1155
1156 static bool
1157 visit_reference_op_store (tree lhs, tree op, tree stmt)
1158 {
1159   bool changed = false;
1160   tree result;
1161   bool resultsame = false;
1162
1163   /* First we want to lookup using the *vuses* from the store and see
1164      if there the last store to this location with the same address
1165      had the same value.
1166
1167      The vuses represent the memory state before the store.  If the
1168      memory state, address, and value of the store is the same as the
1169      last store to this location, then this store will produce the
1170      same memory state as that store.
1171
1172      In this case the vdef versions for this store are value numbered to those
1173      vuse versions, since they represent the same memory state after
1174      this store.
1175
1176      Otherwise, the vdefs for the store are used when inserting into
1177      the table, since the store generates a new memory state.  */
1178
1179   result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt));
1180
1181   if (result)
1182     {
1183       if (TREE_CODE (result) == SSA_NAME)
1184         result = SSA_VAL (result);
1185       resultsame = expressions_equal_p (result, op);
1186     }
1187
1188   if (!result || !resultsame)
1189     {
1190       VEC(tree, gc) *vdefs = copy_vdefs_from_stmt (stmt);
1191       int i;
1192       tree vdef;
1193
1194       if (dump_file && (dump_flags & TDF_DETAILS))
1195         {
1196           fprintf (dump_file, "No store match\n");
1197           fprintf (dump_file, "Value numbering store ");
1198           print_generic_expr (dump_file, lhs, 0);
1199           fprintf (dump_file, " to ");
1200           print_generic_expr (dump_file, op, 0);
1201           fprintf (dump_file, "\n");
1202         }
1203       /* Have to set value numbers before insert, since insert is
1204          going to valueize the references in-place.  */
1205       for (i = 0; VEC_iterate (tree, vdefs, i, vdef); i++)
1206         {
1207           VN_INFO (vdef)->use_processed = true;
1208           changed |= set_ssa_val_to (vdef, vdef);
1209         }
1210
1211       vn_reference_insert (lhs, op, vdefs);
1212     }
1213   else
1214     {
1215       /* We had a match, so value number the vdefs to have the value
1216          number of the vuses they came from.  */
1217       ssa_op_iter op_iter;
1218       def_operand_p var;
1219       vuse_vec_p vv;
1220
1221       if (dump_file && (dump_flags & TDF_DETAILS))
1222         fprintf (dump_file, "Store matched earlier value,"
1223                  "value numbering store vdefs to matching vuses.\n");
1224
1225       FOR_EACH_SSA_VDEF_OPERAND (var, vv, stmt, op_iter)
1226         {
1227           tree def = DEF_FROM_PTR (var);
1228           tree use;
1229
1230           /* Uh, if the vuse is a multiuse, we can't really do much
1231              here, sadly, since we don't know which value number of
1232              which vuse to use.  */
1233           if (VUSE_VECT_NUM_ELEM (*vv) != 1)
1234             use = def;
1235           else
1236             use = VUSE_ELEMENT_VAR (*vv, 0);
1237
1238           VN_INFO (def)->use_processed = true;
1239           changed |= set_ssa_val_to (def, SSA_VAL (use));
1240         }
1241     }
1242
1243   return changed;
1244 }
1245
1246 /* Visit and value number PHI, return true if the value number
1247    changed.  */
1248
1249 static bool
1250 visit_phi (tree phi)
1251 {
1252   bool changed = false;
1253   tree result;
1254   tree sameval = VN_TOP;
1255   bool allsame = true;
1256   int i;
1257
1258   /* See if all non-TOP arguments have the same value.  TOP is
1259      equivalent to everything, so we can ignore it.  */
1260   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1261     {
1262       tree def = PHI_ARG_DEF (phi, i);
1263
1264       if (TREE_CODE (def) == SSA_NAME)
1265         def = SSA_VAL (def);
1266       if (def == VN_TOP)
1267         continue;
1268       if (sameval == VN_TOP)
1269         {
1270           sameval = def;
1271         }
1272       else
1273         {
1274           if (!expressions_equal_p (def, sameval))
1275             {
1276               allsame = false;
1277               break;
1278             }
1279         }
1280     }
1281
1282   /* If all value numbered to the same value, the phi node has that
1283      value.  */
1284   if (allsame)
1285     {
1286       if (is_gimple_min_invariant (sameval))
1287         {
1288           VN_INFO (PHI_RESULT (phi))->has_constants = true;
1289           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1290         }
1291       else
1292         {
1293           VN_INFO (PHI_RESULT (phi))->has_constants = false;
1294           VN_INFO (PHI_RESULT (phi))->expr = sameval;
1295         }
1296       
1297       if (TREE_CODE (sameval) == SSA_NAME)
1298         return visit_copy (PHI_RESULT (phi), sameval);
1299       
1300       return set_ssa_val_to (PHI_RESULT (phi), sameval);
1301     }
1302
1303   /* Otherwise, see if it is equivalent to a phi node in this block.  */
1304   result = vn_phi_lookup (phi);
1305   if (result)
1306     {
1307       if (TREE_CODE (result) == SSA_NAME)
1308         changed = visit_copy (PHI_RESULT (phi), result);
1309       else
1310         changed = set_ssa_val_to (PHI_RESULT (phi), result);
1311     }
1312   else
1313     {
1314       vn_phi_insert (phi, PHI_RESULT (phi));
1315       VN_INFO (PHI_RESULT (phi))->has_constants = false;
1316       VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
1317       changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1318     }
1319
1320   return changed;
1321 }
1322
1323 /* Return true if EXPR contains constants.  */
1324
1325 static bool
1326 expr_has_constants (tree expr)
1327 {
1328   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1329     {
1330     case tcc_unary:
1331       return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
1332
1333     case tcc_binary:
1334       return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
1335         || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
1336       /* Constants inside reference ops are rarely interesting, but
1337          it can take a lot of looking to find them.  */
1338     case tcc_reference:
1339     case tcc_declaration:
1340       return false;
1341     default:
1342       return is_gimple_min_invariant (expr);
1343     }
1344   return false;
1345 }
1346
1347 /* Replace SSA_NAMES in expr with their value numbers, and return the
1348    result.
1349    This is performed in place. */
1350
1351 static tree
1352 valueize_expr (tree expr)
1353 {
1354   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1355     {
1356     case tcc_unary:
1357       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1358           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1359         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1360       break;
1361     case tcc_binary:
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       if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
1366           && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
1367         TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
1368       break;
1369     default:
1370       break;
1371     }
1372   return expr;
1373 }
1374
1375 /* Simplify the binary expression RHS, and return the result if
1376    simplified. */
1377
1378 static tree
1379 simplify_binary_expression (tree rhs)
1380 {
1381   tree result = NULL_TREE;
1382   tree op0 = TREE_OPERAND (rhs, 0);
1383   tree op1 = TREE_OPERAND (rhs, 1);
1384
1385   /* This will not catch every single case we could combine, but will
1386      catch those with constants.  The goal here is to simultaneously
1387      combine constants between expressions, but avoid infinite
1388      expansion of expressions during simplification.  */
1389   if (TREE_CODE (op0) == SSA_NAME)
1390     {
1391       if (VN_INFO (op0)->has_constants)
1392         op0 = valueize_expr (VN_INFO (op0)->expr);
1393       else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
1394         op0 = SSA_VAL (op0);
1395     }
1396
1397   if (TREE_CODE (op1) == SSA_NAME)
1398     {
1399       if (VN_INFO (op1)->has_constants)
1400         op1 = valueize_expr (VN_INFO (op1)->expr);
1401       else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
1402         op1 = SSA_VAL (op1);
1403     }
1404
1405   result = fold_binary (TREE_CODE (rhs), TREE_TYPE (rhs), op0, op1);
1406
1407   /* Make sure result is not a complex expression consisting
1408      of operators of operators (IE (a + b) + (a + c))
1409      Otherwise, we will end up with unbounded expressions if
1410      fold does anything at all.  */
1411   if (result && valid_gimple_expression_p (result))
1412     return result;
1413
1414   return NULL_TREE;
1415 }
1416
1417 /* Try to simplify RHS using equivalences and constant folding.  */
1418
1419 static tree
1420 try_to_simplify (tree stmt, tree rhs)
1421 {
1422   if (TREE_CODE (rhs) == SSA_NAME)
1423     {
1424       if (is_gimple_min_invariant (SSA_VAL (rhs)))
1425         return SSA_VAL (rhs);
1426       else if (VN_INFO (rhs)->has_constants)
1427         return VN_INFO (rhs)->expr;
1428     }
1429   else
1430     {
1431       switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1432         {
1433           /* For references, see if we find a result for the lookup,
1434              and use it if we do.  */
1435         case tcc_declaration:
1436           /* Pull out any truly constant values.  */
1437           if (TREE_READONLY (rhs)
1438               && TREE_STATIC (rhs)
1439               && DECL_INITIAL (rhs)
1440               && valid_gimple_expression_p (DECL_INITIAL (rhs)))
1441             return DECL_INITIAL (rhs);
1442
1443             /* Fallthrough. */
1444         case tcc_reference:
1445           {
1446             tree result = vn_reference_lookup (rhs,
1447                                                shared_vuses_from_stmt (stmt));
1448             if (result)
1449               return result;
1450           }
1451           break;
1452           /* We could do a little more with unary ops, if they expand
1453              into binary ops, but it's debatable whether it is worth it. */
1454         case tcc_unary:
1455           {
1456             tree result = NULL_TREE;
1457             tree op0 = TREE_OPERAND (rhs, 0);
1458             if (TREE_CODE (op0) == SSA_NAME && VN_INFO (op0)->has_constants)
1459               op0 = VN_INFO (op0)->expr;
1460             else if (TREE_CODE (op0) == SSA_NAME && SSA_VAL (op0) != op0)
1461               op0 = SSA_VAL (op0);
1462             result = fold_unary (TREE_CODE (rhs), TREE_TYPE (rhs), op0);
1463             if (result)
1464               return result;
1465           }
1466           break;
1467         case tcc_comparison:
1468         case tcc_binary:
1469           return simplify_binary_expression (rhs);
1470           break;
1471         default:
1472           break;
1473         }
1474     }
1475   return rhs;
1476 }
1477
1478 /* Visit and value number USE, return true if the value number
1479    changed. */
1480
1481 static bool
1482 visit_use (tree use)
1483 {
1484   bool changed = false;
1485   tree stmt = SSA_NAME_DEF_STMT (use);
1486   stmt_ann_t ann;
1487
1488   VN_INFO (use)->use_processed = true;
1489
1490   gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
1491   if (dump_file && (dump_flags & TDF_DETAILS))
1492     {
1493       fprintf (dump_file, "Value numbering ");
1494       print_generic_expr (dump_file, use, 0);
1495       fprintf (dump_file, " stmt = ");
1496       print_generic_stmt (dump_file, stmt, 0);
1497     }
1498
1499   /* RETURN_EXPR may have an embedded MODIFY_STMT.  */
1500   if (TREE_CODE (stmt) == RETURN_EXPR
1501       && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
1502     stmt = TREE_OPERAND (stmt, 0);
1503
1504   ann = stmt_ann (stmt);
1505
1506   /* Handle uninitialized uses.  */
1507   if (IS_EMPTY_STMT (stmt))
1508     {
1509       changed = set_ssa_val_to (use, use);
1510     }
1511   else
1512     {
1513       if (TREE_CODE (stmt) == PHI_NODE)
1514         {
1515           changed = visit_phi (stmt);
1516         }
1517       else if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT
1518                || (ann && ann->has_volatile_ops))
1519         {
1520           changed = defs_to_varying (stmt);
1521         }
1522       else
1523         {
1524           tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1525           tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1526           tree simplified;
1527
1528           STRIP_USELESS_TYPE_CONVERSION (rhs);
1529
1530           /* Shortcut for copies. Simplifying copies is pointless,
1531              since we copy the expression and value they represent.  */
1532           if (TREE_CODE (rhs) == SSA_NAME && TREE_CODE (lhs) == SSA_NAME)
1533             {
1534               changed = visit_copy (lhs, rhs);
1535               goto done;
1536             }
1537           simplified = try_to_simplify (stmt, rhs);
1538           if (simplified && simplified != rhs)
1539             {
1540               if (dump_file && (dump_flags & TDF_DETAILS))
1541                 {
1542                   fprintf (dump_file, "RHS ");
1543                   print_generic_expr (dump_file, rhs, 0);
1544                   fprintf (dump_file, " simplified to ");
1545                   print_generic_expr (dump_file, simplified, 0);
1546                   if (TREE_CODE (lhs) == SSA_NAME)
1547                     fprintf (dump_file, " has constants %d\n",
1548                              VN_INFO (lhs)->has_constants);
1549                   else
1550                     fprintf (dump_file, "\n");
1551
1552                 }
1553             }
1554           /* Setting value numbers to constants will occasionally
1555              screw up phi congruence because constants are not
1556              uniquely associated with a single ssa name that can be
1557              looked up.  */
1558           if (simplified && is_gimple_min_invariant (simplified)
1559               && TREE_CODE (lhs) == SSA_NAME
1560               && simplified != rhs)
1561             {
1562               VN_INFO (lhs)->expr = simplified;
1563               VN_INFO (lhs)->has_constants = true;
1564               changed = set_ssa_val_to (lhs, simplified);
1565               goto done;
1566             }
1567           else if (simplified && TREE_CODE (simplified) == SSA_NAME
1568                    && TREE_CODE (lhs) == SSA_NAME)
1569             {
1570               changed = visit_copy (lhs, simplified);
1571               goto done;
1572             }
1573           else if (simplified)
1574             {
1575               if (TREE_CODE (lhs) == SSA_NAME)
1576                 {
1577                   VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
1578                   /* We have to unshare the expression or else
1579                      valuizing may change the IL stream.  */
1580                   VN_INFO (lhs)->expr = unshare_expr (simplified);
1581                 }
1582               rhs = simplified;
1583             }
1584           else if (expr_has_constants (rhs) && TREE_CODE (lhs) == SSA_NAME)
1585             {
1586               VN_INFO (lhs)->has_constants = true;
1587               VN_INFO (lhs)->expr = unshare_expr (rhs);
1588             }
1589           else if (TREE_CODE (lhs) == SSA_NAME)
1590             {
1591               /* We reset expr and constantness here because we may
1592                  have been value numbering optimistically, and
1593                  iterating. They may become non-constant in this case,
1594                  even if they were optimistically constant. */
1595                  
1596               VN_INFO (lhs)->has_constants = false;
1597               VN_INFO (lhs)->expr = lhs;
1598             }
1599
1600           if (TREE_CODE (lhs) == SSA_NAME
1601               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1602             changed = defs_to_varying (stmt);
1603           else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
1604             {
1605               changed = visit_reference_op_store (lhs, rhs, stmt);
1606             }
1607           else if (TREE_CODE (lhs) == SSA_NAME)
1608             {
1609               if (is_gimple_min_invariant (rhs))
1610                 {
1611                   VN_INFO (lhs)->has_constants = true;
1612                   VN_INFO (lhs)->expr = rhs;
1613                   changed = set_ssa_val_to (lhs, rhs);
1614                 }
1615               else
1616                 {
1617                   switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1618                     {
1619                     case tcc_unary:
1620                       changed = visit_unary_op (lhs, rhs);
1621                       break;
1622                     case tcc_binary:
1623                       changed = visit_binary_op (lhs, rhs);
1624                       break;
1625                       /* If tcc_vl_expr ever encompasses more than
1626                          CALL_EXPR, this will need to be changed.  */
1627                     case tcc_vl_exp:
1628                       if (call_expr_flags (rhs)  & (ECF_PURE | ECF_CONST))
1629                         changed = visit_reference_op_load (lhs, rhs, stmt);
1630                       else
1631                         changed = defs_to_varying (stmt);
1632                       break;
1633                     case tcc_declaration:
1634                     case tcc_reference:
1635                       changed = visit_reference_op_load (lhs, rhs, stmt);
1636                       break;
1637                     case tcc_expression:
1638                       if (TREE_CODE (rhs) == ADDR_EXPR)
1639                         {
1640                           changed = visit_unary_op (lhs, rhs);
1641                           goto done;
1642                         }
1643                       /* Fallthrough.  */
1644                     default:
1645                       changed = defs_to_varying (stmt);
1646                       break;
1647                     }
1648                 }
1649             }
1650           else
1651             changed = defs_to_varying (stmt);
1652         }
1653     }
1654  done:
1655   return changed;
1656 }
1657
1658 /* Compare two operands by reverse postorder index */
1659
1660 static int
1661 compare_ops (const void *pa, const void *pb)
1662 {
1663   const tree opa = *((const tree *)pa);
1664   const tree opb = *((const tree *)pb);
1665   tree opstmta = SSA_NAME_DEF_STMT (opa);
1666   tree opstmtb = SSA_NAME_DEF_STMT (opb);
1667   basic_block bba;
1668   basic_block bbb;
1669
1670   if (IS_EMPTY_STMT (opstmta) && IS_EMPTY_STMT (opstmtb))
1671     return 0;
1672   else if (IS_EMPTY_STMT (opstmta))
1673     return -1;
1674   else if (IS_EMPTY_STMT (opstmtb))
1675     return 1;
1676
1677   bba = bb_for_stmt (opstmta);
1678   bbb = bb_for_stmt (opstmtb);
1679
1680   if (!bba && !bbb)
1681     return 0;
1682   else if (!bba)
1683     return -1;
1684   else if (!bbb)
1685     return 1;
1686
1687   if (bba == bbb)
1688     {
1689       if (TREE_CODE (opstmta) == PHI_NODE && TREE_CODE (opstmtb) == PHI_NODE)
1690         return 0;
1691       else if (TREE_CODE (opstmta) == PHI_NODE)
1692         return -1;
1693       else if (TREE_CODE (opstmtb) == PHI_NODE)
1694         return 1;
1695       return stmt_ann (opstmta)->uid - stmt_ann (opstmtb)->uid;
1696     }
1697   return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
1698 }
1699
1700 /* Sort an array containing members of a strongly connected component
1701    SCC so that the members are ordered by RPO number.
1702    This means that when the sort is complete, iterating through the
1703    array will give you the members in RPO order.  */
1704
1705 static void
1706 sort_scc (VEC (tree, heap) *scc)
1707 {
1708   qsort (VEC_address (tree, scc),
1709          VEC_length (tree, scc),
1710          sizeof (tree),
1711          compare_ops);
1712 }
1713
1714 /* Process a strongly connected component in the SSA graph.  */
1715
1716 static void
1717 process_scc (VEC (tree, heap) *scc)
1718 {
1719   /* If the SCC has a single member, just visit it.  */
1720
1721   if (VEC_length (tree, scc) == 1)
1722     {
1723       tree use = VEC_index (tree, scc, 0);
1724       if (!VN_INFO (use)->use_processed) 
1725         visit_use (use);
1726     }
1727   else
1728     {
1729       tree var;
1730       unsigned int i;
1731       unsigned int iterations = 0;
1732       bool changed = true;
1733
1734       /* Iterate over the SCC with the optimistic table until it stops
1735          changing.  */
1736       current_info = optimistic_info;
1737       while (changed)
1738         {
1739           changed = false;
1740           iterations++;
1741           for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1742             changed |= visit_use (var);
1743         }
1744
1745       if (dump_file && (dump_flags & TDF_STATS))
1746         fprintf (dump_file, "Processing SCC required %d iterations\n",
1747                  iterations);
1748
1749       /* Finally, visit the SCC once using the valid table.  */
1750       current_info = valid_info;
1751       for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1752         visit_use (var);
1753     }
1754 }
1755
1756 /* Depth first search on NAME to discover and process SCC's in the SSA
1757    graph.
1758    Execution of this algorithm relies on the fact that the SCC's are
1759    popped off the stack in topological order.  */
1760
1761 static void
1762 DFS (tree name)
1763 {
1764   ssa_op_iter iter;
1765   use_operand_p usep;
1766   tree defstmt;
1767
1768   /* SCC info */
1769   VN_INFO (name)->dfsnum = next_dfs_num++;
1770   VN_INFO (name)->visited = true;
1771   VN_INFO (name)->low = VN_INFO (name)->dfsnum;
1772
1773   VEC_safe_push (tree, heap, sccstack, name);
1774   VN_INFO (name)->on_sccstack = true;
1775   defstmt = SSA_NAME_DEF_STMT (name);
1776
1777   /* Recursively DFS on our operands, looking for SCC's.  */
1778   if (!IS_EMPTY_STMT (defstmt))
1779     {
1780       FOR_EACH_PHI_OR_STMT_USE (usep, SSA_NAME_DEF_STMT (name), iter,
1781                                 SSA_OP_ALL_USES)
1782         {
1783           tree use = USE_FROM_PTR (usep);
1784
1785           /* Since we handle phi nodes, we will sometimes get
1786              invariants in the use expression.  */
1787           if (TREE_CODE (use) != SSA_NAME)
1788             continue;
1789
1790           if (! (VN_INFO (use)->visited))
1791             {
1792               DFS (use);
1793               VN_INFO (name)->low = MIN (VN_INFO (name)->low,
1794                                          VN_INFO (use)->low);
1795             }
1796           if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
1797               && VN_INFO (use)->on_sccstack)
1798             {
1799               VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
1800                                          VN_INFO (name)->low);
1801             }
1802         }
1803     }
1804
1805   /* See if we found an SCC.  */
1806   if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
1807     {
1808       VEC (tree, heap) *scc = NULL;
1809       tree x;
1810
1811       /* Found an SCC, pop the components off the SCC stack and
1812          process them.  */
1813       do
1814         {
1815           x = VEC_pop (tree, sccstack);
1816
1817           VN_INFO (x)->on_sccstack = false;
1818           VEC_safe_push (tree, heap, scc, x);
1819         } while (x != name);
1820
1821       if (VEC_length (tree, scc) > 1)
1822         sort_scc (scc);
1823
1824       if (dump_file && (dump_flags & TDF_DETAILS))
1825         print_scc (dump_file, scc);
1826
1827       process_scc (scc);
1828
1829       VEC_free (tree, heap, scc);
1830     }
1831 }
1832
1833 static void
1834 free_phi (void *vp)
1835 {
1836   vn_phi_t phi = vp;
1837   VEC_free (tree, heap, phi->phiargs);
1838 }
1839
1840
1841 /* Free a reference operation structure VP.  */
1842
1843 static void
1844 free_reference (void *vp)
1845 {
1846   vn_reference_t vr = vp;
1847   VEC_free (vn_reference_op_s, heap, vr->operands);
1848 }
1849
1850 /* Allocate a value number table.  */
1851
1852 static void
1853 allocate_vn_table (vn_tables_t table)
1854 {
1855   table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
1856   table->unary = htab_create (23, vn_unary_op_hash, vn_unary_op_eq, NULL);
1857   table->binary = htab_create (23, vn_binary_op_hash, vn_binary_op_eq, NULL);
1858   table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
1859                                    free_reference);
1860
1861   table->unary_op_pool = create_alloc_pool ("VN unary operations",
1862                                             sizeof (struct vn_unary_op_s),
1863                                             30);
1864   table->binary_op_pool = create_alloc_pool ("VN binary operations",
1865                                              sizeof (struct vn_binary_op_s),
1866                                              30);
1867   table->phis_pool = create_alloc_pool ("VN phis",
1868                                         sizeof (struct vn_phi_s),
1869                                         30);
1870   table->references_pool = create_alloc_pool ("VN references",
1871                                               sizeof (struct vn_reference_s),
1872                                               30);
1873 }
1874
1875 /* Free a value number table.  */
1876
1877 static void
1878 free_vn_table (vn_tables_t table)
1879 {
1880   htab_delete (table->phis);
1881   htab_delete (table->unary);
1882   htab_delete (table->binary);
1883   htab_delete (table->references);
1884   free_alloc_pool (table->unary_op_pool);
1885   free_alloc_pool (table->binary_op_pool);
1886   free_alloc_pool (table->phis_pool);
1887   free_alloc_pool (table->references_pool);
1888 }
1889
1890 static void
1891 init_scc_vn (void)
1892 {
1893   size_t i;
1894   int j;
1895   int *rpo_numbers_temp;
1896   basic_block bb;
1897   size_t id = 0;
1898
1899   calculate_dominance_info (CDI_DOMINATORS);
1900   sccstack = NULL;
1901   next_dfs_num = 1;
1902
1903   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
1904   /* VEC_alloc doesn't actually grow it to the right size, it just
1905      preallocates the space to do so.  */
1906   VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
1907   shared_lookup_phiargs = NULL;
1908   shared_lookup_vops = NULL;
1909   shared_lookup_references = NULL;
1910   rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
1911   rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
1912   pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
1913
1914   /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
1915      the i'th block in RPO order is bb.  We want to map bb's to RPO
1916      numbers, so we need to rearrange this array.  */
1917   for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
1918     rpo_numbers[rpo_numbers_temp[j]] = j;
1919
1920   free (rpo_numbers_temp);
1921
1922   VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
1923
1924   /* Create the VN_INFO structures, and initialize value numbers to
1925      TOP.  */
1926   for (i = 0; i < num_ssa_names; i++)
1927     {
1928       tree name = ssa_name (i);
1929       if (name)
1930         {
1931           VN_INFO_GET (name)->valnum = VN_TOP;
1932           VN_INFO (name)->expr = name;
1933         }
1934     }
1935
1936   FOR_ALL_BB (bb)
1937     {
1938       block_stmt_iterator bsi;
1939       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1940         {
1941           tree stmt = bsi_stmt (bsi);
1942           stmt_ann (stmt)->uid = id++;
1943         }
1944     }
1945
1946   /* Create the valid and optimistic value numbering tables.  */
1947   valid_info = XCNEW (struct vn_tables_s);
1948   allocate_vn_table (valid_info);
1949   optimistic_info = XCNEW (struct vn_tables_s);
1950   allocate_vn_table (optimistic_info);
1951   pre_info = NULL;
1952 }
1953
1954 void
1955 switch_to_PRE_table (void)
1956 {
1957   pre_info = XCNEW (struct vn_tables_s);
1958   allocate_vn_table (pre_info);
1959   current_info = pre_info;
1960 }
1961
1962 void
1963 free_scc_vn (void)
1964 {
1965   size_t i;
1966
1967   VEC_free (tree, heap, shared_lookup_phiargs);
1968   VEC_free (tree, gc, shared_lookup_vops);
1969   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
1970   XDELETEVEC (rpo_numbers);
1971   for (i = 0; i < num_ssa_names; i++)
1972     {
1973       tree name = ssa_name (i);
1974       if (name)
1975         {
1976           XDELETE (VN_INFO (name));
1977           if (SSA_NAME_VALUE (name) &&
1978               TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
1979             SSA_NAME_VALUE (name) = NULL;
1980         }
1981     }
1982       
1983   VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
1984   VEC_free (tree, heap, sccstack);
1985   free_vn_table (valid_info);
1986   XDELETE (valid_info);
1987   free_vn_table (optimistic_info);
1988   XDELETE (optimistic_info);
1989   if (pre_info)
1990     {
1991       free_vn_table (pre_info);
1992       XDELETE (pre_info);
1993     }
1994 }
1995
1996 void
1997 run_scc_vn (void)
1998 {
1999   size_t i;
2000   tree param;
2001
2002   init_scc_vn ();
2003   current_info = valid_info;
2004
2005   for (param = DECL_ARGUMENTS (current_function_decl);
2006        param;
2007        param = TREE_CHAIN (param))
2008     {
2009       if (gimple_default_def (cfun, param) != NULL)
2010         {
2011           tree def = gimple_default_def (cfun, param);
2012           SSA_VAL (def) = def;
2013         }
2014     }
2015
2016   for (i = num_ssa_names - 1; i > 0; i--)
2017     {
2018       tree name = ssa_name (i);
2019       if (name
2020           && VN_INFO (name)->visited == false
2021           && !has_zero_uses (name))
2022         DFS (name);
2023     }
2024
2025   if (dump_file && (dump_flags & TDF_DETAILS))
2026     {
2027       fprintf (dump_file, "Value numbers:\n");
2028       for (i = 0; i < num_ssa_names; i++)
2029         {
2030           tree name = ssa_name (i);
2031           if (name && VN_INFO (name)->visited
2032               && (SSA_VAL (name) != name
2033                   || is_gimple_min_invariant (VN_INFO (name)->expr)))
2034             {
2035               print_generic_expr (dump_file, name, 0);
2036               fprintf (dump_file, " = ");
2037               if (is_gimple_min_invariant (VN_INFO (name)->expr))
2038                 print_generic_expr (dump_file, VN_INFO (name)->expr, 0);
2039               else
2040                 print_generic_expr (dump_file, SSA_VAL (name), 0);
2041               fprintf (dump_file, "\n");
2042             }
2043         }
2044     }
2045 }