OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-sccvn.c
1 /* SCC value numbering for trees
2    Copyright (C) 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Daniel Berlin <dan@dberlin.org>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "basic-block.h"
28 #include "tree-pretty-print.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-inline.h"
31 #include "tree-flow.h"
32 #include "gimple.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "fibheap.h"
36 #include "hashtab.h"
37 #include "tree-iterator.h"
38 #include "alloc-pool.h"
39 #include "tree-pass.h"
40 #include "flags.h"
41 #include "bitmap.h"
42 #include "langhooks.h"
43 #include "cfgloop.h"
44 #include "params.h"
45 #include "tree-ssa-propagate.h"
46 #include "tree-ssa-sccvn.h"
47
48 /* This algorithm is based on the SCC algorithm presented by Keith
49    Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
50    (http://citeseer.ist.psu.edu/41805.html).  In
51    straight line code, it is equivalent to a regular hash based value
52    numbering that is performed in reverse postorder.
53
54    For code with cycles, there are two alternatives, both of which
55    require keeping the hashtables separate from the actual list of
56    value numbers for SSA names.
57
58    1. Iterate value numbering in an RPO walk of the blocks, removing
59    all the entries from the hashtable after each iteration (but
60    keeping the SSA name->value number mapping between iterations).
61    Iterate until it does not change.
62
63    2. Perform value numbering as part of an SCC walk on the SSA graph,
64    iterating only the cycles in the SSA graph until they do not change
65    (using a separate, optimistic hashtable for value numbering the SCC
66    operands).
67
68    The second is not just faster in practice (because most SSA graph
69    cycles do not involve all the variables in the graph), it also has
70    some nice properties.
71
72    One of these nice properties is that when we pop an SCC off the
73    stack, we are guaranteed to have processed all the operands coming from
74    *outside of that SCC*, so we do not need to do anything special to
75    ensure they have value numbers.
76
77    Another nice property is that the SCC walk is done as part of a DFS
78    of the SSA graph, which makes it easy to perform combining and
79    simplifying operations at the same time.
80
81    The code below is deliberately written in a way that makes it easy
82    to separate the SCC walk from the other work it does.
83
84    In order to propagate constants through the code, we track which
85    expressions contain constants, and use those while folding.  In
86    theory, we could also track expressions whose value numbers are
87    replaced, in case we end up folding based on expression
88    identities.
89
90    In order to value number memory, we assign value numbers to vuses.
91    This enables us to note that, for example, stores to the same
92    address of the same value from the same starting memory states are
93    equivalent.
94    TODO:
95
96    1. We can iterate only the changing portions of the SCC's, but
97    I have not seen an SCC big enough for this to be a win.
98    2. If you differentiate between phi nodes for loops and phi nodes
99    for if-then-else, you can properly consider phi nodes in different
100    blocks for equivalence.
101    3. We could value number vuses in more cases, particularly, whole
102    structure copies.
103 */
104
105 /* The set of hashtables and alloc_pool's for their items.  */
106
107 typedef struct vn_tables_s
108 {
109   htab_t nary;
110   htab_t phis;
111   htab_t references;
112   struct obstack nary_obstack;
113   alloc_pool phis_pool;
114   alloc_pool references_pool;
115 } *vn_tables_t;
116
117 static htab_t constant_to_value_id;
118 static bitmap constant_value_ids;
119
120
121 /* Valid hashtables storing information we have proven to be
122    correct.  */
123
124 static vn_tables_t valid_info;
125
126 /* Optimistic hashtables storing information we are making assumptions about
127    during iterations.  */
128
129 static vn_tables_t optimistic_info;
130
131 /* Pointer to the set of hashtables that is currently being used.
132    Should always point to either the optimistic_info, or the
133    valid_info.  */
134
135 static vn_tables_t current_info;
136
137
138 /* Reverse post order index for each basic block.  */
139
140 static int *rpo_numbers;
141
142 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
143
144 /* This represents the top of the VN lattice, which is the universal
145    value.  */
146
147 tree VN_TOP;
148
149 /* Unique counter for our value ids.  */
150
151 static unsigned int next_value_id;
152
153 /* Next DFS number and the stack for strongly connected component
154    detection. */
155
156 static unsigned int next_dfs_num;
157 static VEC (tree, heap) *sccstack;
158
159
160 DEF_VEC_P(vn_ssa_aux_t);
161 DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
162
163 /* Table of vn_ssa_aux_t's, one per ssa_name.  The vn_ssa_aux_t objects
164    are allocated on an obstack for locality reasons, and to free them
165    without looping over the VEC.  */
166
167 static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
168 static struct obstack vn_ssa_aux_obstack;
169
170 /* Return the value numbering information for a given SSA name.  */
171
172 vn_ssa_aux_t
173 VN_INFO (tree name)
174 {
175   vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
176                                 SSA_NAME_VERSION (name));
177   gcc_checking_assert (res);
178   return res;
179 }
180
181 /* Set the value numbering info for a given SSA name to a given
182    value.  */
183
184 static inline void
185 VN_INFO_SET (tree name, vn_ssa_aux_t value)
186 {
187   VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
188                SSA_NAME_VERSION (name), value);
189 }
190
191 /* Initialize the value numbering info for a given SSA name.
192    This should be called just once for every SSA name.  */
193
194 vn_ssa_aux_t
195 VN_INFO_GET (tree name)
196 {
197   vn_ssa_aux_t newinfo;
198
199   newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
200   memset (newinfo, 0, sizeof (struct vn_ssa_aux));
201   if (SSA_NAME_VERSION (name) >= VEC_length (vn_ssa_aux_t, vn_ssa_aux_table))
202     VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table,
203                    SSA_NAME_VERSION (name) + 1);
204   VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
205                SSA_NAME_VERSION (name), newinfo);
206   return newinfo;
207 }
208
209
210 /* Get the representative expression for the SSA_NAME NAME.  Returns
211    the representative SSA_NAME if there is no expression associated with it.  */
212
213 tree
214 vn_get_expr_for (tree name)
215 {
216   vn_ssa_aux_t vn = VN_INFO (name);
217   gimple def_stmt;
218   tree expr = NULL_TREE;
219
220   if (vn->valnum == VN_TOP)
221     return name;
222
223   /* If the value-number is a constant it is the representative
224      expression.  */
225   if (TREE_CODE (vn->valnum) != SSA_NAME)
226     return vn->valnum;
227
228   /* Get to the information of the value of this SSA_NAME.  */
229   vn = VN_INFO (vn->valnum);
230
231   /* If the value-number is a constant it is the representative
232      expression.  */
233   if (TREE_CODE (vn->valnum) != SSA_NAME)
234     return vn->valnum;
235
236   /* Else if we have an expression, return it.  */
237   if (vn->expr != NULL_TREE)
238     return vn->expr;
239
240   /* Otherwise use the defining statement to build the expression.  */
241   def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
242
243   /* If the value number is a default-definition or a PHI result
244      use it directly.  */
245   if (gimple_nop_p (def_stmt)
246       || gimple_code (def_stmt) == GIMPLE_PHI)
247     return vn->valnum;
248
249   if (!is_gimple_assign (def_stmt))
250     return vn->valnum;
251
252   /* FIXME tuples.  This is incomplete and likely will miss some
253      simplifications.  */
254   switch (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)))
255     {
256     case tcc_reference:
257       if ((gimple_assign_rhs_code (def_stmt) == VIEW_CONVERT_EXPR
258            || gimple_assign_rhs_code (def_stmt) == REALPART_EXPR
259            || gimple_assign_rhs_code (def_stmt) == IMAGPART_EXPR)
260           && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
261         expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
262                             gimple_expr_type (def_stmt),
263                             TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
264       break;
265
266     case tcc_unary:
267       expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
268                           gimple_expr_type (def_stmt),
269                           gimple_assign_rhs1 (def_stmt));
270       break;
271
272     case tcc_binary:
273       expr = fold_build2 (gimple_assign_rhs_code (def_stmt),
274                           gimple_expr_type (def_stmt),
275                           gimple_assign_rhs1 (def_stmt),
276                           gimple_assign_rhs2 (def_stmt));
277       break;
278
279     default:;
280     }
281   if (expr == NULL_TREE)
282     return vn->valnum;
283
284   /* Cache the expression.  */
285   vn->expr = expr;
286
287   return expr;
288 }
289
290
291 /* Free a phi operation structure VP.  */
292
293 static void
294 free_phi (void *vp)
295 {
296   vn_phi_t phi = (vn_phi_t) vp;
297   VEC_free (tree, heap, phi->phiargs);
298 }
299
300 /* Free a reference operation structure VP.  */
301
302 static void
303 free_reference (void *vp)
304 {
305   vn_reference_t vr = (vn_reference_t) vp;
306   VEC_free (vn_reference_op_s, heap, vr->operands);
307 }
308
309 /* Hash table equality function for vn_constant_t.  */
310
311 static int
312 vn_constant_eq (const void *p1, const void *p2)
313 {
314   const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
315   const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
316
317   if (vc1->hashcode != vc2->hashcode)
318     return false;
319
320   return vn_constant_eq_with_type (vc1->constant, vc2->constant);
321 }
322
323 /* Hash table hash function for vn_constant_t.  */
324
325 static hashval_t
326 vn_constant_hash (const void *p1)
327 {
328   const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
329   return vc1->hashcode;
330 }
331
332 /* Lookup a value id for CONSTANT and return it.  If it does not
333    exist returns 0.  */
334
335 unsigned int
336 get_constant_value_id (tree constant)
337 {
338   void **slot;
339   struct vn_constant_s vc;
340
341   vc.hashcode = vn_hash_constant_with_type (constant);
342   vc.constant = constant;
343   slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
344                                    vc.hashcode, NO_INSERT);
345   if (slot)
346     return ((vn_constant_t)*slot)->value_id;
347   return 0;
348 }
349
350 /* Lookup a value id for CONSTANT, and if it does not exist, create a
351    new one and return it.  If it does exist, return it.  */
352
353 unsigned int
354 get_or_alloc_constant_value_id (tree constant)
355 {
356   void **slot;
357   struct vn_constant_s vc;
358   vn_constant_t vcp;
359
360   vc.hashcode = vn_hash_constant_with_type (constant);
361   vc.constant = constant;
362   slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
363                                    vc.hashcode, INSERT);
364   if (*slot)
365     return ((vn_constant_t)*slot)->value_id;
366
367   vcp = XNEW (struct vn_constant_s);
368   vcp->hashcode = vc.hashcode;
369   vcp->constant = constant;
370   vcp->value_id = get_next_value_id ();
371   *slot = (void *) vcp;
372   bitmap_set_bit (constant_value_ids, vcp->value_id);
373   return vcp->value_id;
374 }
375
376 /* Return true if V is a value id for a constant.  */
377
378 bool
379 value_id_constant_p (unsigned int v)
380 {
381   return bitmap_bit_p (constant_value_ids, v);
382 }
383
384 /* Compare two reference operands P1 and P2 for equality.  Return true if
385    they are equal, and false otherwise.  */
386
387 static int
388 vn_reference_op_eq (const void *p1, const void *p2)
389 {
390   const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
391   const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
392
393   return vro1->opcode == vro2->opcode
394     && types_compatible_p (vro1->type, vro2->type)
395     && expressions_equal_p (vro1->op0, vro2->op0)
396     && expressions_equal_p (vro1->op1, vro2->op1)
397     && expressions_equal_p (vro1->op2, vro2->op2);
398 }
399
400 /* Compute the hash for a reference operand VRO1.  */
401
402 static hashval_t
403 vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
404 {
405   result = iterative_hash_hashval_t (vro1->opcode, result);
406   if (vro1->op0)
407     result = iterative_hash_expr (vro1->op0, result);
408   if (vro1->op1)
409     result = iterative_hash_expr (vro1->op1, result);
410   if (vro1->op2)
411     result = iterative_hash_expr (vro1->op2, result);
412   return result;
413 }
414
415 /* Return the hashcode for a given reference operation P1.  */
416
417 static hashval_t
418 vn_reference_hash (const void *p1)
419 {
420   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
421   return vr1->hashcode;
422 }
423
424 /* Compute a hash for the reference operation VR1 and return it.  */
425
426 hashval_t
427 vn_reference_compute_hash (const vn_reference_t vr1)
428 {
429   hashval_t result = 0;
430   int i;
431   vn_reference_op_t vro;
432   HOST_WIDE_INT off = -1;
433   bool deref = false;
434
435   FOR_EACH_VEC_ELT (vn_reference_op_s, vr1->operands, i, vro)
436     {
437       if (vro->opcode == MEM_REF)
438         deref = true;
439       else if (vro->opcode != ADDR_EXPR)
440         deref = false;
441       if (vro->off != -1)
442         {
443           if (off == -1)
444             off = 0;
445           off += vro->off;
446         }
447       else
448         {
449           if (off != -1
450               && off != 0)
451             result = iterative_hash_hashval_t (off, result);
452           off = -1;
453           if (deref
454               && vro->opcode == ADDR_EXPR)
455             {
456               if (vro->op0)
457                 {
458                   tree op = TREE_OPERAND (vro->op0, 0);
459                   result = iterative_hash_hashval_t (TREE_CODE (op), result);
460                   result = iterative_hash_expr (op, result);
461                 }
462             }
463           else
464             result = vn_reference_op_compute_hash (vro, result);
465         }
466     }
467   if (vr1->vuse)
468     result += SSA_NAME_VERSION (vr1->vuse);
469
470   return result;
471 }
472
473 /* Return true if reference operations P1 and P2 are equivalent.  This
474    means they have the same set of operands and vuses.  */
475
476 int
477 vn_reference_eq (const void *p1, const void *p2)
478 {
479   unsigned i, j;
480
481   const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
482   const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
483   if (vr1->hashcode != vr2->hashcode)
484     return false;
485
486   /* Early out if this is not a hash collision.  */
487   if (vr1->hashcode != vr2->hashcode)
488     return false;
489
490   /* The VOP needs to be the same.  */
491   if (vr1->vuse != vr2->vuse)
492     return false;
493
494   /* If the operands are the same we are done.  */
495   if (vr1->operands == vr2->operands)
496     return true;
497
498   if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
499     return false;
500
501   if (INTEGRAL_TYPE_P (vr1->type)
502       && INTEGRAL_TYPE_P (vr2->type))
503     {
504       if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
505         return false;
506     }
507   else if (INTEGRAL_TYPE_P (vr1->type)
508            && (TYPE_PRECISION (vr1->type)
509                != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
510     return false;
511   else if (INTEGRAL_TYPE_P (vr2->type)
512            && (TYPE_PRECISION (vr2->type)
513                != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
514     return false;
515
516   i = 0;
517   j = 0;
518   do
519     {
520       HOST_WIDE_INT off1 = 0, off2 = 0;
521       vn_reference_op_t vro1, vro2;
522       vn_reference_op_s tem1, tem2;
523       bool deref1 = false, deref2 = false;
524       for (; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro1); i++)
525         {
526           if (vro1->opcode == MEM_REF)
527             deref1 = true;
528           if (vro1->off == -1)
529             break;
530           off1 += vro1->off;
531         }
532       for (; VEC_iterate (vn_reference_op_s, vr2->operands, j, vro2); j++)
533         {
534           if (vro2->opcode == MEM_REF)
535             deref2 = true;
536           if (vro2->off == -1)
537             break;
538           off2 += vro2->off;
539         }
540       if (off1 != off2)
541         return false;
542       if (deref1 && vro1->opcode == ADDR_EXPR)
543         {
544           memset (&tem1, 0, sizeof (tem1));
545           tem1.op0 = TREE_OPERAND (vro1->op0, 0);
546           tem1.type = TREE_TYPE (tem1.op0);
547           tem1.opcode = TREE_CODE (tem1.op0);
548           vro1 = &tem1;
549           deref1 = false;
550         }
551       if (deref2 && vro2->opcode == ADDR_EXPR)
552         {
553           memset (&tem2, 0, sizeof (tem2));
554           tem2.op0 = TREE_OPERAND (vro2->op0, 0);
555           tem2.type = TREE_TYPE (tem2.op0);
556           tem2.opcode = TREE_CODE (tem2.op0);
557           vro2 = &tem2;
558           deref2 = false;
559         }
560       if (deref1 != deref2)
561         return false;
562       if (!vn_reference_op_eq (vro1, vro2))
563         return false;
564       ++j;
565       ++i;
566     }
567   while (VEC_length (vn_reference_op_s, vr1->operands) != i
568          || VEC_length (vn_reference_op_s, vr2->operands) != j);
569
570   return true;
571 }
572
573 /* Copy the operations present in load/store REF into RESULT, a vector of
574    vn_reference_op_s's.  */
575
576 void
577 copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
578 {
579   if (TREE_CODE (ref) == TARGET_MEM_REF)
580     {
581       vn_reference_op_s temp;
582
583       memset (&temp, 0, sizeof (temp));
584       /* We do not care for spurious type qualifications.  */
585       temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
586       temp.opcode = TREE_CODE (ref);
587       temp.op0 = TMR_INDEX (ref);
588       temp.op1 = TMR_STEP (ref);
589       temp.op2 = TMR_OFFSET (ref);
590       temp.off = -1;
591       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
592
593       memset (&temp, 0, sizeof (temp));
594       temp.type = NULL_TREE;
595       temp.opcode = ERROR_MARK;
596       temp.op0 = TMR_INDEX2 (ref);
597       temp.off = -1;
598       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
599
600       memset (&temp, 0, sizeof (temp));
601       temp.type = NULL_TREE;
602       temp.opcode = TREE_CODE (TMR_BASE (ref));
603       temp.op0 = TMR_BASE (ref);
604       temp.off = -1;
605       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
606       return;
607     }
608
609   /* For non-calls, store the information that makes up the address.  */
610
611   while (ref)
612     {
613       vn_reference_op_s temp;
614
615       memset (&temp, 0, sizeof (temp));
616       /* We do not care for spurious type qualifications.  */
617       temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
618       temp.opcode = TREE_CODE (ref);
619       temp.off = -1;
620
621       switch (temp.opcode)
622         {
623         case MEM_REF:
624           /* The base address gets its own vn_reference_op_s structure.  */
625           temp.op0 = TREE_OPERAND (ref, 1);
626           if (host_integerp (TREE_OPERAND (ref, 1), 0))
627             temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
628           break;
629         case BIT_FIELD_REF:
630           /* Record bits and position.  */
631           temp.op0 = TREE_OPERAND (ref, 1);
632           temp.op1 = TREE_OPERAND (ref, 2);
633           break;
634         case COMPONENT_REF:
635           /* The field decl is enough to unambiguously specify the field,
636              a matching type is not necessary and a mismatching type
637              is always a spurious difference.  */
638           temp.type = NULL_TREE;
639           temp.op0 = TREE_OPERAND (ref, 1);
640           temp.op1 = TREE_OPERAND (ref, 2);
641           {
642             tree this_offset = component_ref_field_offset (ref);
643             if (this_offset
644                 && TREE_CODE (this_offset) == INTEGER_CST)
645               {
646                 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
647                 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
648                   {
649                     double_int off
650                       = double_int_add (tree_to_double_int (this_offset),
651                                         double_int_rshift
652                                           (tree_to_double_int (bit_offset),
653                                            BITS_PER_UNIT == 8
654                                            ? 3 : exact_log2 (BITS_PER_UNIT),
655                                            HOST_BITS_PER_DOUBLE_INT, true));
656                     if (double_int_fits_in_shwi_p (off))
657                       temp.off = off.low;
658                   }
659               }
660           }
661           break;
662         case ARRAY_RANGE_REF:
663         case ARRAY_REF:
664           /* Record index as operand.  */
665           temp.op0 = TREE_OPERAND (ref, 1);
666           /* Always record lower bounds and element size.  */
667           temp.op1 = array_ref_low_bound (ref);
668           temp.op2 = array_ref_element_size (ref);
669           if (TREE_CODE (temp.op0) == INTEGER_CST
670               && TREE_CODE (temp.op1) == INTEGER_CST
671               && TREE_CODE (temp.op2) == INTEGER_CST)
672             {
673               double_int off = tree_to_double_int (temp.op0);
674               off = double_int_add (off,
675                                     double_int_neg
676                                       (tree_to_double_int (temp.op1)));
677               off = double_int_mul (off, tree_to_double_int (temp.op2));
678               if (double_int_fits_in_shwi_p (off))
679                 temp.off = off.low;
680             }
681           break;
682         case STRING_CST:
683         case INTEGER_CST:
684         case COMPLEX_CST:
685         case VECTOR_CST:
686         case REAL_CST:
687         case CONSTRUCTOR:
688         case VAR_DECL:
689         case PARM_DECL:
690         case CONST_DECL:
691         case RESULT_DECL:
692         case SSA_NAME:
693           temp.op0 = ref;
694           break;
695         case ADDR_EXPR:
696           if (is_gimple_min_invariant (ref))
697             {
698               temp.op0 = ref;
699               break;
700             }
701           /* Fallthrough.  */
702           /* These are only interesting for their operands, their
703              existence, and their type.  They will never be the last
704              ref in the chain of references (IE they require an
705              operand), so we don't have to put anything
706              for op* as it will be handled by the iteration  */
707         case REALPART_EXPR:
708         case VIEW_CONVERT_EXPR:
709           temp.off = 0;
710           break;
711         case IMAGPART_EXPR:
712           /* This is only interesting for its constant offset.  */
713           temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
714           break;
715         default:
716           gcc_unreachable ();
717         }
718       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
719
720       if (REFERENCE_CLASS_P (ref)
721           || (TREE_CODE (ref) == ADDR_EXPR
722               && !is_gimple_min_invariant (ref)))
723         ref = TREE_OPERAND (ref, 0);
724       else
725         ref = NULL_TREE;
726     }
727 }
728
729 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
730    operands in *OPS, the reference alias set SET and the reference type TYPE.
731    Return true if something useful was produced.  */
732
733 bool
734 ao_ref_init_from_vn_reference (ao_ref *ref,
735                                alias_set_type set, tree type,
736                                VEC (vn_reference_op_s, heap) *ops)
737 {
738   vn_reference_op_t op;
739   unsigned i;
740   tree base = NULL_TREE;
741   tree *op0_p = &base;
742   HOST_WIDE_INT offset = 0;
743   HOST_WIDE_INT max_size;
744   HOST_WIDE_INT size = -1;
745   tree size_tree = NULL_TREE;
746   alias_set_type base_alias_set = -1;
747
748   /* First get the final access size from just the outermost expression.  */
749   op = VEC_index (vn_reference_op_s, ops, 0);
750   if (op->opcode == COMPONENT_REF)
751     size_tree = DECL_SIZE (op->op0);
752   else if (op->opcode == BIT_FIELD_REF)
753     size_tree = op->op0;
754   else
755     {
756       enum machine_mode mode = TYPE_MODE (type);
757       if (mode == BLKmode)
758         size_tree = TYPE_SIZE (type);
759       else
760         size = GET_MODE_BITSIZE (mode);
761     }
762   if (size_tree != NULL_TREE)
763     {
764       if (!host_integerp (size_tree, 1))
765         size = -1;
766       else
767         size = TREE_INT_CST_LOW (size_tree);
768     }
769
770   /* Initially, maxsize is the same as the accessed element size.
771      In the following it will only grow (or become -1).  */
772   max_size = size;
773
774   /* Compute cumulative bit-offset for nested component-refs and array-refs,
775      and find the ultimate containing object.  */
776   FOR_EACH_VEC_ELT (vn_reference_op_s, ops, i, op)
777     {
778       switch (op->opcode)
779         {
780         /* These may be in the reference ops, but we cannot do anything
781            sensible with them here.  */
782         case ADDR_EXPR:
783           /* Apart from ADDR_EXPR arguments to MEM_REF.  */
784           if (base != NULL_TREE
785               && TREE_CODE (base) == MEM_REF
786               && op->op0
787               && DECL_P (TREE_OPERAND (op->op0, 0)))
788             {
789               vn_reference_op_t pop = VEC_index (vn_reference_op_s, ops, i-1);
790               base = TREE_OPERAND (op->op0, 0);
791               if (pop->off == -1)
792                 {
793                   max_size = -1;
794                   offset = 0;
795                 }
796               else
797                 offset += pop->off * BITS_PER_UNIT;
798               op0_p = NULL;
799               break;
800             }
801           /* Fallthru.  */
802         case CALL_EXPR:
803           return false;
804
805         /* Record the base objects.  */
806         case MEM_REF:
807           base_alias_set = get_deref_alias_set (op->op0);
808           *op0_p = build2 (MEM_REF, op->type,
809                            NULL_TREE, op->op0);
810           op0_p = &TREE_OPERAND (*op0_p, 0);
811           break;
812
813         case VAR_DECL:
814         case PARM_DECL:
815         case RESULT_DECL:
816         case SSA_NAME:
817           *op0_p = op->op0;
818           op0_p = NULL;
819           break;
820
821         /* And now the usual component-reference style ops.  */
822         case BIT_FIELD_REF:
823           offset += tree_low_cst (op->op1, 0);
824           break;
825
826         case COMPONENT_REF:
827           {
828             tree field = op->op0;
829             /* We do not have a complete COMPONENT_REF tree here so we
830                cannot use component_ref_field_offset.  Do the interesting
831                parts manually.  */
832
833             if (op->op1
834                 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
835               max_size = -1;
836             else
837               {
838                 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
839                            * BITS_PER_UNIT);
840                 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
841               }
842             break;
843           }
844
845         case ARRAY_RANGE_REF:
846         case ARRAY_REF:
847           /* We recorded the lower bound and the element size.  */
848           if (!host_integerp (op->op0, 0)
849               || !host_integerp (op->op1, 0)
850               || !host_integerp (op->op2, 0))
851             max_size = -1;
852           else
853             {
854               HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
855               hindex -= TREE_INT_CST_LOW (op->op1);
856               hindex *= TREE_INT_CST_LOW (op->op2);
857               hindex *= BITS_PER_UNIT;
858               offset += hindex;
859             }
860           break;
861
862         case REALPART_EXPR:
863           break;
864
865         case IMAGPART_EXPR:
866           offset += size;
867           break;
868
869         case VIEW_CONVERT_EXPR:
870           break;
871
872         case STRING_CST:
873         case INTEGER_CST:
874         case COMPLEX_CST:
875         case VECTOR_CST:
876         case REAL_CST:
877         case CONSTRUCTOR:
878         case CONST_DECL:
879           return false;
880
881         default:
882           return false;
883         }
884     }
885
886   if (base == NULL_TREE)
887     return false;
888
889   ref->ref = NULL_TREE;
890   ref->base = base;
891   ref->offset = offset;
892   ref->size = size;
893   ref->max_size = max_size;
894   ref->ref_alias_set = set;
895   if (base_alias_set != -1)
896     ref->base_alias_set = base_alias_set;
897   else
898     ref->base_alias_set = get_alias_set (base);
899
900   return true;
901 }
902
903 /* Copy the operations present in load/store/call REF into RESULT, a vector of
904    vn_reference_op_s's.  */
905
906 void
907 copy_reference_ops_from_call (gimple call,
908                               VEC(vn_reference_op_s, heap) **result)
909 {
910   vn_reference_op_s temp;
911   unsigned i;
912
913   /* Copy the type, opcode, function being called and static chain.  */
914   memset (&temp, 0, sizeof (temp));
915   temp.type = gimple_call_return_type (call);
916   temp.opcode = CALL_EXPR;
917   temp.op0 = gimple_call_fn (call);
918   temp.op1 = gimple_call_chain (call);
919   temp.off = -1;
920   VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
921
922   /* Copy the call arguments.  As they can be references as well,
923      just chain them together.  */
924   for (i = 0; i < gimple_call_num_args (call); ++i)
925     {
926       tree callarg = gimple_call_arg (call, i);
927       copy_reference_ops_from_ref (callarg, result);
928     }
929 }
930
931 /* Create a vector of vn_reference_op_s structures from REF, a
932    REFERENCE_CLASS_P tree.  The vector is not shared. */
933
934 static VEC(vn_reference_op_s, heap) *
935 create_reference_ops_from_ref (tree ref)
936 {
937   VEC (vn_reference_op_s, heap) *result = NULL;
938
939   copy_reference_ops_from_ref (ref, &result);
940   return result;
941 }
942
943 /* Create a vector of vn_reference_op_s structures from CALL, a
944    call statement.  The vector is not shared.  */
945
946 static VEC(vn_reference_op_s, heap) *
947 create_reference_ops_from_call (gimple call)
948 {
949   VEC (vn_reference_op_s, heap) *result = NULL;
950
951   copy_reference_ops_from_call (call, &result);
952   return result;
953 }
954
955 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS.  Updates
956    *I_P to point to the last element of the replacement.  */
957 void
958 vn_reference_fold_indirect (VEC (vn_reference_op_s, heap) **ops,
959                             unsigned int *i_p)
960 {
961   unsigned int i = *i_p;
962   vn_reference_op_t op = VEC_index (vn_reference_op_s, *ops, i);
963   vn_reference_op_t mem_op = VEC_index (vn_reference_op_s, *ops, i - 1);
964   tree addr_base;
965   HOST_WIDE_INT addr_offset;
966
967   /* The only thing we have to do is from &OBJ.foo.bar add the offset
968      from .foo.bar to the preceeding MEM_REF offset and replace the
969      address with &OBJ.  */
970   addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
971                                              &addr_offset);
972   gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
973   if (addr_base != op->op0)
974     {
975       double_int off = tree_to_double_int (mem_op->op0);
976       off = double_int_sext (off, TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
977       off = double_int_add (off, shwi_to_double_int (addr_offset));
978       mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
979       op->op0 = build_fold_addr_expr (addr_base);
980       if (host_integerp (mem_op->op0, 0))
981         mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
982       else
983         mem_op->off = -1;
984     }
985 }
986
987 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS.  Updates
988    *I_P to point to the last element of the replacement.  */
989 static void
990 vn_reference_maybe_forwprop_address (VEC (vn_reference_op_s, heap) **ops,
991                                      unsigned int *i_p)
992 {
993   unsigned int i = *i_p;
994   vn_reference_op_t op = VEC_index (vn_reference_op_s, *ops, i);
995   vn_reference_op_t mem_op = VEC_index (vn_reference_op_s, *ops, i - 1);
996   gimple def_stmt;
997   enum tree_code code;
998   double_int off;
999
1000   def_stmt = SSA_NAME_DEF_STMT (op->op0);
1001   if (!is_gimple_assign (def_stmt))
1002     return;
1003
1004   code = gimple_assign_rhs_code (def_stmt);
1005   if (code != ADDR_EXPR
1006       && code != POINTER_PLUS_EXPR)
1007     return;
1008
1009   off = tree_to_double_int (mem_op->op0);
1010   off = double_int_sext (off, TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1011
1012   /* The only thing we have to do is from &OBJ.foo.bar add the offset
1013      from .foo.bar to the preceeding MEM_REF offset and replace the
1014      address with &OBJ.  */
1015   if (code == ADDR_EXPR)
1016     {
1017       tree addr, addr_base;
1018       HOST_WIDE_INT addr_offset;
1019
1020       addr = gimple_assign_rhs1 (def_stmt);
1021       addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1022                                                  &addr_offset);
1023       if (!addr_base
1024           || TREE_CODE (addr_base) != MEM_REF)
1025         return;
1026
1027       off = double_int_add (off, shwi_to_double_int (addr_offset));
1028       off = double_int_add (off, mem_ref_offset (addr_base));
1029       op->op0 = TREE_OPERAND (addr_base, 0);
1030     }
1031   else
1032     {
1033       tree ptr, ptroff;
1034       ptr = gimple_assign_rhs1 (def_stmt);
1035       ptroff = gimple_assign_rhs2 (def_stmt);
1036       if (TREE_CODE (ptr) != SSA_NAME
1037           || TREE_CODE (ptroff) != INTEGER_CST)
1038         return;
1039
1040       off = double_int_add (off, tree_to_double_int (ptroff));
1041       op->op0 = ptr;
1042     }
1043
1044   mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1045   if (host_integerp (mem_op->op0, 0))
1046     mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1047   else
1048     mem_op->off = -1;
1049   if (TREE_CODE (op->op0) == SSA_NAME)
1050     op->op0 = SSA_VAL (op->op0);
1051   if (TREE_CODE (op->op0) != SSA_NAME)
1052     op->opcode = TREE_CODE (op->op0);
1053
1054   /* And recurse.  */
1055   if (TREE_CODE (op->op0) == SSA_NAME)
1056     vn_reference_maybe_forwprop_address (ops, i_p);
1057   else if (TREE_CODE (op->op0) == ADDR_EXPR)
1058     vn_reference_fold_indirect (ops, i_p);
1059 }
1060
1061 /* Optimize the reference REF to a constant if possible or return
1062    NULL_TREE if not.  */
1063
1064 tree
1065 fully_constant_vn_reference_p (vn_reference_t ref)
1066 {
1067   VEC (vn_reference_op_s, heap) *operands = ref->operands;
1068   vn_reference_op_t op;
1069
1070   /* Try to simplify the translated expression if it is
1071      a call to a builtin function with at most two arguments.  */
1072   op = VEC_index (vn_reference_op_s, operands, 0);
1073   if (op->opcode == CALL_EXPR
1074       && TREE_CODE (op->op0) == ADDR_EXPR
1075       && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1076       && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1077       && VEC_length (vn_reference_op_s, operands) >= 2
1078       && VEC_length (vn_reference_op_s, operands) <= 3)
1079     {
1080       vn_reference_op_t arg0, arg1 = NULL;
1081       bool anyconst = false;
1082       arg0 = VEC_index (vn_reference_op_s, operands, 1);
1083       if (VEC_length (vn_reference_op_s, operands) > 2)
1084         arg1 = VEC_index (vn_reference_op_s, operands, 2);
1085       if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1086           || (arg0->opcode == ADDR_EXPR
1087               && is_gimple_min_invariant (arg0->op0)))
1088         anyconst = true;
1089       if (arg1
1090           && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1091               || (arg1->opcode == ADDR_EXPR
1092                   && is_gimple_min_invariant (arg1->op0))))
1093         anyconst = true;
1094       if (anyconst)
1095         {
1096           tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1097                                          arg1 ? 2 : 1,
1098                                          arg0->op0,
1099                                          arg1 ? arg1->op0 : NULL);
1100           if (folded
1101               && TREE_CODE (folded) == NOP_EXPR)
1102             folded = TREE_OPERAND (folded, 0);
1103           if (folded
1104               && is_gimple_min_invariant (folded))
1105             return folded;
1106         }
1107     }
1108
1109   /* Simplify reads from constant strings.  */
1110   else if (op->opcode == ARRAY_REF
1111            && TREE_CODE (op->op0) == INTEGER_CST
1112            && integer_zerop (op->op1)
1113            && VEC_length (vn_reference_op_s, operands) == 2)
1114     {
1115       vn_reference_op_t arg0;
1116       arg0 = VEC_index (vn_reference_op_s, operands, 1);
1117       if (arg0->opcode == STRING_CST
1118           && (TYPE_MODE (op->type)
1119               == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1120           && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1121           && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1122           && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1123         return build_int_cst_type (op->type,
1124                                    (TREE_STRING_POINTER (arg0->op0)
1125                                     [TREE_INT_CST_LOW (op->op0)]));
1126     }
1127
1128   return NULL_TREE;
1129 }
1130
1131 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1132    structures into their value numbers.  This is done in-place, and
1133    the vector passed in is returned.  */
1134
1135 static VEC (vn_reference_op_s, heap) *
1136 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
1137 {
1138   vn_reference_op_t vro;
1139   unsigned int i;
1140
1141   FOR_EACH_VEC_ELT (vn_reference_op_s, orig, i, vro)
1142     {
1143       if (vro->opcode == SSA_NAME
1144           || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1145         {
1146           vro->op0 = SSA_VAL (vro->op0);
1147           /* If it transforms from an SSA_NAME to a constant, update
1148              the opcode.  */
1149           if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1150             vro->opcode = TREE_CODE (vro->op0);
1151         }
1152       if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1153         vro->op1 = SSA_VAL (vro->op1);
1154       if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1155         vro->op2 = SSA_VAL (vro->op2);
1156       /* If it transforms from an SSA_NAME to an address, fold with
1157          a preceding indirect reference.  */
1158       if (i > 0
1159           && vro->op0
1160           && TREE_CODE (vro->op0) == ADDR_EXPR
1161           && VEC_index (vn_reference_op_s,
1162                         orig, i - 1)->opcode == MEM_REF)
1163         vn_reference_fold_indirect (&orig, &i);
1164       else if (i > 0
1165                && vro->opcode == SSA_NAME
1166                && VEC_index (vn_reference_op_s,
1167                              orig, i - 1)->opcode == MEM_REF)
1168         vn_reference_maybe_forwprop_address (&orig, &i);
1169       /* If it transforms a non-constant ARRAY_REF into a constant
1170          one, adjust the constant offset.  */
1171       else if (vro->opcode == ARRAY_REF
1172                && vro->off == -1
1173                && TREE_CODE (vro->op0) == INTEGER_CST
1174                && TREE_CODE (vro->op1) == INTEGER_CST
1175                && TREE_CODE (vro->op2) == INTEGER_CST)
1176         {
1177           double_int off = tree_to_double_int (vro->op0);
1178           off = double_int_add (off,
1179                                 double_int_neg
1180                                   (tree_to_double_int (vro->op1)));
1181           off = double_int_mul (off, tree_to_double_int (vro->op2));
1182           if (double_int_fits_in_shwi_p (off))
1183             vro->off = off.low;
1184         }
1185     }
1186
1187   return orig;
1188 }
1189
1190 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
1191
1192 /* Create a vector of vn_reference_op_s structures from REF, a
1193    REFERENCE_CLASS_P tree.  The vector is shared among all callers of
1194    this function.  */
1195
1196 static VEC(vn_reference_op_s, heap) *
1197 valueize_shared_reference_ops_from_ref (tree ref)
1198 {
1199   if (!ref)
1200     return NULL;
1201   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1202   copy_reference_ops_from_ref (ref, &shared_lookup_references);
1203   shared_lookup_references = valueize_refs (shared_lookup_references);
1204   return shared_lookup_references;
1205 }
1206
1207 /* Create a vector of vn_reference_op_s structures from CALL, a
1208    call statement.  The vector is shared among all callers of
1209    this function.  */
1210
1211 static VEC(vn_reference_op_s, heap) *
1212 valueize_shared_reference_ops_from_call (gimple call)
1213 {
1214   if (!call)
1215     return NULL;
1216   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1217   copy_reference_ops_from_call (call, &shared_lookup_references);
1218   shared_lookup_references = valueize_refs (shared_lookup_references);
1219   return shared_lookup_references;
1220 }
1221
1222 /* Lookup a SCCVN reference operation VR in the current hash table.
1223    Returns the resulting value number if it exists in the hash table,
1224    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
1225    vn_reference_t stored in the hashtable if something is found.  */
1226
1227 static tree
1228 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1229 {
1230   void **slot;
1231   hashval_t hash;
1232
1233   hash = vr->hashcode;
1234   slot = htab_find_slot_with_hash (current_info->references, vr,
1235                                    hash, NO_INSERT);
1236   if (!slot && current_info == optimistic_info)
1237     slot = htab_find_slot_with_hash (valid_info->references, vr,
1238                                      hash, NO_INSERT);
1239   if (slot)
1240     {
1241       if (vnresult)
1242         *vnresult = (vn_reference_t)*slot;
1243       return ((vn_reference_t)*slot)->result;
1244     }
1245
1246   return NULL_TREE;
1247 }
1248
1249 static tree *last_vuse_ptr;
1250 static vn_lookup_kind vn_walk_kind;
1251 static vn_lookup_kind default_vn_walk_kind;
1252
1253 /* Callback for walk_non_aliased_vuses.  Adjusts the vn_reference_t VR_
1254    with the current VUSE and performs the expression lookup.  */
1255
1256 static void *
1257 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, void *vr_)
1258 {
1259   vn_reference_t vr = (vn_reference_t)vr_;
1260   void **slot;
1261   hashval_t hash;
1262
1263   if (last_vuse_ptr)
1264     *last_vuse_ptr = vuse;
1265
1266   /* Fixup vuse and hash.  */
1267   if (vr->vuse)
1268     vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1269   vr->vuse = SSA_VAL (vuse);
1270   if (vr->vuse)
1271     vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1272
1273   hash = vr->hashcode;
1274   slot = htab_find_slot_with_hash (current_info->references, vr,
1275                                    hash, NO_INSERT);
1276   if (!slot && current_info == optimistic_info)
1277     slot = htab_find_slot_with_hash (valid_info->references, vr,
1278                                      hash, NO_INSERT);
1279   if (slot)
1280     return *slot;
1281
1282   return NULL;
1283 }
1284
1285 /* Callback for walk_non_aliased_vuses.  Tries to perform a lookup
1286    from the statement defining VUSE and if not successful tries to
1287    translate *REFP and VR_ through an aggregate copy at the defintion
1288    of VUSE.  */
1289
1290 static void *
1291 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1292 {
1293   vn_reference_t vr = (vn_reference_t)vr_;
1294   gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1295   tree fndecl;
1296   tree base;
1297   HOST_WIDE_INT offset, maxsize;
1298   static VEC (vn_reference_op_s, heap) *lhs_ops = NULL;
1299   ao_ref lhs_ref;
1300   bool lhs_ref_ok = false;
1301
1302   /* First try to disambiguate after value-replacing in the definitions LHS.  */
1303   if (is_gimple_assign (def_stmt))
1304     {
1305       VEC (vn_reference_op_s, heap) *tem;
1306       tree lhs = gimple_assign_lhs (def_stmt);
1307       /* Avoid re-allocation overhead.  */
1308       VEC_truncate (vn_reference_op_s, lhs_ops, 0);
1309       copy_reference_ops_from_ref (lhs, &lhs_ops);
1310       tem = lhs_ops;
1311       lhs_ops = valueize_refs (lhs_ops);
1312       gcc_assert (lhs_ops == tem);
1313       lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref, get_alias_set (lhs),
1314                                                   TREE_TYPE (lhs), lhs_ops);
1315       if (lhs_ref_ok
1316           && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1317         return NULL;
1318     }
1319
1320   base = ao_ref_base (ref);
1321   offset = ref->offset;
1322   maxsize = ref->max_size;
1323
1324   /* If we cannot constrain the size of the reference we cannot
1325      test if anything kills it.  */
1326   if (maxsize == -1)
1327     return (void *)-1;
1328
1329   /* def_stmt may-defs *ref.  See if we can derive a value for *ref
1330      from that defintion.
1331      1) Memset.  */
1332   if (is_gimple_reg_type (vr->type)
1333       && is_gimple_call (def_stmt)
1334       && (fndecl = gimple_call_fndecl (def_stmt))
1335       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
1336       && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
1337       && integer_zerop (gimple_call_arg (def_stmt, 1))
1338       && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1339       && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1340     {
1341       tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1342       tree base2;
1343       HOST_WIDE_INT offset2, size2, maxsize2;
1344       base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1345       size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1346       if ((unsigned HOST_WIDE_INT)size2 / 8
1347           == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1348           && maxsize2 != -1
1349           && operand_equal_p (base, base2, 0)
1350           && offset2 <= offset
1351           && offset2 + size2 >= offset + maxsize)
1352         {
1353           tree val = build_zero_cst (vr->type);
1354           unsigned int value_id = get_or_alloc_constant_value_id (val);
1355           return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1356                                              VEC_copy (vn_reference_op_s,
1357                                                        heap, vr->operands),
1358                                              val, value_id);
1359         }
1360     }
1361
1362   /* 2) Assignment from an empty CONSTRUCTOR.  */
1363   else if (is_gimple_reg_type (vr->type)
1364            && gimple_assign_single_p (def_stmt)
1365            && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1366            && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1367     {
1368       tree base2;
1369       HOST_WIDE_INT offset2, size2, maxsize2;
1370       base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1371                                        &offset2, &size2, &maxsize2);
1372       if (maxsize2 != -1
1373           && operand_equal_p (base, base2, 0)
1374           && offset2 <= offset
1375           && offset2 + size2 >= offset + maxsize)
1376         {
1377           tree val = build_zero_cst (vr->type);
1378           unsigned int value_id = get_or_alloc_constant_value_id (val);
1379           return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1380                                              VEC_copy (vn_reference_op_s,
1381                                                        heap, vr->operands),
1382                                              val, value_id);
1383         }
1384     }
1385
1386   /* For aggregate copies translate the reference through them if
1387      the copy kills ref.  */
1388   else if (vn_walk_kind == VN_WALKREWRITE
1389            && gimple_assign_single_p (def_stmt)
1390            && (DECL_P (gimple_assign_rhs1 (def_stmt))
1391                || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1392                || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1393     {
1394       tree base2;
1395       HOST_WIDE_INT offset2, size2, maxsize2;
1396       int i, j;
1397       VEC (vn_reference_op_s, heap) *rhs = NULL;
1398       vn_reference_op_t vro;
1399       ao_ref r;
1400
1401       if (!lhs_ref_ok)
1402         return (void *)-1;
1403
1404       /* See if the assignment kills REF.  */
1405       base2 = ao_ref_base (&lhs_ref);
1406       offset2 = lhs_ref.offset;
1407       size2 = lhs_ref.size;
1408       maxsize2 = lhs_ref.max_size;
1409       if (maxsize2 == -1
1410           || (base != base2 && !operand_equal_p (base, base2, 0))
1411           || offset2 > offset
1412           || offset2 + size2 < offset + maxsize)
1413         return (void *)-1;
1414
1415       /* Find the common base of ref and the lhs.  lhs_ops already
1416          contains valueized operands for the lhs.  */
1417       i = VEC_length (vn_reference_op_s, vr->operands) - 1;
1418       j = VEC_length (vn_reference_op_s, lhs_ops) - 1;
1419       while (j >= 0 && i >= 0
1420              && vn_reference_op_eq (VEC_index (vn_reference_op_s,
1421                                                vr->operands, i),
1422                                     VEC_index (vn_reference_op_s, lhs_ops, j)))
1423         {
1424           i--;
1425           j--;
1426         }
1427
1428       /* i now points to the first additional op.
1429          ???  LHS may not be completely contained in VR, one or more
1430          VIEW_CONVERT_EXPRs could be in its way.  We could at least
1431          try handling outermost VIEW_CONVERT_EXPRs.  */
1432       if (j != -1)
1433         return (void *)-1;
1434
1435       /* Now re-write REF to be based on the rhs of the assignment.  */
1436       copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1437       /* We need to pre-pend vr->operands[0..i] to rhs.  */
1438       if (i + 1 + VEC_length (vn_reference_op_s, rhs)
1439           > VEC_length (vn_reference_op_s, vr->operands))
1440         {
1441           VEC (vn_reference_op_s, heap) *old = vr->operands;
1442           VEC_safe_grow (vn_reference_op_s, heap, vr->operands,
1443                          i + 1 + VEC_length (vn_reference_op_s, rhs));
1444           if (old == shared_lookup_references
1445               && vr->operands != old)
1446             shared_lookup_references = NULL;
1447         }
1448       else
1449         VEC_truncate (vn_reference_op_s, vr->operands,
1450                       i + 1 + VEC_length (vn_reference_op_s, rhs));
1451       FOR_EACH_VEC_ELT (vn_reference_op_s, rhs, j, vro)
1452         VEC_replace (vn_reference_op_s, vr->operands, i + 1 + j, vro);
1453       VEC_free (vn_reference_op_s, heap, rhs);
1454       vr->hashcode = vn_reference_compute_hash (vr);
1455
1456       /* Adjust *ref from the new operands.  */
1457       if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1458         return (void *)-1;
1459       /* This can happen with bitfields.  */
1460       if (ref->size != r.size)
1461         return (void *)-1;
1462       *ref = r;
1463
1464       /* Do not update last seen VUSE after translating.  */
1465       last_vuse_ptr = NULL;
1466
1467       /* Keep looking for the adjusted *REF / VR pair.  */
1468       return NULL;
1469     }
1470
1471   /* Bail out and stop walking.  */
1472   return (void *)-1;
1473 }
1474
1475 /* Lookup a reference operation by it's parts, in the current hash table.
1476    Returns the resulting value number if it exists in the hash table,
1477    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
1478    vn_reference_t stored in the hashtable if something is found.  */
1479
1480 tree
1481 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1482                             VEC (vn_reference_op_s, heap) *operands,
1483                             vn_reference_t *vnresult, vn_lookup_kind kind)
1484 {
1485   struct vn_reference_s vr1;
1486   vn_reference_t tmp;
1487   tree cst;
1488
1489   if (!vnresult)
1490     vnresult = &tmp;
1491   *vnresult = NULL;
1492
1493   vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1494   VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1495   VEC_safe_grow (vn_reference_op_s, heap, shared_lookup_references,
1496                  VEC_length (vn_reference_op_s, operands));
1497   memcpy (VEC_address (vn_reference_op_s, shared_lookup_references),
1498           VEC_address (vn_reference_op_s, operands),
1499           sizeof (vn_reference_op_s)
1500           * VEC_length (vn_reference_op_s, operands));
1501   vr1.operands = operands = shared_lookup_references
1502     = valueize_refs (shared_lookup_references);
1503   vr1.type = type;
1504   vr1.set = set;
1505   vr1.hashcode = vn_reference_compute_hash (&vr1);
1506   if ((cst = fully_constant_vn_reference_p (&vr1)))
1507     return cst;
1508
1509   vn_reference_lookup_1 (&vr1, vnresult);
1510   if (!*vnresult
1511       && kind != VN_NOWALK
1512       && vr1.vuse)
1513     {
1514       ao_ref r;
1515       vn_walk_kind = kind;
1516       if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
1517         *vnresult =
1518           (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1519                                                   vn_reference_lookup_2,
1520                                                   vn_reference_lookup_3, &vr1);
1521       if (vr1.operands != operands)
1522         VEC_free (vn_reference_op_s, heap, vr1.operands);
1523     }
1524
1525   if (*vnresult)
1526      return (*vnresult)->result;
1527
1528   return NULL_TREE;
1529 }
1530
1531 /* Lookup OP in the current hash table, and return the resulting value
1532    number if it exists in the hash table.  Return NULL_TREE if it does
1533    not exist in the hash table or if the result field of the structure
1534    was NULL..  VNRESULT will be filled in with the vn_reference_t
1535    stored in the hashtable if one exists.  */
1536
1537 tree
1538 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
1539                      vn_reference_t *vnresult)
1540 {
1541   VEC (vn_reference_op_s, heap) *operands;
1542   struct vn_reference_s vr1;
1543   tree cst;
1544
1545   if (vnresult)
1546     *vnresult = NULL;
1547
1548   vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1549   vr1.operands = operands = valueize_shared_reference_ops_from_ref (op);
1550   vr1.type = TREE_TYPE (op);
1551   vr1.set = get_alias_set (op);
1552   vr1.hashcode = vn_reference_compute_hash (&vr1);
1553   if ((cst = fully_constant_vn_reference_p (&vr1)))
1554     return cst;
1555
1556   if (kind != VN_NOWALK
1557       && vr1.vuse)
1558     {
1559       vn_reference_t wvnresult;
1560       ao_ref r;
1561       ao_ref_init (&r, op);
1562       vn_walk_kind = kind;
1563       wvnresult =
1564         (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1565                                                 vn_reference_lookup_2,
1566                                                 vn_reference_lookup_3, &vr1);
1567       if (vr1.operands != operands)
1568         VEC_free (vn_reference_op_s, heap, vr1.operands);
1569       if (wvnresult)
1570         {
1571           if (vnresult)
1572             *vnresult = wvnresult;
1573           return wvnresult->result;
1574         }
1575
1576       return NULL_TREE;
1577     }
1578
1579   return vn_reference_lookup_1 (&vr1, vnresult);
1580 }
1581
1582
1583 /* Insert OP into the current hash table with a value number of
1584    RESULT, and return the resulting reference structure we created.  */
1585
1586 vn_reference_t
1587 vn_reference_insert (tree op, tree result, tree vuse)
1588 {
1589   void **slot;
1590   vn_reference_t vr1;
1591
1592   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1593   if (TREE_CODE (result) == SSA_NAME)
1594     vr1->value_id = VN_INFO (result)->value_id;
1595   else
1596     vr1->value_id = get_or_alloc_constant_value_id (result);
1597   vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1598   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
1599   vr1->type = TREE_TYPE (op);
1600   vr1->set = get_alias_set (op);
1601   vr1->hashcode = vn_reference_compute_hash (vr1);
1602   vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
1603
1604   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1605                                    INSERT);
1606
1607   /* Because we lookup stores using vuses, and value number failures
1608      using the vdefs (see visit_reference_op_store for how and why),
1609      it's possible that on failure we may try to insert an already
1610      inserted store.  This is not wrong, there is no ssa name for a
1611      store that we could use as a differentiator anyway.  Thus, unlike
1612      the other lookup functions, you cannot gcc_assert (!*slot)
1613      here.  */
1614
1615   /* But free the old slot in case of a collision.  */
1616   if (*slot)
1617     free_reference (*slot);
1618
1619   *slot = vr1;
1620   return vr1;
1621 }
1622
1623 /* Insert a reference by it's pieces into the current hash table with
1624    a value number of RESULT.  Return the resulting reference
1625    structure we created.  */
1626
1627 vn_reference_t
1628 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
1629                             VEC (vn_reference_op_s, heap) *operands,
1630                             tree result, unsigned int value_id)
1631
1632 {
1633   void **slot;
1634   vn_reference_t vr1;
1635
1636   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1637   vr1->value_id = value_id;
1638   vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1639   vr1->operands = valueize_refs (operands);
1640   vr1->type = type;
1641   vr1->set = set;
1642   vr1->hashcode = vn_reference_compute_hash (vr1);
1643   if (result && TREE_CODE (result) == SSA_NAME)
1644     result = SSA_VAL (result);
1645   vr1->result = result;
1646
1647   slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1648                                    INSERT);
1649
1650   /* At this point we should have all the things inserted that we have
1651      seen before, and we should never try inserting something that
1652      already exists.  */
1653   gcc_assert (!*slot);
1654   if (*slot)
1655     free_reference (*slot);
1656
1657   *slot = vr1;
1658   return vr1;
1659 }
1660
1661 /* Compute and return the hash value for nary operation VBO1.  */
1662
1663 hashval_t
1664 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
1665 {
1666   hashval_t hash;
1667   unsigned i;
1668
1669   for (i = 0; i < vno1->length; ++i)
1670     if (TREE_CODE (vno1->op[i]) == SSA_NAME)
1671       vno1->op[i] = SSA_VAL (vno1->op[i]);
1672
1673   if (vno1->length == 2
1674       && commutative_tree_code (vno1->opcode)
1675       && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
1676     {
1677       tree temp = vno1->op[0];
1678       vno1->op[0] = vno1->op[1];
1679       vno1->op[1] = temp;
1680     }
1681
1682   hash = iterative_hash_hashval_t (vno1->opcode, 0);
1683   for (i = 0; i < vno1->length; ++i)
1684     hash = iterative_hash_expr (vno1->op[i], hash);
1685
1686   return hash;
1687 }
1688
1689 /* Return the computed hashcode for nary operation P1.  */
1690
1691 static hashval_t
1692 vn_nary_op_hash (const void *p1)
1693 {
1694   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1695   return vno1->hashcode;
1696 }
1697
1698 /* Compare nary operations P1 and P2 and return true if they are
1699    equivalent.  */
1700
1701 int
1702 vn_nary_op_eq (const void *p1, const void *p2)
1703 {
1704   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1705   const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
1706   unsigned i;
1707
1708   if (vno1->hashcode != vno2->hashcode)
1709     return false;
1710
1711   if (vno1->opcode != vno2->opcode
1712       || !types_compatible_p (vno1->type, vno2->type))
1713     return false;
1714
1715   for (i = 0; i < vno1->length; ++i)
1716     if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
1717       return false;
1718
1719   return true;
1720 }
1721
1722 /* Initialize VNO from the pieces provided.  */
1723
1724 static void
1725 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
1726                              enum tree_code code, tree type, tree op0,
1727                              tree op1, tree op2, tree op3)
1728 {
1729   vno->opcode = code;
1730   vno->length = length;
1731   vno->type = type;
1732   switch (length)
1733     {
1734       /* The fallthrus here are deliberate.  */
1735     case 4: vno->op[3] = op3;
1736     case 3: vno->op[2] = op2;
1737     case 2: vno->op[1] = op1;
1738     case 1: vno->op[0] = op0;
1739     default:
1740       break;
1741     }
1742 }
1743
1744 /* Initialize VNO from OP.  */
1745
1746 static void
1747 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
1748 {
1749   unsigned i;
1750
1751   vno->opcode = TREE_CODE (op);
1752   vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
1753   vno->type = TREE_TYPE (op);
1754   for (i = 0; i < vno->length; ++i)
1755     vno->op[i] = TREE_OPERAND (op, i);
1756 }
1757
1758 /* Initialize VNO from STMT.  */
1759
1760 static void
1761 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
1762 {
1763   unsigned i;
1764
1765   vno->opcode = gimple_assign_rhs_code (stmt);
1766   vno->length = gimple_num_ops (stmt) - 1;
1767   vno->type = gimple_expr_type (stmt);
1768   for (i = 0; i < vno->length; ++i)
1769     vno->op[i] = gimple_op (stmt, i + 1);
1770   if (vno->opcode == REALPART_EXPR
1771       || vno->opcode == IMAGPART_EXPR
1772       || vno->opcode == VIEW_CONVERT_EXPR)
1773     vno->op[0] = TREE_OPERAND (vno->op[0], 0);
1774 }
1775
1776 /* Compute the hashcode for VNO and look for it in the hash table;
1777    return the resulting value number if it exists in the hash table.
1778    Return NULL_TREE if it does not exist in the hash table or if the
1779    result field of the operation is NULL.  VNRESULT will contain the
1780    vn_nary_op_t from the hashtable if it exists.  */
1781
1782 static tree
1783 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
1784 {
1785   void **slot;
1786
1787   if (vnresult)
1788     *vnresult = NULL;
1789
1790   vno->hashcode = vn_nary_op_compute_hash (vno);
1791   slot = htab_find_slot_with_hash (current_info->nary, vno, vno->hashcode,
1792                                    NO_INSERT);
1793   if (!slot && current_info == optimistic_info)
1794     slot = htab_find_slot_with_hash (valid_info->nary, vno, vno->hashcode,
1795                                      NO_INSERT);
1796   if (!slot)
1797     return NULL_TREE;
1798   if (vnresult)
1799     *vnresult = (vn_nary_op_t)*slot;
1800   return ((vn_nary_op_t)*slot)->result;
1801 }
1802
1803 /* Lookup a n-ary operation by its pieces and return the resulting value
1804    number if it exists in the hash table.  Return NULL_TREE if it does
1805    not exist in the hash table or if the result field of the operation
1806    is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1807    if it exists.  */
1808
1809 tree
1810 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
1811                           tree type, tree op0, tree op1, tree op2,
1812                           tree op3, vn_nary_op_t *vnresult)
1813 {
1814   struct vn_nary_op_s vno1;
1815   init_vn_nary_op_from_pieces (&vno1, length, code, type, op0, op1, op2, op3);
1816   return vn_nary_op_lookup_1 (&vno1, vnresult);
1817 }
1818
1819 /* Lookup OP in the current hash table, and return the resulting value
1820    number if it exists in the hash table.  Return NULL_TREE if it does
1821    not exist in the hash table or if the result field of the operation
1822    is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1823    if it exists.  */
1824
1825 tree
1826 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
1827 {
1828   struct vn_nary_op_s vno1;
1829   init_vn_nary_op_from_op (&vno1, op);
1830   return vn_nary_op_lookup_1 (&vno1, vnresult);
1831 }
1832
1833 /* Lookup the rhs of STMT in the current hash table, and return the resulting
1834    value number if it exists in the hash table.  Return NULL_TREE if
1835    it does not exist in the hash table.  VNRESULT will contain the
1836    vn_nary_op_t from the hashtable if it exists.  */
1837
1838 tree
1839 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
1840 {
1841   struct vn_nary_op_s vno1;
1842   init_vn_nary_op_from_stmt (&vno1, stmt);
1843   return vn_nary_op_lookup_1 (&vno1, vnresult);
1844 }
1845
1846 /* Return the size of a vn_nary_op_t with LENGTH operands.  */
1847
1848 static size_t
1849 sizeof_vn_nary_op (unsigned int length)
1850 {
1851   return sizeof (struct vn_nary_op_s) - sizeof (tree) * (4 - length);
1852 }
1853
1854 /* Allocate a vn_nary_op_t with LENGTH operands on STACK.  */
1855
1856 static vn_nary_op_t
1857 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
1858 {
1859   return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
1860 }
1861
1862 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
1863    obstack.  */
1864
1865 static vn_nary_op_t
1866 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
1867 {
1868   vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
1869                                                &current_info->nary_obstack);
1870
1871   vno1->value_id = value_id;
1872   vno1->length = length;
1873   vno1->result = result;
1874
1875   return vno1;
1876 }
1877
1878 /* Insert VNO into TABLE.  If COMPUTE_HASH is true, then compute
1879    VNO->HASHCODE first.  */
1880
1881 static vn_nary_op_t
1882 vn_nary_op_insert_into (vn_nary_op_t vno, htab_t table, bool compute_hash)
1883 {
1884   void **slot;
1885
1886   if (compute_hash)
1887     vno->hashcode = vn_nary_op_compute_hash (vno);
1888
1889   slot = htab_find_slot_with_hash (table, vno, vno->hashcode, INSERT);
1890   gcc_assert (!*slot);
1891
1892   *slot = vno;
1893   return vno;
1894 }
1895
1896 /* Insert a n-ary operation into the current hash table using it's
1897    pieces.  Return the vn_nary_op_t structure we created and put in
1898    the hashtable.  */
1899
1900 vn_nary_op_t
1901 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
1902                           tree type, tree op0,
1903                           tree op1, tree op2, tree op3,
1904                           tree result,
1905                           unsigned int value_id)
1906 {
1907   vn_nary_op_t vno1;
1908
1909   vno1 = alloc_vn_nary_op (length, result, value_id);
1910   init_vn_nary_op_from_pieces (vno1, length, code, type, op0, op1, op2, op3);
1911   return vn_nary_op_insert_into (vno1, current_info->nary, true);
1912 }
1913
1914 /* Insert OP into the current hash table with a value number of
1915    RESULT.  Return the vn_nary_op_t structure we created and put in
1916    the hashtable.  */
1917
1918 vn_nary_op_t
1919 vn_nary_op_insert (tree op, tree result)
1920 {
1921   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
1922   vn_nary_op_t vno1;
1923
1924   vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
1925   init_vn_nary_op_from_op (vno1, op);
1926   return vn_nary_op_insert_into (vno1, current_info->nary, true);
1927 }
1928
1929 /* Insert the rhs of STMT into the current hash table with a value number of
1930    RESULT.  */
1931
1932 vn_nary_op_t
1933 vn_nary_op_insert_stmt (gimple stmt, tree result)
1934 {
1935   unsigned length = gimple_num_ops (stmt) - 1;
1936   vn_nary_op_t vno1;
1937
1938   vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
1939   init_vn_nary_op_from_stmt (vno1, stmt);
1940   return vn_nary_op_insert_into (vno1, current_info->nary, true);
1941 }
1942
1943 /* Compute a hashcode for PHI operation VP1 and return it.  */
1944
1945 static inline hashval_t
1946 vn_phi_compute_hash (vn_phi_t vp1)
1947 {
1948   hashval_t result;
1949   int i;
1950   tree phi1op;
1951   tree type;
1952
1953   result = vp1->block->index;
1954
1955   /* If all PHI arguments are constants we need to distinguish
1956      the PHI node via its type.  */
1957   type = TREE_TYPE (VEC_index (tree, vp1->phiargs, 0));
1958   result += (INTEGRAL_TYPE_P (type)
1959              + (INTEGRAL_TYPE_P (type)
1960                 ? TYPE_PRECISION (type) + TYPE_UNSIGNED (type) : 0));
1961
1962   FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
1963     {
1964       if (phi1op == VN_TOP)
1965         continue;
1966       result = iterative_hash_expr (phi1op, result);
1967     }
1968
1969   return result;
1970 }
1971
1972 /* Return the computed hashcode for phi operation P1.  */
1973
1974 static hashval_t
1975 vn_phi_hash (const void *p1)
1976 {
1977   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1978   return vp1->hashcode;
1979 }
1980
1981 /* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
1982
1983 static int
1984 vn_phi_eq (const void *p1, const void *p2)
1985 {
1986   const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1987   const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
1988
1989   if (vp1->hashcode != vp2->hashcode)
1990     return false;
1991
1992   if (vp1->block == vp2->block)
1993     {
1994       int i;
1995       tree phi1op;
1996
1997       /* If the PHI nodes do not have compatible types
1998          they are not the same.  */
1999       if (!types_compatible_p (TREE_TYPE (VEC_index (tree, vp1->phiargs, 0)),
2000                                TREE_TYPE (VEC_index (tree, vp2->phiargs, 0))))
2001         return false;
2002
2003       /* Any phi in the same block will have it's arguments in the
2004          same edge order, because of how we store phi nodes.  */
2005       FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
2006         {
2007           tree phi2op = VEC_index (tree, vp2->phiargs, i);
2008           if (phi1op == VN_TOP || phi2op == VN_TOP)
2009             continue;
2010           if (!expressions_equal_p (phi1op, phi2op))
2011             return false;
2012         }
2013       return true;
2014     }
2015   return false;
2016 }
2017
2018 static VEC(tree, heap) *shared_lookup_phiargs;
2019
2020 /* Lookup PHI in the current hash table, and return the resulting
2021    value number if it exists in the hash table.  Return NULL_TREE if
2022    it does not exist in the hash table. */
2023
2024 static tree
2025 vn_phi_lookup (gimple phi)
2026 {
2027   void **slot;
2028   struct vn_phi_s vp1;
2029   unsigned i;
2030
2031   VEC_truncate (tree, shared_lookup_phiargs, 0);
2032
2033   /* Canonicalize the SSA_NAME's to their value number.  */
2034   for (i = 0; i < gimple_phi_num_args (phi); i++)
2035     {
2036       tree def = PHI_ARG_DEF (phi, i);
2037       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2038       VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
2039     }
2040   vp1.phiargs = shared_lookup_phiargs;
2041   vp1.block = gimple_bb (phi);
2042   vp1.hashcode = vn_phi_compute_hash (&vp1);
2043   slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
2044                                    NO_INSERT);
2045   if (!slot && current_info == optimistic_info)
2046     slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
2047                                      NO_INSERT);
2048   if (!slot)
2049     return NULL_TREE;
2050   return ((vn_phi_t)*slot)->result;
2051 }
2052
2053 /* Insert PHI into the current hash table with a value number of
2054    RESULT.  */
2055
2056 static vn_phi_t
2057 vn_phi_insert (gimple phi, tree result)
2058 {
2059   void **slot;
2060   vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2061   unsigned i;
2062   VEC (tree, heap) *args = NULL;
2063
2064   /* Canonicalize the SSA_NAME's to their value number.  */
2065   for (i = 0; i < gimple_phi_num_args (phi); i++)
2066     {
2067       tree def = PHI_ARG_DEF (phi, i);
2068       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2069       VEC_safe_push (tree, heap, args, def);
2070     }
2071   vp1->value_id = VN_INFO (result)->value_id;
2072   vp1->phiargs = args;
2073   vp1->block = gimple_bb (phi);
2074   vp1->result = result;
2075   vp1->hashcode = vn_phi_compute_hash (vp1);
2076
2077   slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
2078                                    INSERT);
2079
2080   /* Because we iterate over phi operations more than once, it's
2081      possible the slot might already exist here, hence no assert.*/
2082   *slot = vp1;
2083   return vp1;
2084 }
2085
2086
2087 /* Print set of components in strongly connected component SCC to OUT. */
2088
2089 static void
2090 print_scc (FILE *out, VEC (tree, heap) *scc)
2091 {
2092   tree var;
2093   unsigned int i;
2094
2095   fprintf (out, "SCC consists of: ");
2096   FOR_EACH_VEC_ELT (tree, scc, i, var)
2097     {
2098       print_generic_expr (out, var, 0);
2099       fprintf (out, " ");
2100     }
2101   fprintf (out, "\n");
2102 }
2103
2104 /* Set the value number of FROM to TO, return true if it has changed
2105    as a result.  */
2106
2107 static inline bool
2108 set_ssa_val_to (tree from, tree to)
2109 {
2110   tree currval = SSA_VAL (from);
2111
2112   if (from != to)
2113     {
2114       if (currval == from)
2115         {
2116           if (dump_file && (dump_flags & TDF_DETAILS))
2117             {
2118               fprintf (dump_file, "Not changing value number of ");
2119               print_generic_expr (dump_file, from, 0);
2120               fprintf (dump_file, " from VARYING to ");
2121               print_generic_expr (dump_file, to, 0);
2122               fprintf (dump_file, "\n");
2123             }
2124           return false;
2125         }
2126       else if (TREE_CODE (to) == SSA_NAME
2127                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2128         to = from;
2129     }
2130
2131   /* The only thing we allow as value numbers are VN_TOP, ssa_names
2132      and invariants.  So assert that here.  */
2133   gcc_assert (to != NULL_TREE
2134               && (to == VN_TOP
2135                   || TREE_CODE (to) == SSA_NAME
2136                   || is_gimple_min_invariant (to)));
2137
2138   if (dump_file && (dump_flags & TDF_DETAILS))
2139     {
2140       fprintf (dump_file, "Setting value number of ");
2141       print_generic_expr (dump_file, from, 0);
2142       fprintf (dump_file, " to ");
2143       print_generic_expr (dump_file, to, 0);
2144     }
2145
2146   if (currval != to  && !operand_equal_p (currval, to, OEP_PURE_SAME))
2147     {
2148       VN_INFO (from)->valnum = to;
2149       if (dump_file && (dump_flags & TDF_DETAILS))
2150         fprintf (dump_file, " (changed)\n");
2151       return true;
2152     }
2153   if (dump_file && (dump_flags & TDF_DETAILS))
2154     fprintf (dump_file, "\n");
2155   return false;
2156 }
2157
2158 /* Set all definitions in STMT to value number to themselves.
2159    Return true if a value number changed. */
2160
2161 static bool
2162 defs_to_varying (gimple stmt)
2163 {
2164   bool changed = false;
2165   ssa_op_iter iter;
2166   def_operand_p defp;
2167
2168   FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2169     {
2170       tree def = DEF_FROM_PTR (defp);
2171
2172       VN_INFO (def)->use_processed = true;
2173       changed |= set_ssa_val_to (def, def);
2174     }
2175   return changed;
2176 }
2177
2178 static bool expr_has_constants (tree expr);
2179 static tree valueize_expr (tree expr);
2180
2181 /* Visit a copy between LHS and RHS, return true if the value number
2182    changed.  */
2183
2184 static bool
2185 visit_copy (tree lhs, tree rhs)
2186 {
2187   /* Follow chains of copies to their destination.  */
2188   while (TREE_CODE (rhs) == SSA_NAME
2189          && SSA_VAL (rhs) != rhs)
2190     rhs = SSA_VAL (rhs);
2191
2192   /* The copy may have a more interesting constant filled expression
2193      (we don't, since we know our RHS is just an SSA name).  */
2194   if (TREE_CODE (rhs) == SSA_NAME)
2195     {
2196       VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2197       VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2198     }
2199
2200   return set_ssa_val_to (lhs, rhs);
2201 }
2202
2203 /* Visit a nary operator RHS, value number it, and return true if the
2204    value number of LHS has changed as a result.  */
2205
2206 static bool
2207 visit_nary_op (tree lhs, gimple stmt)
2208 {
2209   bool changed = false;
2210   tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2211
2212   if (result)
2213     changed = set_ssa_val_to (lhs, result);
2214   else
2215     {
2216       changed = set_ssa_val_to (lhs, lhs);
2217       vn_nary_op_insert_stmt (stmt, lhs);
2218     }
2219
2220   return changed;
2221 }
2222
2223 /* Visit a call STMT storing into LHS.  Return true if the value number
2224    of the LHS has changed as a result.  */
2225
2226 static bool
2227 visit_reference_op_call (tree lhs, gimple stmt)
2228 {
2229   bool changed = false;
2230   struct vn_reference_s vr1;
2231   tree result;
2232   tree vuse = gimple_vuse (stmt);
2233
2234   vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2235   vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2236   vr1.type = gimple_expr_type (stmt);
2237   vr1.set = 0;
2238   vr1.hashcode = vn_reference_compute_hash (&vr1);
2239   result = vn_reference_lookup_1 (&vr1, NULL);
2240   if (result)
2241     {
2242       changed = set_ssa_val_to (lhs, result);
2243       if (TREE_CODE (result) == SSA_NAME
2244           && VN_INFO (result)->has_constants)
2245         VN_INFO (lhs)->has_constants = true;
2246     }
2247   else
2248     {
2249       void **slot;
2250       vn_reference_t vr2;
2251       changed = set_ssa_val_to (lhs, lhs);
2252       vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2253       vr2->vuse = vr1.vuse;
2254       vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2255       vr2->type = vr1.type;
2256       vr2->set = vr1.set;
2257       vr2->hashcode = vr1.hashcode;
2258       vr2->result = lhs;
2259       slot = htab_find_slot_with_hash (current_info->references,
2260                                        vr2, vr2->hashcode, INSERT);
2261       if (*slot)
2262         free_reference (*slot);
2263       *slot = vr2;
2264     }
2265
2266   return changed;
2267 }
2268
2269 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2270    and return true if the value number of the LHS has changed as a result.  */
2271
2272 static bool
2273 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2274 {
2275   bool changed = false;
2276   tree last_vuse;
2277   tree result;
2278
2279   last_vuse = gimple_vuse (stmt);
2280   last_vuse_ptr = &last_vuse;
2281   result = vn_reference_lookup (op, gimple_vuse (stmt),
2282                                 default_vn_walk_kind, NULL);
2283   last_vuse_ptr = NULL;
2284
2285   /* If we have a VCE, try looking up its operand as it might be stored in
2286      a different type.  */
2287   if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2288     result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2289                                   default_vn_walk_kind, NULL);
2290
2291   /* We handle type-punning through unions by value-numbering based
2292      on offset and size of the access.  Be prepared to handle a
2293      type-mismatch here via creating a VIEW_CONVERT_EXPR.  */
2294   if (result
2295       && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2296     {
2297       /* We will be setting the value number of lhs to the value number
2298          of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2299          So first simplify and lookup this expression to see if it
2300          is already available.  */
2301       tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2302       if ((CONVERT_EXPR_P (val)
2303            || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2304           && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2305         {
2306           tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2307           if ((CONVERT_EXPR_P (tem)
2308                || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2309               && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2310                                                     TREE_TYPE (val), tem)))
2311             val = tem;
2312         }
2313       result = val;
2314       if (!is_gimple_min_invariant (val)
2315           && TREE_CODE (val) != SSA_NAME)
2316         result = vn_nary_op_lookup (val, NULL);
2317       /* If the expression is not yet available, value-number lhs to
2318          a new SSA_NAME we create.  */
2319       if (!result)
2320         {
2321           result = make_ssa_name (SSA_NAME_VAR (lhs), gimple_build_nop ());
2322           /* Initialize value-number information properly.  */
2323           VN_INFO_GET (result)->valnum = result;
2324           VN_INFO (result)->value_id = get_next_value_id ();
2325           VN_INFO (result)->expr = val;
2326           VN_INFO (result)->has_constants = expr_has_constants (val);
2327           VN_INFO (result)->needs_insertion = true;
2328           /* As all "inserted" statements are singleton SCCs, insert
2329              to the valid table.  This is strictly needed to
2330              avoid re-generating new value SSA_NAMEs for the same
2331              expression during SCC iteration over and over (the
2332              optimistic table gets cleared after each iteration).
2333              We do not need to insert into the optimistic table, as
2334              lookups there will fall back to the valid table.  */
2335           if (current_info == optimistic_info)
2336             {
2337               current_info = valid_info;
2338               vn_nary_op_insert (val, result);
2339               current_info = optimistic_info;
2340             }
2341           else
2342             vn_nary_op_insert (val, result);
2343           if (dump_file && (dump_flags & TDF_DETAILS))
2344             {
2345               fprintf (dump_file, "Inserting name ");
2346               print_generic_expr (dump_file, result, 0);
2347               fprintf (dump_file, " for expression ");
2348               print_generic_expr (dump_file, val, 0);
2349               fprintf (dump_file, "\n");
2350             }
2351         }
2352     }
2353
2354   if (result)
2355     {
2356       changed = set_ssa_val_to (lhs, result);
2357       if (TREE_CODE (result) == SSA_NAME
2358           && VN_INFO (result)->has_constants)
2359         {
2360           VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2361           VN_INFO (lhs)->has_constants = true;
2362         }
2363     }
2364   else
2365     {
2366       changed = set_ssa_val_to (lhs, lhs);
2367       vn_reference_insert (op, lhs, last_vuse);
2368     }
2369
2370   return changed;
2371 }
2372
2373
2374 /* Visit a store to a reference operator LHS, part of STMT, value number it,
2375    and return true if the value number of the LHS has changed as a result.  */
2376
2377 static bool
2378 visit_reference_op_store (tree lhs, tree op, gimple stmt)
2379 {
2380   bool changed = false;
2381   tree result;
2382   bool resultsame = false;
2383
2384   /* First we want to lookup using the *vuses* from the store and see
2385      if there the last store to this location with the same address
2386      had the same value.
2387
2388      The vuses represent the memory state before the store.  If the
2389      memory state, address, and value of the store is the same as the
2390      last store to this location, then this store will produce the
2391      same memory state as that store.
2392
2393      In this case the vdef versions for this store are value numbered to those
2394      vuse versions, since they represent the same memory state after
2395      this store.
2396
2397      Otherwise, the vdefs for the store are used when inserting into
2398      the table, since the store generates a new memory state.  */
2399
2400   result = vn_reference_lookup (lhs, gimple_vuse (stmt), VN_NOWALK, NULL);
2401
2402   if (result)
2403     {
2404       if (TREE_CODE (result) == SSA_NAME)
2405         result = SSA_VAL (result);
2406       if (TREE_CODE (op) == SSA_NAME)
2407         op = SSA_VAL (op);
2408       resultsame = expressions_equal_p (result, op);
2409     }
2410
2411   if (!result || !resultsame)
2412     {
2413       tree vdef;
2414
2415       if (dump_file && (dump_flags & TDF_DETAILS))
2416         {
2417           fprintf (dump_file, "No store match\n");
2418           fprintf (dump_file, "Value numbering store ");
2419           print_generic_expr (dump_file, lhs, 0);
2420           fprintf (dump_file, " to ");
2421           print_generic_expr (dump_file, op, 0);
2422           fprintf (dump_file, "\n");
2423         }
2424       /* Have to set value numbers before insert, since insert is
2425          going to valueize the references in-place.  */
2426       if ((vdef = gimple_vdef (stmt)))
2427         {
2428           VN_INFO (vdef)->use_processed = true;
2429           changed |= set_ssa_val_to (vdef, vdef);
2430         }
2431
2432       /* Do not insert structure copies into the tables.  */
2433       if (is_gimple_min_invariant (op)
2434           || is_gimple_reg (op))
2435         vn_reference_insert (lhs, op, vdef);
2436     }
2437   else
2438     {
2439       /* We had a match, so value number the vdef to have the value
2440          number of the vuse it came from.  */
2441       tree def, use;
2442
2443       if (dump_file && (dump_flags & TDF_DETAILS))
2444         fprintf (dump_file, "Store matched earlier value,"
2445                  "value numbering store vdefs to matching vuses.\n");
2446
2447       def = gimple_vdef (stmt);
2448       use = gimple_vuse (stmt);
2449
2450       VN_INFO (def)->use_processed = true;
2451       changed |= set_ssa_val_to (def, SSA_VAL (use));
2452     }
2453
2454   return changed;
2455 }
2456
2457 /* Visit and value number PHI, return true if the value number
2458    changed.  */
2459
2460 static bool
2461 visit_phi (gimple phi)
2462 {
2463   bool changed = false;
2464   tree result;
2465   tree sameval = VN_TOP;
2466   bool allsame = true;
2467   unsigned i;
2468
2469   /* TODO: We could check for this in init_sccvn, and replace this
2470      with a gcc_assert.  */
2471   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
2472     return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2473
2474   /* See if all non-TOP arguments have the same value.  TOP is
2475      equivalent to everything, so we can ignore it.  */
2476   for (i = 0; i < gimple_phi_num_args (phi); i++)
2477     {
2478       tree def = PHI_ARG_DEF (phi, i);
2479
2480       if (TREE_CODE (def) == SSA_NAME)
2481         def = SSA_VAL (def);
2482       if (def == VN_TOP)
2483         continue;
2484       if (sameval == VN_TOP)
2485         {
2486           sameval = def;
2487         }
2488       else
2489         {
2490           if (!expressions_equal_p (def, sameval))
2491             {
2492               allsame = false;
2493               break;
2494             }
2495         }
2496     }
2497
2498   /* If all value numbered to the same value, the phi node has that
2499      value.  */
2500   if (allsame)
2501     {
2502       if (is_gimple_min_invariant (sameval))
2503         {
2504           VN_INFO (PHI_RESULT (phi))->has_constants = true;
2505           VN_INFO (PHI_RESULT (phi))->expr = sameval;
2506         }
2507       else
2508         {
2509           VN_INFO (PHI_RESULT (phi))->has_constants = false;
2510           VN_INFO (PHI_RESULT (phi))->expr = sameval;
2511         }
2512
2513       if (TREE_CODE (sameval) == SSA_NAME)
2514         return visit_copy (PHI_RESULT (phi), sameval);
2515
2516       return set_ssa_val_to (PHI_RESULT (phi), sameval);
2517     }
2518
2519   /* Otherwise, see if it is equivalent to a phi node in this block.  */
2520   result = vn_phi_lookup (phi);
2521   if (result)
2522     {
2523       if (TREE_CODE (result) == SSA_NAME)
2524         changed = visit_copy (PHI_RESULT (phi), result);
2525       else
2526         changed = set_ssa_val_to (PHI_RESULT (phi), result);
2527     }
2528   else
2529     {
2530       vn_phi_insert (phi, PHI_RESULT (phi));
2531       VN_INFO (PHI_RESULT (phi))->has_constants = false;
2532       VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
2533       changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2534     }
2535
2536   return changed;
2537 }
2538
2539 /* Return true if EXPR contains constants.  */
2540
2541 static bool
2542 expr_has_constants (tree expr)
2543 {
2544   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2545     {
2546     case tcc_unary:
2547       return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
2548
2549     case tcc_binary:
2550       return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
2551         || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
2552       /* Constants inside reference ops are rarely interesting, but
2553          it can take a lot of looking to find them.  */
2554     case tcc_reference:
2555     case tcc_declaration:
2556       return false;
2557     default:
2558       return is_gimple_min_invariant (expr);
2559     }
2560   return false;
2561 }
2562
2563 /* Return true if STMT contains constants.  */
2564
2565 static bool
2566 stmt_has_constants (gimple stmt)
2567 {
2568   if (gimple_code (stmt) != GIMPLE_ASSIGN)
2569     return false;
2570
2571   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2572     {
2573     case GIMPLE_UNARY_RHS:
2574       return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2575
2576     case GIMPLE_BINARY_RHS:
2577       return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
2578               || is_gimple_min_invariant (gimple_assign_rhs2 (stmt)));
2579     case GIMPLE_TERNARY_RHS:
2580       return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
2581               || is_gimple_min_invariant (gimple_assign_rhs2 (stmt))
2582               || is_gimple_min_invariant (gimple_assign_rhs3 (stmt)));
2583     case GIMPLE_SINGLE_RHS:
2584       /* Constants inside reference ops are rarely interesting, but
2585          it can take a lot of looking to find them.  */
2586       return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2587     default:
2588       gcc_unreachable ();
2589     }
2590   return false;
2591 }
2592
2593 /* Replace SSA_NAMES in expr with their value numbers, and return the
2594    result.
2595    This is performed in place. */
2596
2597 static tree
2598 valueize_expr (tree expr)
2599 {
2600   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2601     {
2602     case tcc_unary:
2603       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2604           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2605         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2606       break;
2607     case tcc_binary:
2608       if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2609           && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2610         TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2611       if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
2612           && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
2613         TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
2614       break;
2615     default:
2616       break;
2617     }
2618   return expr;
2619 }
2620
2621 /* Simplify the binary expression RHS, and return the result if
2622    simplified. */
2623
2624 static tree
2625 simplify_binary_expression (gimple stmt)
2626 {
2627   tree result = NULL_TREE;
2628   tree op0 = gimple_assign_rhs1 (stmt);
2629   tree op1 = gimple_assign_rhs2 (stmt);
2630
2631   /* This will not catch every single case we could combine, but will
2632      catch those with constants.  The goal here is to simultaneously
2633      combine constants between expressions, but avoid infinite
2634      expansion of expressions during simplification.  */
2635   if (TREE_CODE (op0) == SSA_NAME)
2636     {
2637       if (VN_INFO (op0)->has_constants
2638           || TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
2639         op0 = valueize_expr (vn_get_expr_for (op0));
2640       else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
2641         op0 = SSA_VAL (op0);
2642     }
2643
2644   if (TREE_CODE (op1) == SSA_NAME)
2645     {
2646       if (VN_INFO (op1)->has_constants)
2647         op1 = valueize_expr (vn_get_expr_for (op1));
2648       else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
2649         op1 = SSA_VAL (op1);
2650     }
2651
2652   /* Avoid folding if nothing changed.  */
2653   if (op0 == gimple_assign_rhs1 (stmt)
2654       && op1 == gimple_assign_rhs2 (stmt))
2655     return NULL_TREE;
2656
2657   fold_defer_overflow_warnings ();
2658
2659   result = fold_binary (gimple_assign_rhs_code (stmt),
2660                         gimple_expr_type (stmt), op0, op1);
2661   if (result)
2662     STRIP_USELESS_TYPE_CONVERSION (result);
2663
2664   fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
2665                                   stmt, 0);
2666
2667   /* Make sure result is not a complex expression consisting
2668      of operators of operators (IE (a + b) + (a + c))
2669      Otherwise, we will end up with unbounded expressions if
2670      fold does anything at all.  */
2671   if (result && valid_gimple_rhs_p (result))
2672     return result;
2673
2674   return NULL_TREE;
2675 }
2676
2677 /* Simplify the unary expression RHS, and return the result if
2678    simplified. */
2679
2680 static tree
2681 simplify_unary_expression (gimple stmt)
2682 {
2683   tree result = NULL_TREE;
2684   tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
2685
2686   /* We handle some tcc_reference codes here that are all
2687      GIMPLE_ASSIGN_SINGLE codes.  */
2688   if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
2689       || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2690       || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2691     op0 = TREE_OPERAND (op0, 0);
2692
2693   if (TREE_CODE (op0) != SSA_NAME)
2694     return NULL_TREE;
2695
2696   orig_op0 = op0;
2697   if (VN_INFO (op0)->has_constants)
2698     op0 = valueize_expr (vn_get_expr_for (op0));
2699   else if (gimple_assign_cast_p (stmt)
2700            || gimple_assign_rhs_code (stmt) == REALPART_EXPR
2701            || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2702            || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2703     {
2704       /* We want to do tree-combining on conversion-like expressions.
2705          Make sure we feed only SSA_NAMEs or constants to fold though.  */
2706       tree tem = valueize_expr (vn_get_expr_for (op0));
2707       if (UNARY_CLASS_P (tem)
2708           || BINARY_CLASS_P (tem)
2709           || TREE_CODE (tem) == VIEW_CONVERT_EXPR
2710           || TREE_CODE (tem) == SSA_NAME
2711           || is_gimple_min_invariant (tem))
2712         op0 = tem;
2713     }
2714
2715   /* Avoid folding if nothing changed, but remember the expression.  */
2716   if (op0 == orig_op0)
2717     return NULL_TREE;
2718
2719   result = fold_unary_ignore_overflow (gimple_assign_rhs_code (stmt),
2720                                        gimple_expr_type (stmt), op0);
2721   if (result)
2722     {
2723       STRIP_USELESS_TYPE_CONVERSION (result);
2724       if (valid_gimple_rhs_p (result))
2725         return result;
2726     }
2727
2728   return NULL_TREE;
2729 }
2730
2731 /* Try to simplify RHS using equivalences and constant folding.  */
2732
2733 static tree
2734 try_to_simplify (gimple stmt)
2735 {
2736   tree tem;
2737
2738   /* For stores we can end up simplifying a SSA_NAME rhs.  Just return
2739      in this case, there is no point in doing extra work.  */
2740   if (gimple_assign_copy_p (stmt)
2741       && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2742     return NULL_TREE;
2743
2744   switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2745     {
2746     case tcc_declaration:
2747       tem = get_symbol_constant_value (gimple_assign_rhs1 (stmt));
2748       if (tem)
2749         return tem;
2750       break;
2751
2752     case tcc_reference:
2753       /* Do not do full-blown reference lookup here, but simplify
2754          reads from constant aggregates.  */
2755       tem = fold_const_aggregate_ref (gimple_assign_rhs1 (stmt));
2756       if (tem)
2757         return tem;
2758
2759       /* Fallthrough for some codes that can operate on registers.  */
2760       if (!(TREE_CODE (gimple_assign_rhs1 (stmt)) == REALPART_EXPR
2761             || TREE_CODE (gimple_assign_rhs1 (stmt)) == IMAGPART_EXPR
2762             || TREE_CODE (gimple_assign_rhs1 (stmt)) == VIEW_CONVERT_EXPR))
2763         break;
2764       /* We could do a little more with unary ops, if they expand
2765          into binary ops, but it's debatable whether it is worth it. */
2766     case tcc_unary:
2767       return simplify_unary_expression (stmt);
2768       break;
2769     case tcc_comparison:
2770     case tcc_binary:
2771       return simplify_binary_expression (stmt);
2772       break;
2773     default:
2774       break;
2775     }
2776
2777   return NULL_TREE;
2778 }
2779
2780 /* Visit and value number USE, return true if the value number
2781    changed. */
2782
2783 static bool
2784 visit_use (tree use)
2785 {
2786   bool changed = false;
2787   gimple stmt = SSA_NAME_DEF_STMT (use);
2788
2789   VN_INFO (use)->use_processed = true;
2790
2791   gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
2792   if (dump_file && (dump_flags & TDF_DETAILS)
2793       && !SSA_NAME_IS_DEFAULT_DEF (use))
2794     {
2795       fprintf (dump_file, "Value numbering ");
2796       print_generic_expr (dump_file, use, 0);
2797       fprintf (dump_file, " stmt = ");
2798       print_gimple_stmt (dump_file, stmt, 0, 0);
2799     }
2800
2801   /* Handle uninitialized uses.  */
2802   if (SSA_NAME_IS_DEFAULT_DEF (use))
2803     changed = set_ssa_val_to (use, use);
2804   else
2805     {
2806       if (gimple_code (stmt) == GIMPLE_PHI)
2807         changed = visit_phi (stmt);
2808       else if (!gimple_has_lhs (stmt)
2809                || gimple_has_volatile_ops (stmt)
2810                || stmt_could_throw_p (stmt))
2811         changed = defs_to_varying (stmt);
2812       else if (is_gimple_assign (stmt))
2813         {
2814           tree lhs = gimple_assign_lhs (stmt);
2815           tree simplified;
2816
2817           /* Shortcut for copies. Simplifying copies is pointless,
2818              since we copy the expression and value they represent.  */
2819           if (gimple_assign_copy_p (stmt)
2820               && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2821               && TREE_CODE (lhs) == SSA_NAME)
2822             {
2823               changed = visit_copy (lhs, gimple_assign_rhs1 (stmt));
2824               goto done;
2825             }
2826           simplified = try_to_simplify (stmt);
2827           if (simplified)
2828             {
2829               if (dump_file && (dump_flags & TDF_DETAILS))
2830                 {
2831                   fprintf (dump_file, "RHS ");
2832                   print_gimple_expr (dump_file, stmt, 0, 0);
2833                   fprintf (dump_file, " simplified to ");
2834                   print_generic_expr (dump_file, simplified, 0);
2835                   if (TREE_CODE (lhs) == SSA_NAME)
2836                     fprintf (dump_file, " has constants %d\n",
2837                              expr_has_constants (simplified));
2838                   else
2839                     fprintf (dump_file, "\n");
2840                 }
2841             }
2842           /* Setting value numbers to constants will occasionally
2843              screw up phi congruence because constants are not
2844              uniquely associated with a single ssa name that can be
2845              looked up.  */
2846           if (simplified
2847               && is_gimple_min_invariant (simplified)
2848               && TREE_CODE (lhs) == SSA_NAME)
2849             {
2850               VN_INFO (lhs)->expr = simplified;
2851               VN_INFO (lhs)->has_constants = true;
2852               changed = set_ssa_val_to (lhs, simplified);
2853               goto done;
2854             }
2855           else if (simplified
2856                    && TREE_CODE (simplified) == SSA_NAME
2857                    && TREE_CODE (lhs) == SSA_NAME)
2858             {
2859               changed = visit_copy (lhs, simplified);
2860               goto done;
2861             }
2862           else if (simplified)
2863             {
2864               if (TREE_CODE (lhs) == SSA_NAME)
2865                 {
2866                   VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
2867                   /* We have to unshare the expression or else
2868                      valuizing may change the IL stream.  */
2869                   VN_INFO (lhs)->expr = unshare_expr (simplified);
2870                 }
2871             }
2872           else if (stmt_has_constants (stmt)
2873                    && TREE_CODE (lhs) == SSA_NAME)
2874             VN_INFO (lhs)->has_constants = true;
2875           else if (TREE_CODE (lhs) == SSA_NAME)
2876             {
2877               /* We reset expr and constantness here because we may
2878                  have been value numbering optimistically, and
2879                  iterating. They may become non-constant in this case,
2880                  even if they were optimistically constant. */
2881
2882               VN_INFO (lhs)->has_constants = false;
2883               VN_INFO (lhs)->expr = NULL_TREE;
2884             }
2885
2886           if ((TREE_CODE (lhs) == SSA_NAME
2887                /* We can substitute SSA_NAMEs that are live over
2888                   abnormal edges with their constant value.  */
2889                && !(gimple_assign_copy_p (stmt)
2890                     && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2891                && !(simplified
2892                     && is_gimple_min_invariant (simplified))
2893                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2894               /* Stores or copies from SSA_NAMEs that are live over
2895                  abnormal edges are a problem.  */
2896               || (gimple_assign_single_p (stmt)
2897                   && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2898                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt))))
2899             changed = defs_to_varying (stmt);
2900           else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
2901             {
2902               changed = visit_reference_op_store (lhs, gimple_assign_rhs1 (stmt), stmt);
2903             }
2904           else if (TREE_CODE (lhs) == SSA_NAME)
2905             {
2906               if ((gimple_assign_copy_p (stmt)
2907                    && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2908                   || (simplified
2909                       && is_gimple_min_invariant (simplified)))
2910                 {
2911                   VN_INFO (lhs)->has_constants = true;
2912                   if (simplified)
2913                     changed = set_ssa_val_to (lhs, simplified);
2914                   else
2915                     changed = set_ssa_val_to (lhs, gimple_assign_rhs1 (stmt));
2916                 }
2917               else
2918                 {
2919                   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2920                     {
2921                     case GIMPLE_UNARY_RHS:
2922                     case GIMPLE_BINARY_RHS:
2923                     case GIMPLE_TERNARY_RHS:
2924                       changed = visit_nary_op (lhs, stmt);
2925                       break;
2926                     case GIMPLE_SINGLE_RHS:
2927                       switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2928                         {
2929                         case tcc_reference:
2930                           /* VOP-less references can go through unary case.  */
2931                           if ((gimple_assign_rhs_code (stmt) == REALPART_EXPR
2932                                || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2933                                || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2934                               && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (stmt), 0)) == SSA_NAME)
2935                             {
2936                               changed = visit_nary_op (lhs, stmt);
2937                               break;
2938                             }
2939                           /* Fallthrough.  */
2940                         case tcc_declaration:
2941                           changed = visit_reference_op_load
2942                               (lhs, gimple_assign_rhs1 (stmt), stmt);
2943                           break;
2944                         case tcc_expression:
2945                           if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
2946                             {
2947                               changed = visit_nary_op (lhs, stmt);
2948                               break;
2949                             }
2950                           /* Fallthrough.  */
2951                         default:
2952                           changed = defs_to_varying (stmt);
2953                         }
2954                       break;
2955                     default:
2956                       changed = defs_to_varying (stmt);
2957                       break;
2958                     }
2959                 }
2960             }
2961           else
2962             changed = defs_to_varying (stmt);
2963         }
2964       else if (is_gimple_call (stmt))
2965         {
2966           tree lhs = gimple_call_lhs (stmt);
2967
2968           /* ???  We could try to simplify calls.  */
2969
2970           if (stmt_has_constants (stmt)
2971               && TREE_CODE (lhs) == SSA_NAME)
2972             VN_INFO (lhs)->has_constants = true;
2973           else if (TREE_CODE (lhs) == SSA_NAME)
2974             {
2975               /* We reset expr and constantness here because we may
2976                  have been value numbering optimistically, and
2977                  iterating. They may become non-constant in this case,
2978                  even if they were optimistically constant. */
2979               VN_INFO (lhs)->has_constants = false;
2980               VN_INFO (lhs)->expr = NULL_TREE;
2981             }
2982
2983           if (TREE_CODE (lhs) == SSA_NAME
2984               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2985             changed = defs_to_varying (stmt);
2986           /* ???  We should handle stores from calls.  */
2987           else if (TREE_CODE (lhs) == SSA_NAME)
2988             {
2989               if (gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST))
2990                 changed = visit_reference_op_call (lhs, stmt);
2991               else
2992                 changed = defs_to_varying (stmt);
2993             }
2994           else
2995             changed = defs_to_varying (stmt);
2996         }
2997     }
2998  done:
2999   return changed;
3000 }
3001
3002 /* Compare two operands by reverse postorder index */
3003
3004 static int
3005 compare_ops (const void *pa, const void *pb)
3006 {
3007   const tree opa = *((const tree *)pa);
3008   const tree opb = *((const tree *)pb);
3009   gimple opstmta = SSA_NAME_DEF_STMT (opa);
3010   gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3011   basic_block bba;
3012   basic_block bbb;
3013
3014   if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3015     return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3016   else if (gimple_nop_p (opstmta))
3017     return -1;
3018   else if (gimple_nop_p (opstmtb))
3019     return 1;
3020
3021   bba = gimple_bb (opstmta);
3022   bbb = gimple_bb (opstmtb);
3023
3024   if (!bba && !bbb)
3025     return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3026   else if (!bba)
3027     return -1;
3028   else if (!bbb)
3029     return 1;
3030
3031   if (bba == bbb)
3032     {
3033       if (gimple_code (opstmta) == GIMPLE_PHI
3034           && gimple_code (opstmtb) == GIMPLE_PHI)
3035         return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3036       else if (gimple_code (opstmta) == GIMPLE_PHI)
3037         return -1;
3038       else if (gimple_code (opstmtb) == GIMPLE_PHI)
3039         return 1;
3040       else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3041         return gimple_uid (opstmta) - gimple_uid (opstmtb);
3042       else
3043         return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3044     }
3045   return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3046 }
3047
3048 /* Sort an array containing members of a strongly connected component
3049    SCC so that the members are ordered by RPO number.
3050    This means that when the sort is complete, iterating through the
3051    array will give you the members in RPO order.  */
3052
3053 static void
3054 sort_scc (VEC (tree, heap) *scc)
3055 {
3056   VEC_qsort (tree, scc, compare_ops);
3057 }
3058
3059 /* Insert the no longer used nary ONARY to the hash INFO.  */
3060
3061 static void
3062 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3063 {
3064   size_t size = sizeof_vn_nary_op (onary->length);
3065   vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3066                                                &info->nary_obstack);
3067   memcpy (nary, onary, size);
3068   vn_nary_op_insert_into (nary, info->nary, false);
3069 }
3070
3071 /* Insert the no longer used phi OPHI to the hash INFO.  */
3072
3073 static void
3074 copy_phi (vn_phi_t ophi, vn_tables_t info)
3075 {
3076   vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3077   void **slot;
3078   memcpy (phi, ophi, sizeof (*phi));
3079   ophi->phiargs = NULL;
3080   slot = htab_find_slot_with_hash (info->phis, phi, phi->hashcode, INSERT);
3081   gcc_assert (!*slot);
3082   *slot = phi;
3083 }
3084
3085 /* Insert the no longer used reference OREF to the hash INFO.  */
3086
3087 static void
3088 copy_reference (vn_reference_t oref, vn_tables_t info)
3089 {
3090   vn_reference_t ref;
3091   void **slot;
3092   ref = (vn_reference_t) pool_alloc (info->references_pool);
3093   memcpy (ref, oref, sizeof (*ref));
3094   oref->operands = NULL;
3095   slot = htab_find_slot_with_hash (info->references, ref, ref->hashcode,
3096                                    INSERT);
3097   if (*slot)
3098     free_reference (*slot);
3099   *slot = ref;
3100 }
3101
3102 /* Process a strongly connected component in the SSA graph.  */
3103
3104 static void
3105 process_scc (VEC (tree, heap) *scc)
3106 {
3107   tree var;
3108   unsigned int i;
3109   unsigned int iterations = 0;
3110   bool changed = true;
3111   htab_iterator hi;
3112   vn_nary_op_t nary;
3113   vn_phi_t phi;
3114   vn_reference_t ref;
3115
3116   /* If the SCC has a single member, just visit it.  */
3117   if (VEC_length (tree, scc) == 1)
3118     {
3119       tree use = VEC_index (tree, scc, 0);
3120       if (VN_INFO (use)->use_processed)
3121         return;
3122       /* We need to make sure it doesn't form a cycle itself, which can
3123          happen for self-referential PHI nodes.  In that case we would
3124          end up inserting an expression with VN_TOP operands into the
3125          valid table which makes us derive bogus equivalences later.
3126          The cheapest way to check this is to assume it for all PHI nodes.  */
3127       if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3128         /* Fallthru to iteration.  */ ;
3129       else
3130         {
3131           visit_use (use);
3132           return;
3133         }
3134     }
3135
3136   /* Iterate over the SCC with the optimistic table until it stops
3137      changing.  */
3138   current_info = optimistic_info;
3139   while (changed)
3140     {
3141       changed = false;
3142       iterations++;
3143       if (dump_file && (dump_flags & TDF_DETAILS))
3144         fprintf (dump_file, "Starting iteration %d\n", iterations);
3145       /* As we are value-numbering optimistically we have to
3146          clear the expression tables and the simplified expressions
3147          in each iteration until we converge.  */
3148       htab_empty (optimistic_info->nary);
3149       htab_empty (optimistic_info->phis);
3150       htab_empty (optimistic_info->references);
3151       obstack_free (&optimistic_info->nary_obstack, NULL);
3152       gcc_obstack_init (&optimistic_info->nary_obstack);
3153       empty_alloc_pool (optimistic_info->phis_pool);
3154       empty_alloc_pool (optimistic_info->references_pool);
3155       FOR_EACH_VEC_ELT (tree, scc, i, var)
3156         VN_INFO (var)->expr = NULL_TREE;
3157       FOR_EACH_VEC_ELT (tree, scc, i, var)
3158         changed |= visit_use (var);
3159     }
3160
3161   statistics_histogram_event (cfun, "SCC iterations", iterations);
3162
3163   /* Finally, copy the contents of the no longer used optimistic
3164      table to the valid table.  */
3165   FOR_EACH_HTAB_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hi)
3166     copy_nary (nary, valid_info);
3167   FOR_EACH_HTAB_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hi)
3168     copy_phi (phi, valid_info);
3169   FOR_EACH_HTAB_ELEMENT (optimistic_info->references, ref, vn_reference_t, hi)
3170     copy_reference (ref, valid_info);
3171
3172   current_info = valid_info;
3173 }
3174
3175 DEF_VEC_O(ssa_op_iter);
3176 DEF_VEC_ALLOC_O(ssa_op_iter,heap);
3177
3178 /* Pop the components of the found SCC for NAME off the SCC stack
3179    and process them.  Returns true if all went well, false if
3180    we run into resource limits.  */
3181
3182 static bool
3183 extract_and_process_scc_for_name (tree name)
3184 {
3185   VEC (tree, heap) *scc = NULL;
3186   tree x;
3187
3188   /* Found an SCC, pop the components off the SCC stack and
3189      process them.  */
3190   do
3191     {
3192       x = VEC_pop (tree, sccstack);
3193
3194       VN_INFO (x)->on_sccstack = false;
3195       VEC_safe_push (tree, heap, scc, x);
3196     } while (x != name);
3197
3198   /* Bail out of SCCVN in case a SCC turns out to be incredibly large.  */
3199   if (VEC_length (tree, scc)
3200       > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3201     {
3202       if (dump_file)
3203         fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3204                  "SCC size %u exceeding %u\n", VEC_length (tree, scc),
3205                  (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3206       return false;
3207     }
3208
3209   if (VEC_length (tree, scc) > 1)
3210     sort_scc (scc);
3211
3212   if (dump_file && (dump_flags & TDF_DETAILS))
3213     print_scc (dump_file, scc);
3214
3215   process_scc (scc);
3216
3217   VEC_free (tree, heap, scc);
3218
3219   return true;
3220 }
3221
3222 /* Depth first search on NAME to discover and process SCC's in the SSA
3223    graph.
3224    Execution of this algorithm relies on the fact that the SCC's are
3225    popped off the stack in topological order.
3226    Returns true if successful, false if we stopped processing SCC's due
3227    to resource constraints.  */
3228
3229 static bool
3230 DFS (tree name)
3231 {
3232   VEC(ssa_op_iter, heap) *itervec = NULL;
3233   VEC(tree, heap) *namevec = NULL;
3234   use_operand_p usep = NULL;
3235   gimple defstmt;
3236   tree use;
3237   ssa_op_iter iter;
3238
3239 start_over:
3240   /* SCC info */
3241   VN_INFO (name)->dfsnum = next_dfs_num++;
3242   VN_INFO (name)->visited = true;
3243   VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3244
3245   VEC_safe_push (tree, heap, sccstack, name);
3246   VN_INFO (name)->on_sccstack = true;
3247   defstmt = SSA_NAME_DEF_STMT (name);
3248
3249   /* Recursively DFS on our operands, looking for SCC's.  */
3250   if (!gimple_nop_p (defstmt))
3251     {
3252       /* Push a new iterator.  */
3253       if (gimple_code (defstmt) == GIMPLE_PHI)
3254         usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3255       else
3256         usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3257     }
3258   else
3259     clear_and_done_ssa_iter (&iter);
3260
3261   while (1)
3262     {
3263       /* If we are done processing uses of a name, go up the stack
3264          of iterators and process SCCs as we found them.  */
3265       if (op_iter_done (&iter))
3266         {
3267           /* See if we found an SCC.  */
3268           if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3269             if (!extract_and_process_scc_for_name (name))
3270               {
3271                 VEC_free (tree, heap, namevec);
3272                 VEC_free (ssa_op_iter, heap, itervec);
3273                 return false;
3274               }
3275
3276           /* Check if we are done.  */
3277           if (VEC_empty (tree, namevec))
3278             {
3279               VEC_free (tree, heap, namevec);
3280               VEC_free (ssa_op_iter, heap, itervec);
3281               return true;
3282             }
3283
3284           /* Restore the last use walker and continue walking there.  */
3285           use = name;
3286           name = VEC_pop (tree, namevec);
3287           memcpy (&iter, VEC_last (ssa_op_iter, itervec),
3288                   sizeof (ssa_op_iter));
3289           VEC_pop (ssa_op_iter, itervec);
3290           goto continue_walking;
3291         }
3292
3293       use = USE_FROM_PTR (usep);
3294
3295       /* Since we handle phi nodes, we will sometimes get
3296          invariants in the use expression.  */
3297       if (TREE_CODE (use) == SSA_NAME)
3298         {
3299           if (! (VN_INFO (use)->visited))
3300             {
3301               /* Recurse by pushing the current use walking state on
3302                  the stack and starting over.  */
3303               VEC_safe_push(ssa_op_iter, heap, itervec, &iter);
3304               VEC_safe_push(tree, heap, namevec, name);
3305               name = use;
3306               goto start_over;
3307
3308 continue_walking:
3309               VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3310                                          VN_INFO (use)->low);
3311             }
3312           if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3313               && VN_INFO (use)->on_sccstack)
3314             {
3315               VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3316                                          VN_INFO (name)->low);
3317             }
3318         }
3319
3320       usep = op_iter_next_use (&iter);
3321     }
3322 }
3323
3324 /* Allocate a value number table.  */
3325
3326 static void
3327 allocate_vn_table (vn_tables_t table)
3328 {
3329   table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
3330   table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
3331   table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
3332                                    free_reference);
3333
3334   gcc_obstack_init (&table->nary_obstack);
3335   table->phis_pool = create_alloc_pool ("VN phis",
3336                                         sizeof (struct vn_phi_s),
3337                                         30);
3338   table->references_pool = create_alloc_pool ("VN references",
3339                                               sizeof (struct vn_reference_s),
3340                                               30);
3341 }
3342
3343 /* Free a value number table.  */
3344
3345 static void
3346 free_vn_table (vn_tables_t table)
3347 {
3348   htab_delete (table->phis);
3349   htab_delete (table->nary);
3350   htab_delete (table->references);
3351   obstack_free (&table->nary_obstack, NULL);
3352   free_alloc_pool (table->phis_pool);
3353   free_alloc_pool (table->references_pool);
3354 }
3355
3356 static void
3357 init_scc_vn (void)
3358 {
3359   size_t i;
3360   int j;
3361   int *rpo_numbers_temp;
3362
3363   calculate_dominance_info (CDI_DOMINATORS);
3364   sccstack = NULL;
3365   constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
3366                                   free);
3367
3368   constant_value_ids = BITMAP_ALLOC (NULL);
3369
3370   next_dfs_num = 1;
3371   next_value_id = 1;
3372
3373   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
3374   /* VEC_alloc doesn't actually grow it to the right size, it just
3375      preallocates the space to do so.  */
3376   VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
3377   gcc_obstack_init (&vn_ssa_aux_obstack);
3378
3379   shared_lookup_phiargs = NULL;
3380   shared_lookup_references = NULL;
3381   rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3382   rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3383   pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3384
3385   /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3386      the i'th block in RPO order is bb.  We want to map bb's to RPO
3387      numbers, so we need to rearrange this array.  */
3388   for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3389     rpo_numbers[rpo_numbers_temp[j]] = j;
3390
3391   XDELETE (rpo_numbers_temp);
3392
3393   VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3394
3395   /* Create the VN_INFO structures, and initialize value numbers to
3396      TOP.  */
3397   for (i = 0; i < num_ssa_names; i++)
3398     {
3399       tree name = ssa_name (i);
3400       if (name)
3401         {
3402           VN_INFO_GET (name)->valnum = VN_TOP;
3403           VN_INFO (name)->expr = NULL_TREE;
3404           VN_INFO (name)->value_id = 0;
3405         }
3406     }
3407
3408   renumber_gimple_stmt_uids ();
3409
3410   /* Create the valid and optimistic value numbering tables.  */
3411   valid_info = XCNEW (struct vn_tables_s);
3412   allocate_vn_table (valid_info);
3413   optimistic_info = XCNEW (struct vn_tables_s);
3414   allocate_vn_table (optimistic_info);
3415 }
3416
3417 void
3418 free_scc_vn (void)
3419 {
3420   size_t i;
3421
3422   htab_delete (constant_to_value_id);
3423   BITMAP_FREE (constant_value_ids);
3424   VEC_free (tree, heap, shared_lookup_phiargs);
3425   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
3426   XDELETEVEC (rpo_numbers);
3427
3428   for (i = 0; i < num_ssa_names; i++)
3429     {
3430       tree name = ssa_name (i);
3431       if (name
3432           && VN_INFO (name)->needs_insertion)
3433         release_ssa_name (name);
3434     }
3435   obstack_free (&vn_ssa_aux_obstack, NULL);
3436   VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
3437
3438   VEC_free (tree, heap, sccstack);
3439   free_vn_table (valid_info);
3440   XDELETE (valid_info);
3441   free_vn_table (optimistic_info);
3442   XDELETE (optimistic_info);
3443 }
3444
3445 /* Set *ID if we computed something useful in RESULT.  */
3446
3447 static void
3448 set_value_id_for_result (tree result, unsigned int *id)
3449 {
3450   if (result)
3451     {
3452       if (TREE_CODE (result) == SSA_NAME)
3453         *id = VN_INFO (result)->value_id;
3454       else if (is_gimple_min_invariant (result))
3455         *id = get_or_alloc_constant_value_id (result);
3456     }
3457 }
3458
3459 /* Set the value ids in the valid hash tables.  */
3460
3461 static void
3462 set_hashtable_value_ids (void)
3463 {
3464   htab_iterator hi;
3465   vn_nary_op_t vno;
3466   vn_reference_t vr;
3467   vn_phi_t vp;
3468
3469   /* Now set the value ids of the things we had put in the hash
3470      table.  */
3471
3472   FOR_EACH_HTAB_ELEMENT (valid_info->nary,
3473                          vno, vn_nary_op_t, hi)
3474     set_value_id_for_result (vno->result, &vno->value_id);
3475
3476   FOR_EACH_HTAB_ELEMENT (valid_info->phis,
3477                          vp, vn_phi_t, hi)
3478     set_value_id_for_result (vp->result, &vp->value_id);
3479
3480   FOR_EACH_HTAB_ELEMENT (valid_info->references,
3481                          vr, vn_reference_t, hi)
3482     set_value_id_for_result (vr->result, &vr->value_id);
3483 }
3484
3485 /* Do SCCVN.  Returns true if it finished, false if we bailed out
3486    due to resource constraints.  DEFAULT_VN_WALK_KIND_ specifies
3487    how we use the alias oracle walking during the VN process.  */
3488
3489 bool
3490 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
3491 {
3492   size_t i;
3493   tree param;
3494   bool changed = true;
3495
3496   default_vn_walk_kind = default_vn_walk_kind_;
3497
3498   init_scc_vn ();
3499   current_info = valid_info;
3500
3501   for (param = DECL_ARGUMENTS (current_function_decl);
3502        param;
3503        param = DECL_CHAIN (param))
3504     {
3505       if (gimple_default_def (cfun, param) != NULL)
3506         {
3507           tree def = gimple_default_def (cfun, param);
3508           VN_INFO (def)->valnum = def;
3509         }
3510     }
3511
3512   for (i = 1; i < num_ssa_names; ++i)
3513     {
3514       tree name = ssa_name (i);
3515       if (name
3516           && VN_INFO (name)->visited == false
3517           && !has_zero_uses (name))
3518         if (!DFS (name))
3519           {
3520             free_scc_vn ();
3521             return false;
3522           }
3523     }
3524
3525   /* Initialize the value ids.  */
3526
3527   for (i = 1; i < num_ssa_names; ++i)
3528     {
3529       tree name = ssa_name (i);
3530       vn_ssa_aux_t info;
3531       if (!name)
3532         continue;
3533       info = VN_INFO (name);
3534       if (info->valnum == name
3535           || info->valnum == VN_TOP)
3536         info->value_id = get_next_value_id ();
3537       else if (is_gimple_min_invariant (info->valnum))
3538         info->value_id = get_or_alloc_constant_value_id (info->valnum);
3539     }
3540
3541   /* Propagate until they stop changing.  */
3542   while (changed)
3543     {
3544       changed = false;
3545       for (i = 1; i < num_ssa_names; ++i)
3546         {
3547           tree name = ssa_name (i);
3548           vn_ssa_aux_t info;
3549           if (!name)
3550             continue;
3551           info = VN_INFO (name);
3552           if (TREE_CODE (info->valnum) == SSA_NAME
3553               && info->valnum != name
3554               && info->value_id != VN_INFO (info->valnum)->value_id)
3555             {
3556               changed = true;
3557               info->value_id = VN_INFO (info->valnum)->value_id;
3558             }
3559         }
3560     }
3561
3562   set_hashtable_value_ids ();
3563
3564   if (dump_file && (dump_flags & TDF_DETAILS))
3565     {
3566       fprintf (dump_file, "Value numbers:\n");
3567       for (i = 0; i < num_ssa_names; i++)
3568         {
3569           tree name = ssa_name (i);
3570           if (name
3571               && VN_INFO (name)->visited
3572               && SSA_VAL (name) != name)
3573             {
3574               print_generic_expr (dump_file, name, 0);
3575               fprintf (dump_file, " = ");
3576               print_generic_expr (dump_file, SSA_VAL (name), 0);
3577               fprintf (dump_file, "\n");
3578             }
3579         }
3580     }
3581
3582   return true;
3583 }
3584
3585 /* Return the maximum value id we have ever seen.  */
3586
3587 unsigned int
3588 get_max_value_id (void)
3589 {
3590   return next_value_id;
3591 }
3592
3593 /* Return the next unique value id.  */
3594
3595 unsigned int
3596 get_next_value_id (void)
3597 {
3598   return next_value_id++;
3599 }
3600
3601
3602 /* Compare two expressions E1 and E2 and return true if they are equal.  */
3603
3604 bool
3605 expressions_equal_p (tree e1, tree e2)
3606 {
3607   /* The obvious case.  */
3608   if (e1 == e2)
3609     return true;
3610
3611   /* If only one of them is null, they cannot be equal.  */
3612   if (!e1 || !e2)
3613     return false;
3614
3615   /* Now perform the actual comparison.  */
3616   if (TREE_CODE (e1) == TREE_CODE (e2)
3617       && operand_equal_p (e1, e2, OEP_PURE_SAME))
3618     return true;
3619
3620   return false;
3621 }
3622
3623
3624 /* Return true if the nary operation NARY may trap.  This is a copy
3625    of stmt_could_throw_1_p adjusted to the SCCVN IL.  */
3626
3627 bool
3628 vn_nary_may_trap (vn_nary_op_t nary)
3629 {
3630   tree type;
3631   tree rhs2 = NULL_TREE;
3632   bool honor_nans = false;
3633   bool honor_snans = false;
3634   bool fp_operation = false;
3635   bool honor_trapv = false;
3636   bool handled, ret;
3637   unsigned i;
3638
3639   if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
3640       || TREE_CODE_CLASS (nary->opcode) == tcc_unary
3641       || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
3642     {
3643       type = nary->type;
3644       fp_operation = FLOAT_TYPE_P (type);
3645       if (fp_operation)
3646         {
3647           honor_nans = flag_trapping_math && !flag_finite_math_only;
3648           honor_snans = flag_signaling_nans != 0;
3649         }
3650       else if (INTEGRAL_TYPE_P (type)
3651                && TYPE_OVERFLOW_TRAPS (type))
3652         honor_trapv = true;
3653     }
3654   if (nary->length >= 2)
3655     rhs2 = nary->op[1];
3656   ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
3657                                        honor_trapv,
3658                                        honor_nans, honor_snans, rhs2,
3659                                        &handled);
3660   if (handled
3661       && ret)
3662     return true;
3663
3664   for (i = 0; i < nary->length; ++i)
3665     if (tree_could_trap_p (nary->op[i]))
3666       return true;
3667
3668   return false;
3669 }