Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blobcc667207ee0e44eb450f3dbe70a2dca13de508b2
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>
6 This file is part of GCC.
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.
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.
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/>. */
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"
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.
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.
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.
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).
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.
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.
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.
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.
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.
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:
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.
105 /* The set of hashtables and alloc_pool's for their items. */
107 typedef struct vn_tables_s
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;
117 static htab_t constant_to_value_id;
118 static bitmap constant_value_ids;
121 /* Valid hashtables storing information we have proven to be
122 correct. */
124 static vn_tables_t valid_info;
126 /* Optimistic hashtables storing information we are making assumptions about
127 during iterations. */
129 static vn_tables_t optimistic_info;
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. */
135 static vn_tables_t current_info;
138 /* Reverse post order index for each basic block. */
140 static int *rpo_numbers;
142 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
144 /* This represents the top of the VN lattice, which is the universal
145 value. */
147 tree VN_TOP;
149 /* Unique counter for our value ids. */
151 static unsigned int next_value_id;
153 /* Next DFS number and the stack for strongly connected component
154 detection. */
156 static unsigned int next_dfs_num;
157 static VEC (tree, heap) *sccstack;
160 DEF_VEC_P(vn_ssa_aux_t);
161 DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
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. */
167 static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
168 static struct obstack vn_ssa_aux_obstack;
170 /* Return the value numbering information for a given SSA name. */
172 vn_ssa_aux_t
173 VN_INFO (tree name)
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;
181 /* Set the value numbering info for a given SSA name to a given
182 value. */
184 static inline void
185 VN_INFO_SET (tree name, vn_ssa_aux_t value)
187 VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
188 SSA_NAME_VERSION (name), value);
191 /* Initialize the value numbering info for a given SSA name.
192 This should be called just once for every SSA name. */
194 vn_ssa_aux_t
195 VN_INFO_GET (tree name)
197 vn_ssa_aux_t newinfo;
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;
210 /* Get the representative expression for the SSA_NAME NAME. Returns
211 the representative SSA_NAME if there is no expression associated with it. */
213 tree
214 vn_get_expr_for (tree name)
216 vn_ssa_aux_t vn = VN_INFO (name);
217 gimple def_stmt;
218 tree expr = NULL_TREE;
220 if (vn->valnum == VN_TOP)
221 return name;
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;
228 /* Get to the information of the value of this SSA_NAME. */
229 vn = VN_INFO (vn->valnum);
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;
236 /* Else if we have an expression, return it. */
237 if (vn->expr != NULL_TREE)
238 return vn->expr;
240 /* Otherwise use the defining statement to build the expression. */
241 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
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;
249 if (!is_gimple_assign (def_stmt))
250 return vn->valnum;
252 /* FIXME tuples. This is incomplete and likely will miss some
253 simplifications. */
254 switch (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)))
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;
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;
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;
279 default:;
281 if (expr == NULL_TREE)
282 return vn->valnum;
284 /* Cache the expression. */
285 vn->expr = expr;
287 return expr;
291 /* Free a phi operation structure VP. */
293 static void
294 free_phi (void *vp)
296 vn_phi_t phi = (vn_phi_t) vp;
297 VEC_free (tree, heap, phi->phiargs);
300 /* Free a reference operation structure VP. */
302 static void
303 free_reference (void *vp)
305 vn_reference_t vr = (vn_reference_t) vp;
306 VEC_free (vn_reference_op_s, heap, vr->operands);
309 /* Hash table equality function for vn_constant_t. */
311 static int
312 vn_constant_eq (const void *p1, const void *p2)
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;
317 if (vc1->hashcode != vc2->hashcode)
318 return false;
320 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
323 /* Hash table hash function for vn_constant_t. */
325 static hashval_t
326 vn_constant_hash (const void *p1)
328 const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
329 return vc1->hashcode;
332 /* Lookup a value id for CONSTANT and return it. If it does not
333 exist returns 0. */
335 unsigned int
336 get_constant_value_id (tree constant)
338 void **slot;
339 struct vn_constant_s vc;
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;
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. */
353 unsigned int
354 get_or_alloc_constant_value_id (tree constant)
356 void **slot;
357 struct vn_constant_s vc;
358 vn_constant_t vcp;
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;
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;
376 /* Return true if V is a value id for a constant. */
378 bool
379 value_id_constant_p (unsigned int v)
381 return bitmap_bit_p (constant_value_ids, v);
384 /* Compare two reference operands P1 and P2 for equality. Return true if
385 they are equal, and false otherwise. */
387 static int
388 vn_reference_op_eq (const void *p1, const void *p2)
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;
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);
400 /* Compute the hash for a reference operand VRO1. */
402 static hashval_t
403 vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
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;
415 /* Return the hashcode for a given reference operation P1. */
417 static hashval_t
418 vn_reference_hash (const void *p1)
420 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
421 return vr1->hashcode;
424 /* Compute a hash for the reference operation VR1 and return it. */
426 hashval_t
427 vn_reference_compute_hash (const vn_reference_t vr1)
429 hashval_t result = 0;
430 int i;
431 vn_reference_op_t vro;
432 HOST_WIDE_INT off = -1;
433 bool deref = false;
435 for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
437 if (vro->opcode == MEM_REF)
438 deref = true;
439 else if (vro->opcode != ADDR_EXPR)
440 deref = false;
441 if (vro->off != -1)
443 if (off == -1)
444 off = 0;
445 off += vro->off;
447 else
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)
456 if (vro->op0)
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);
463 else
464 result = vn_reference_op_compute_hash (vro, result);
467 if (vr1->vuse)
468 result += SSA_NAME_VERSION (vr1->vuse);
470 return result;
473 /* Return true if reference operations P1 and P2 are equivalent. This
474 means they have the same set of operands and vuses. */
477 vn_reference_eq (const void *p1, const void *p2)
479 unsigned i, j;
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;
486 /* Early out if this is not a hash collision. */
487 if (vr1->hashcode != vr2->hashcode)
488 return false;
490 /* The VOP needs to be the same. */
491 if (vr1->vuse != vr2->vuse)
492 return false;
494 /* If the operands are the same we are done. */
495 if (vr1->operands == vr2->operands)
496 return true;
498 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
499 return false;
501 i = 0;
502 j = 0;
505 HOST_WIDE_INT off1 = 0, off2 = 0;
506 vn_reference_op_t vro1, vro2;
507 vn_reference_op_s tem1, tem2;
508 bool deref1 = false, deref2 = false;
509 for (; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro1); i++)
511 if (vro1->opcode == MEM_REF)
512 deref1 = true;
513 if (vro1->off == -1)
514 break;
515 off1 += vro1->off;
517 for (; VEC_iterate (vn_reference_op_s, vr2->operands, j, vro2); j++)
519 if (vro2->opcode == MEM_REF)
520 deref2 = true;
521 if (vro2->off == -1)
522 break;
523 off2 += vro2->off;
525 if (off1 != off2)
526 return false;
527 if (deref1 && vro1->opcode == ADDR_EXPR)
529 memset (&tem1, 0, sizeof (tem1));
530 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
531 tem1.type = TREE_TYPE (tem1.op0);
532 tem1.opcode = TREE_CODE (tem1.op0);
533 vro1 = &tem1;
535 if (deref2 && vro2->opcode == ADDR_EXPR)
537 memset (&tem2, 0, sizeof (tem2));
538 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
539 tem2.type = TREE_TYPE (tem2.op0);
540 tem2.opcode = TREE_CODE (tem2.op0);
541 vro2 = &tem2;
543 if (!vn_reference_op_eq (vro1, vro2))
544 return false;
545 ++j;
546 ++i;
548 while (VEC_length (vn_reference_op_s, vr1->operands) != i
549 || VEC_length (vn_reference_op_s, vr2->operands) != j);
551 return true;
554 /* Copy the operations present in load/store REF into RESULT, a vector of
555 vn_reference_op_s's. */
557 void
558 copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
560 if (TREE_CODE (ref) == TARGET_MEM_REF)
562 vn_reference_op_s temp;
563 tree base;
565 base = TMR_SYMBOL (ref) ? TMR_SYMBOL (ref) : TMR_BASE (ref);
566 if (!base)
567 base = build_int_cst (ptr_type_node, 0);
569 memset (&temp, 0, sizeof (temp));
570 /* We do not care for spurious type qualifications. */
571 temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
572 temp.opcode = TREE_CODE (ref);
573 temp.op0 = TMR_INDEX (ref);
574 temp.op1 = TMR_STEP (ref);
575 temp.op2 = TMR_OFFSET (ref);
576 temp.off = -1;
577 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
579 memset (&temp, 0, sizeof (temp));
580 temp.type = NULL_TREE;
581 temp.opcode = TREE_CODE (base);
582 temp.op0 = base;
583 temp.op1 = TMR_ORIGINAL (ref);
584 temp.off = -1;
585 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
586 return;
589 /* For non-calls, store the information that makes up the address. */
591 while (ref)
593 vn_reference_op_s temp;
595 memset (&temp, 0, sizeof (temp));
596 /* We do not care for spurious type qualifications. */
597 temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
598 temp.opcode = TREE_CODE (ref);
599 temp.off = -1;
601 switch (temp.opcode)
603 case ALIGN_INDIRECT_REF:
604 /* The only operand is the address, which gets its own
605 vn_reference_op_s structure. */
606 break;
607 case MISALIGNED_INDIRECT_REF:
608 temp.op0 = TREE_OPERAND (ref, 1);
609 break;
610 case MEM_REF:
611 /* The base address gets its own vn_reference_op_s structure. */
612 temp.op0 = TREE_OPERAND (ref, 1);
613 if (host_integerp (TREE_OPERAND (ref, 1), 0))
614 temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
615 break;
616 case BIT_FIELD_REF:
617 /* Record bits and position. */
618 temp.op0 = TREE_OPERAND (ref, 1);
619 temp.op1 = TREE_OPERAND (ref, 2);
620 break;
621 case COMPONENT_REF:
622 /* The field decl is enough to unambiguously specify the field,
623 a matching type is not necessary and a mismatching type
624 is always a spurious difference. */
625 temp.type = NULL_TREE;
626 temp.op0 = TREE_OPERAND (ref, 1);
627 temp.op1 = TREE_OPERAND (ref, 2);
629 tree this_offset = component_ref_field_offset (ref);
630 if (this_offset
631 && TREE_CODE (this_offset) == INTEGER_CST)
633 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
634 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
636 double_int off
637 = double_int_add (tree_to_double_int (this_offset),
638 double_int_sdiv
639 (tree_to_double_int (bit_offset),
640 uhwi_to_double_int (BITS_PER_UNIT),
641 TRUNC_DIV_EXPR));
642 if (double_int_fits_in_shwi_p (off))
643 temp.off = off.low;
647 break;
648 case ARRAY_RANGE_REF:
649 case ARRAY_REF:
650 /* Record index as operand. */
651 temp.op0 = TREE_OPERAND (ref, 1);
652 /* Always record lower bounds and element size. */
653 temp.op1 = array_ref_low_bound (ref);
654 temp.op2 = array_ref_element_size (ref);
655 if (TREE_CODE (temp.op0) == INTEGER_CST
656 && TREE_CODE (temp.op1) == INTEGER_CST
657 && TREE_CODE (temp.op2) == INTEGER_CST)
659 double_int off = tree_to_double_int (temp.op0);
660 off = double_int_add (off,
661 double_int_neg
662 (tree_to_double_int (temp.op1)));
663 off = double_int_mul (off, tree_to_double_int (temp.op2));
664 if (double_int_fits_in_shwi_p (off))
665 temp.off = off.low;
667 break;
668 case STRING_CST:
669 case INTEGER_CST:
670 case COMPLEX_CST:
671 case VECTOR_CST:
672 case REAL_CST:
673 case CONSTRUCTOR:
674 case VAR_DECL:
675 case PARM_DECL:
676 case CONST_DECL:
677 case RESULT_DECL:
678 case SSA_NAME:
679 temp.op0 = ref;
680 break;
681 case ADDR_EXPR:
682 if (is_gimple_min_invariant (ref))
684 temp.op0 = ref;
685 break;
687 /* Fallthrough. */
688 /* These are only interesting for their operands, their
689 existence, and their type. They will never be the last
690 ref in the chain of references (IE they require an
691 operand), so we don't have to put anything
692 for op* as it will be handled by the iteration */
693 case REALPART_EXPR:
694 case VIEW_CONVERT_EXPR:
695 temp.off = 0;
696 break;
697 case IMAGPART_EXPR:
698 /* This is only interesting for its constant offset. */
699 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
700 break;
701 default:
702 gcc_unreachable ();
704 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
706 if (REFERENCE_CLASS_P (ref)
707 || (TREE_CODE (ref) == ADDR_EXPR
708 && !is_gimple_min_invariant (ref)))
709 ref = TREE_OPERAND (ref, 0);
710 else
711 ref = NULL_TREE;
715 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
716 operands in *OPS, the reference alias set SET and the reference type TYPE.
717 Return true if something useful was produced. */
719 bool
720 ao_ref_init_from_vn_reference (ao_ref *ref,
721 alias_set_type set, tree type,
722 VEC (vn_reference_op_s, heap) *ops)
724 vn_reference_op_t op;
725 unsigned i;
726 tree base = NULL_TREE;
727 tree *op0_p = &base;
728 HOST_WIDE_INT offset = 0;
729 HOST_WIDE_INT max_size;
730 HOST_WIDE_INT size = -1;
731 tree size_tree = NULL_TREE;
732 alias_set_type base_alias_set = -1;
734 /* First get the final access size from just the outermost expression. */
735 op = VEC_index (vn_reference_op_s, ops, 0);
736 if (op->opcode == COMPONENT_REF)
737 size_tree = DECL_SIZE (op->op0);
738 else if (op->opcode == BIT_FIELD_REF)
739 size_tree = op->op0;
740 else
742 enum machine_mode mode = TYPE_MODE (type);
743 if (mode == BLKmode)
744 size_tree = TYPE_SIZE (type);
745 else
746 size = GET_MODE_BITSIZE (mode);
748 if (size_tree != NULL_TREE)
750 if (!host_integerp (size_tree, 1))
751 size = -1;
752 else
753 size = TREE_INT_CST_LOW (size_tree);
756 /* Initially, maxsize is the same as the accessed element size.
757 In the following it will only grow (or become -1). */
758 max_size = size;
760 /* Compute cumulative bit-offset for nested component-refs and array-refs,
761 and find the ultimate containing object. */
762 for (i = 0; VEC_iterate (vn_reference_op_s, ops, i, op); ++i)
764 switch (op->opcode)
766 /* These may be in the reference ops, but we cannot do anything
767 sensible with them here. */
768 case ADDR_EXPR:
769 /* Apart from ADDR_EXPR arguments to MEM_REF. */
770 if (base != NULL_TREE
771 && TREE_CODE (base) == MEM_REF
772 && op->op0
773 && DECL_P (TREE_OPERAND (op->op0, 0)))
775 vn_reference_op_t pop = VEC_index (vn_reference_op_s, ops, i-1);
776 base = TREE_OPERAND (op->op0, 0);
777 if (pop->off == -1)
779 max_size = -1;
780 offset = 0;
782 else
783 offset += pop->off * BITS_PER_UNIT;
784 op0_p = NULL;
785 break;
787 /* Fallthru. */
788 case CALL_EXPR:
789 return false;
791 /* Record the base objects. */
792 case ALIGN_INDIRECT_REF:
793 *op0_p = build1 (op->opcode, op->type, NULL_TREE);
794 op0_p = &TREE_OPERAND (*op0_p, 0);
795 break;
797 case MISALIGNED_INDIRECT_REF:
798 *op0_p = build2 (MISALIGNED_INDIRECT_REF, op->type,
799 NULL_TREE, op->op0);
800 op0_p = &TREE_OPERAND (*op0_p, 0);
801 break;
803 case MEM_REF:
804 base_alias_set = get_deref_alias_set (op->op0);
805 *op0_p = build2 (MEM_REF, op->type,
806 NULL_TREE, op->op0);
807 op0_p = &TREE_OPERAND (*op0_p, 0);
808 break;
810 case VAR_DECL:
811 case PARM_DECL:
812 case RESULT_DECL:
813 case SSA_NAME:
814 *op0_p = op->op0;
815 op0_p = NULL;
816 break;
818 /* And now the usual component-reference style ops. */
819 case BIT_FIELD_REF:
820 offset += tree_low_cst (op->op1, 0);
821 break;
823 case COMPONENT_REF:
825 tree field = op->op0;
826 /* We do not have a complete COMPONENT_REF tree here so we
827 cannot use component_ref_field_offset. Do the interesting
828 parts manually. */
830 if (op->op1
831 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
832 max_size = -1;
833 else
835 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
836 * BITS_PER_UNIT);
837 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
839 break;
842 case ARRAY_RANGE_REF:
843 case ARRAY_REF:
844 /* We recorded the lower bound and the element size. */
845 if (!host_integerp (op->op0, 0)
846 || !host_integerp (op->op1, 0)
847 || !host_integerp (op->op2, 0))
848 max_size = -1;
849 else
851 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
852 hindex -= TREE_INT_CST_LOW (op->op1);
853 hindex *= TREE_INT_CST_LOW (op->op2);
854 hindex *= BITS_PER_UNIT;
855 offset += hindex;
857 break;
859 case REALPART_EXPR:
860 break;
862 case IMAGPART_EXPR:
863 offset += size;
864 break;
866 case VIEW_CONVERT_EXPR:
867 break;
869 case STRING_CST:
870 case INTEGER_CST:
871 case COMPLEX_CST:
872 case VECTOR_CST:
873 case REAL_CST:
874 case CONSTRUCTOR:
875 case CONST_DECL:
876 return false;
878 default:
879 return false;
883 if (base == NULL_TREE)
884 return false;
886 ref->ref = NULL_TREE;
887 ref->base = base;
888 ref->offset = offset;
889 ref->size = size;
890 ref->max_size = max_size;
891 ref->ref_alias_set = set;
892 if (base_alias_set != -1)
893 ref->base_alias_set = base_alias_set;
894 else
895 ref->base_alias_set = get_alias_set (base);
897 return true;
900 /* Copy the operations present in load/store/call REF into RESULT, a vector of
901 vn_reference_op_s's. */
903 void
904 copy_reference_ops_from_call (gimple call,
905 VEC(vn_reference_op_s, heap) **result)
907 vn_reference_op_s temp;
908 unsigned i;
910 /* Copy the type, opcode, function being called and static chain. */
911 memset (&temp, 0, sizeof (temp));
912 temp.type = gimple_call_return_type (call);
913 temp.opcode = CALL_EXPR;
914 temp.op0 = gimple_call_fn (call);
915 temp.op1 = gimple_call_chain (call);
916 temp.off = -1;
917 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
919 /* Copy the call arguments. As they can be references as well,
920 just chain them together. */
921 for (i = 0; i < gimple_call_num_args (call); ++i)
923 tree callarg = gimple_call_arg (call, i);
924 copy_reference_ops_from_ref (callarg, result);
928 /* Create a vector of vn_reference_op_s structures from REF, a
929 REFERENCE_CLASS_P tree. The vector is not shared. */
931 static VEC(vn_reference_op_s, heap) *
932 create_reference_ops_from_ref (tree ref)
934 VEC (vn_reference_op_s, heap) *result = NULL;
936 copy_reference_ops_from_ref (ref, &result);
937 return result;
940 /* Create a vector of vn_reference_op_s structures from CALL, a
941 call statement. The vector is not shared. */
943 static VEC(vn_reference_op_s, heap) *
944 create_reference_ops_from_call (gimple call)
946 VEC (vn_reference_op_s, heap) *result = NULL;
948 copy_reference_ops_from_call (call, &result);
949 return result;
952 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
953 *I_P to point to the last element of the replacement. */
954 void
955 vn_reference_fold_indirect (VEC (vn_reference_op_s, heap) **ops,
956 unsigned int *i_p)
958 unsigned int i = *i_p;
959 vn_reference_op_t op = VEC_index (vn_reference_op_s, *ops, i);
960 vn_reference_op_t mem_op = VEC_index (vn_reference_op_s, *ops, i - 1);
961 tree addr_base;
962 HOST_WIDE_INT addr_offset;
964 /* The only thing we have to do is from &OBJ.foo.bar add the offset
965 from .foo.bar to the preceeding MEM_REF offset and replace the
966 address with &OBJ. */
967 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
968 &addr_offset);
969 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
970 if (addr_base != op->op0)
972 double_int off = tree_to_double_int (mem_op->op0);
973 off = double_int_sext (off, TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
974 off = double_int_add (off, shwi_to_double_int (addr_offset));
975 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
976 op->op0 = build_fold_addr_expr (addr_base);
977 if (host_integerp (mem_op->op0, 0))
978 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
979 else
980 mem_op->off = -1;
984 /* Optimize the reference REF to a constant if possible or return
985 NULL_TREE if not. */
987 tree
988 fully_constant_vn_reference_p (vn_reference_t ref)
990 VEC (vn_reference_op_s, heap) *operands = ref->operands;
991 vn_reference_op_t op;
993 /* Try to simplify the translated expression if it is
994 a call to a builtin function with at most two arguments. */
995 op = VEC_index (vn_reference_op_s, operands, 0);
996 if (op->opcode == CALL_EXPR
997 && TREE_CODE (op->op0) == ADDR_EXPR
998 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
999 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1000 && VEC_length (vn_reference_op_s, operands) >= 2
1001 && VEC_length (vn_reference_op_s, operands) <= 3)
1003 vn_reference_op_t arg0, arg1 = NULL;
1004 bool anyconst = false;
1005 arg0 = VEC_index (vn_reference_op_s, operands, 1);
1006 if (VEC_length (vn_reference_op_s, operands) > 2)
1007 arg1 = VEC_index (vn_reference_op_s, operands, 2);
1008 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1009 || (arg0->opcode == ADDR_EXPR
1010 && is_gimple_min_invariant (arg0->op0)))
1011 anyconst = true;
1012 if (arg1
1013 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1014 || (arg1->opcode == ADDR_EXPR
1015 && is_gimple_min_invariant (arg1->op0))))
1016 anyconst = true;
1017 if (anyconst)
1019 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1020 arg1 ? 2 : 1,
1021 arg0->op0,
1022 arg1 ? arg1->op0 : NULL);
1023 if (folded
1024 && TREE_CODE (folded) == NOP_EXPR)
1025 folded = TREE_OPERAND (folded, 0);
1026 if (folded
1027 && is_gimple_min_invariant (folded))
1028 return folded;
1032 /* Simplify reads from constant strings. */
1033 else if (op->opcode == ARRAY_REF
1034 && TREE_CODE (op->op0) == INTEGER_CST
1035 && integer_zerop (op->op1)
1036 && VEC_length (vn_reference_op_s, operands) == 2)
1038 vn_reference_op_t arg0;
1039 arg0 = VEC_index (vn_reference_op_s, operands, 1);
1040 if (arg0->opcode == STRING_CST
1041 && (TYPE_MODE (op->type)
1042 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1043 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1044 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1045 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1046 return build_int_cst_type (op->type,
1047 (TREE_STRING_POINTER (arg0->op0)
1048 [TREE_INT_CST_LOW (op->op0)]));
1051 return NULL_TREE;
1054 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1055 structures into their value numbers. This is done in-place, and
1056 the vector passed in is returned. */
1058 static VEC (vn_reference_op_s, heap) *
1059 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
1061 vn_reference_op_t vro;
1062 unsigned int i;
1064 for (i = 0; VEC_iterate (vn_reference_op_s, orig, i, vro); i++)
1066 if (vro->opcode == SSA_NAME
1067 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1069 vro->op0 = SSA_VAL (vro->op0);
1070 /* If it transforms from an SSA_NAME to a constant, update
1071 the opcode. */
1072 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1073 vro->opcode = TREE_CODE (vro->op0);
1075 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1076 vro->op1 = SSA_VAL (vro->op1);
1077 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1078 vro->op2 = SSA_VAL (vro->op2);
1079 /* If it transforms from an SSA_NAME to an address, fold with
1080 a preceding indirect reference. */
1081 if (i > 0
1082 && vro->op0
1083 && TREE_CODE (vro->op0) == ADDR_EXPR
1084 && VEC_index (vn_reference_op_s,
1085 orig, i - 1)->opcode == MEM_REF)
1086 vn_reference_fold_indirect (&orig, &i);
1087 /* If it transforms a non-constant ARRAY_REF into a constant
1088 one, adjust the constant offset. */
1089 else if (vro->opcode == ARRAY_REF
1090 && vro->off == -1
1091 && TREE_CODE (vro->op0) == INTEGER_CST
1092 && TREE_CODE (vro->op1) == INTEGER_CST
1093 && TREE_CODE (vro->op2) == INTEGER_CST)
1095 double_int off = tree_to_double_int (vro->op0);
1096 off = double_int_add (off,
1097 double_int_neg
1098 (tree_to_double_int (vro->op1)));
1099 off = double_int_mul (off, tree_to_double_int (vro->op2));
1100 if (double_int_fits_in_shwi_p (off))
1101 vro->off = off.low;
1105 return orig;
1108 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
1110 /* Create a vector of vn_reference_op_s structures from REF, a
1111 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1112 this function. */
1114 static VEC(vn_reference_op_s, heap) *
1115 valueize_shared_reference_ops_from_ref (tree ref)
1117 if (!ref)
1118 return NULL;
1119 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1120 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1121 shared_lookup_references = valueize_refs (shared_lookup_references);
1122 return shared_lookup_references;
1125 /* Create a vector of vn_reference_op_s structures from CALL, a
1126 call statement. The vector is shared among all callers of
1127 this function. */
1129 static VEC(vn_reference_op_s, heap) *
1130 valueize_shared_reference_ops_from_call (gimple call)
1132 if (!call)
1133 return NULL;
1134 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1135 copy_reference_ops_from_call (call, &shared_lookup_references);
1136 shared_lookup_references = valueize_refs (shared_lookup_references);
1137 return shared_lookup_references;
1140 /* Lookup a SCCVN reference operation VR in the current hash table.
1141 Returns the resulting value number if it exists in the hash table,
1142 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1143 vn_reference_t stored in the hashtable if something is found. */
1145 static tree
1146 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1148 void **slot;
1149 hashval_t hash;
1151 hash = vr->hashcode;
1152 slot = htab_find_slot_with_hash (current_info->references, vr,
1153 hash, NO_INSERT);
1154 if (!slot && current_info == optimistic_info)
1155 slot = htab_find_slot_with_hash (valid_info->references, vr,
1156 hash, NO_INSERT);
1157 if (slot)
1159 if (vnresult)
1160 *vnresult = (vn_reference_t)*slot;
1161 return ((vn_reference_t)*slot)->result;
1164 return NULL_TREE;
1167 static tree *last_vuse_ptr;
1169 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1170 with the current VUSE and performs the expression lookup. */
1172 static void *
1173 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, void *vr_)
1175 vn_reference_t vr = (vn_reference_t)vr_;
1176 void **slot;
1177 hashval_t hash;
1179 if (last_vuse_ptr)
1180 *last_vuse_ptr = vuse;
1182 /* Fixup vuse and hash. */
1183 if (vr->vuse)
1184 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1185 vr->vuse = SSA_VAL (vuse);
1186 if (vr->vuse)
1187 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1189 hash = vr->hashcode;
1190 slot = htab_find_slot_with_hash (current_info->references, vr,
1191 hash, NO_INSERT);
1192 if (!slot && current_info == optimistic_info)
1193 slot = htab_find_slot_with_hash (valid_info->references, vr,
1194 hash, NO_INSERT);
1195 if (slot)
1196 return *slot;
1198 return NULL;
1201 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1202 from the statement defining VUSE and if not successful tries to
1203 translate *REFP and VR_ through an aggregate copy at the defintion
1204 of VUSE. */
1206 static void *
1207 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1209 vn_reference_t vr = (vn_reference_t)vr_;
1210 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1211 tree fndecl;
1212 tree base;
1213 HOST_WIDE_INT offset, maxsize;
1215 base = ao_ref_base (ref);
1216 offset = ref->offset;
1217 maxsize = ref->max_size;
1219 /* If we cannot constrain the size of the reference we cannot
1220 test if anything kills it. */
1221 if (maxsize == -1)
1222 return (void *)-1;
1224 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1225 from that defintion.
1226 1) Memset. */
1227 if (is_gimple_reg_type (vr->type)
1228 && is_gimple_call (def_stmt)
1229 && (fndecl = gimple_call_fndecl (def_stmt))
1230 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
1231 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
1232 && integer_zerop (gimple_call_arg (def_stmt, 1))
1233 && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1234 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1236 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1237 tree base2;
1238 HOST_WIDE_INT offset2, size2, maxsize2;
1239 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1240 size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1241 if ((unsigned HOST_WIDE_INT)size2 / 8
1242 == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1243 && operand_equal_p (base, base2, 0)
1244 && offset2 <= offset
1245 && offset2 + size2 >= offset + maxsize)
1247 tree val = fold_convert (vr->type, integer_zero_node);
1248 unsigned int value_id = get_or_alloc_constant_value_id (val);
1249 return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1250 VEC_copy (vn_reference_op_s,
1251 heap, vr->operands),
1252 val, value_id);
1256 /* 2) Assignment from an empty CONSTRUCTOR. */
1257 else if (is_gimple_reg_type (vr->type)
1258 && gimple_assign_single_p (def_stmt)
1259 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1260 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1262 tree base2;
1263 HOST_WIDE_INT offset2, size2, maxsize2;
1264 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1265 &offset2, &size2, &maxsize2);
1266 if (operand_equal_p (base, base2, 0)
1267 && offset2 <= offset
1268 && offset2 + size2 >= offset + maxsize)
1270 tree val = fold_convert (vr->type, integer_zero_node);
1271 unsigned int value_id = get_or_alloc_constant_value_id (val);
1272 return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1273 VEC_copy (vn_reference_op_s,
1274 heap, vr->operands),
1275 val, value_id);
1279 /* For aggregate copies translate the reference through them if
1280 the copy kills ref. */
1281 else if (gimple_assign_single_p (def_stmt)
1282 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1283 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1284 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1286 tree base2;
1287 HOST_WIDE_INT offset2, size2, maxsize2;
1288 int i, j;
1289 VEC (vn_reference_op_s, heap) *lhs = NULL, *rhs = NULL;
1290 vn_reference_op_t vro;
1291 ao_ref r;
1293 /* See if the assignment kills REF. */
1294 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1295 &offset2, &size2, &maxsize2);
1296 if (!operand_equal_p (base, base2, 0)
1297 || offset2 > offset
1298 || offset2 + size2 < offset + maxsize)
1299 return (void *)-1;
1301 /* Find the common base of ref and the lhs. */
1302 copy_reference_ops_from_ref (gimple_assign_lhs (def_stmt), &lhs);
1303 i = VEC_length (vn_reference_op_s, vr->operands) - 1;
1304 j = VEC_length (vn_reference_op_s, lhs) - 1;
1305 while (j >= 0 && i >= 0
1306 && vn_reference_op_eq (VEC_index (vn_reference_op_s,
1307 vr->operands, i),
1308 VEC_index (vn_reference_op_s, lhs, j)))
1310 i--;
1311 j--;
1314 VEC_free (vn_reference_op_s, heap, lhs);
1315 /* i now points to the first additional op.
1316 ??? LHS may not be completely contained in VR, one or more
1317 VIEW_CONVERT_EXPRs could be in its way. We could at least
1318 try handling outermost VIEW_CONVERT_EXPRs. */
1319 if (j != -1)
1320 return (void *)-1;
1322 /* Now re-write REF to be based on the rhs of the assignment. */
1323 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1324 /* We need to pre-pend vr->operands[0..i] to rhs. */
1325 if (i + 1 + VEC_length (vn_reference_op_s, rhs)
1326 > VEC_length (vn_reference_op_s, vr->operands))
1328 VEC (vn_reference_op_s, heap) *old = vr->operands;
1329 VEC_safe_grow (vn_reference_op_s, heap, vr->operands,
1330 i + 1 + VEC_length (vn_reference_op_s, rhs));
1331 if (old == shared_lookup_references
1332 && vr->operands != old)
1333 shared_lookup_references = NULL;
1335 else
1336 VEC_truncate (vn_reference_op_s, vr->operands,
1337 i + 1 + VEC_length (vn_reference_op_s, rhs));
1338 for (j = 0; VEC_iterate (vn_reference_op_s, rhs, j, vro); ++j)
1339 VEC_replace (vn_reference_op_s, vr->operands, i + 1 + j, vro);
1340 VEC_free (vn_reference_op_s, heap, rhs);
1341 vr->hashcode = vn_reference_compute_hash (vr);
1343 /* Adjust *ref from the new operands. */
1344 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1345 return (void *)-1;
1346 /* This can happen with bitfields. */
1347 if (ref->size != r.size)
1348 return (void *)-1;
1349 *ref = r;
1351 /* Do not update last seen VUSE after translating. */
1352 last_vuse_ptr = NULL;
1354 /* Keep looking for the adjusted *REF / VR pair. */
1355 return NULL;
1358 /* Bail out and stop walking. */
1359 return (void *)-1;
1362 /* Lookup a reference operation by it's parts, in the current hash table.
1363 Returns the resulting value number if it exists in the hash table,
1364 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1365 vn_reference_t stored in the hashtable if something is found. */
1367 tree
1368 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1369 VEC (vn_reference_op_s, heap) *operands,
1370 vn_reference_t *vnresult, bool maywalk)
1372 struct vn_reference_s vr1;
1373 vn_reference_t tmp;
1374 tree cst;
1376 if (!vnresult)
1377 vnresult = &tmp;
1378 *vnresult = NULL;
1380 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1381 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1382 VEC_safe_grow (vn_reference_op_s, heap, shared_lookup_references,
1383 VEC_length (vn_reference_op_s, operands));
1384 memcpy (VEC_address (vn_reference_op_s, shared_lookup_references),
1385 VEC_address (vn_reference_op_s, operands),
1386 sizeof (vn_reference_op_s)
1387 * VEC_length (vn_reference_op_s, operands));
1388 vr1.operands = operands = shared_lookup_references
1389 = valueize_refs (shared_lookup_references);
1390 vr1.type = type;
1391 vr1.set = set;
1392 vr1.hashcode = vn_reference_compute_hash (&vr1);
1393 if ((cst = fully_constant_vn_reference_p (&vr1)))
1394 return cst;
1396 vn_reference_lookup_1 (&vr1, vnresult);
1397 if (!*vnresult
1398 && maywalk
1399 && vr1.vuse)
1401 ao_ref r;
1402 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
1403 *vnresult =
1404 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1405 vn_reference_lookup_2,
1406 vn_reference_lookup_3, &vr1);
1407 if (vr1.operands != operands)
1408 VEC_free (vn_reference_op_s, heap, vr1.operands);
1411 if (*vnresult)
1412 return (*vnresult)->result;
1414 return NULL_TREE;
1417 /* Lookup OP in the current hash table, and return the resulting value
1418 number if it exists in the hash table. Return NULL_TREE if it does
1419 not exist in the hash table or if the result field of the structure
1420 was NULL.. VNRESULT will be filled in with the vn_reference_t
1421 stored in the hashtable if one exists. */
1423 tree
1424 vn_reference_lookup (tree op, tree vuse, bool maywalk,
1425 vn_reference_t *vnresult)
1427 VEC (vn_reference_op_s, heap) *operands;
1428 struct vn_reference_s vr1;
1429 tree cst;
1431 if (vnresult)
1432 *vnresult = NULL;
1434 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1435 vr1.operands = operands = valueize_shared_reference_ops_from_ref (op);
1436 vr1.type = TREE_TYPE (op);
1437 vr1.set = get_alias_set (op);
1438 vr1.hashcode = vn_reference_compute_hash (&vr1);
1439 if ((cst = fully_constant_vn_reference_p (&vr1)))
1440 return cst;
1442 if (maywalk
1443 && vr1.vuse)
1445 vn_reference_t wvnresult;
1446 ao_ref r;
1447 ao_ref_init (&r, op);
1448 wvnresult =
1449 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1450 vn_reference_lookup_2,
1451 vn_reference_lookup_3, &vr1);
1452 if (vr1.operands != operands)
1453 VEC_free (vn_reference_op_s, heap, vr1.operands);
1454 if (wvnresult)
1456 if (vnresult)
1457 *vnresult = wvnresult;
1458 return wvnresult->result;
1461 return NULL_TREE;
1464 return vn_reference_lookup_1 (&vr1, vnresult);
1468 /* Insert OP into the current hash table with a value number of
1469 RESULT, and return the resulting reference structure we created. */
1471 vn_reference_t
1472 vn_reference_insert (tree op, tree result, tree vuse)
1474 void **slot;
1475 vn_reference_t vr1;
1477 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1478 if (TREE_CODE (result) == SSA_NAME)
1479 vr1->value_id = VN_INFO (result)->value_id;
1480 else
1481 vr1->value_id = get_or_alloc_constant_value_id (result);
1482 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1483 vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
1484 vr1->type = TREE_TYPE (op);
1485 vr1->set = get_alias_set (op);
1486 vr1->hashcode = vn_reference_compute_hash (vr1);
1487 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
1489 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1490 INSERT);
1492 /* Because we lookup stores using vuses, and value number failures
1493 using the vdefs (see visit_reference_op_store for how and why),
1494 it's possible that on failure we may try to insert an already
1495 inserted store. This is not wrong, there is no ssa name for a
1496 store that we could use as a differentiator anyway. Thus, unlike
1497 the other lookup functions, you cannot gcc_assert (!*slot)
1498 here. */
1500 /* But free the old slot in case of a collision. */
1501 if (*slot)
1502 free_reference (*slot);
1504 *slot = vr1;
1505 return vr1;
1508 /* Insert a reference by it's pieces into the current hash table with
1509 a value number of RESULT. Return the resulting reference
1510 structure we created. */
1512 vn_reference_t
1513 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
1514 VEC (vn_reference_op_s, heap) *operands,
1515 tree result, unsigned int value_id)
1518 void **slot;
1519 vn_reference_t vr1;
1521 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1522 vr1->value_id = value_id;
1523 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1524 vr1->operands = valueize_refs (operands);
1525 vr1->type = type;
1526 vr1->set = set;
1527 vr1->hashcode = vn_reference_compute_hash (vr1);
1528 if (result && TREE_CODE (result) == SSA_NAME)
1529 result = SSA_VAL (result);
1530 vr1->result = result;
1532 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1533 INSERT);
1535 /* At this point we should have all the things inserted that we have
1536 seen before, and we should never try inserting something that
1537 already exists. */
1538 gcc_assert (!*slot);
1539 if (*slot)
1540 free_reference (*slot);
1542 *slot = vr1;
1543 return vr1;
1546 /* Compute and return the hash value for nary operation VBO1. */
1548 hashval_t
1549 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
1551 hashval_t hash;
1552 unsigned i;
1554 for (i = 0; i < vno1->length; ++i)
1555 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
1556 vno1->op[i] = SSA_VAL (vno1->op[i]);
1558 if (vno1->length == 2
1559 && commutative_tree_code (vno1->opcode)
1560 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
1562 tree temp = vno1->op[0];
1563 vno1->op[0] = vno1->op[1];
1564 vno1->op[1] = temp;
1567 hash = iterative_hash_hashval_t (vno1->opcode, 0);
1568 for (i = 0; i < vno1->length; ++i)
1569 hash = iterative_hash_expr (vno1->op[i], hash);
1571 return hash;
1574 /* Return the computed hashcode for nary operation P1. */
1576 static hashval_t
1577 vn_nary_op_hash (const void *p1)
1579 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1580 return vno1->hashcode;
1583 /* Compare nary operations P1 and P2 and return true if they are
1584 equivalent. */
1587 vn_nary_op_eq (const void *p1, const void *p2)
1589 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1590 const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
1591 unsigned i;
1593 if (vno1->hashcode != vno2->hashcode)
1594 return false;
1596 if (vno1->opcode != vno2->opcode
1597 || !types_compatible_p (vno1->type, vno2->type))
1598 return false;
1600 for (i = 0; i < vno1->length; ++i)
1601 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
1602 return false;
1604 return true;
1607 /* Lookup a n-ary operation by its pieces and return the resulting value
1608 number if it exists in the hash table. Return NULL_TREE if it does
1609 not exist in the hash table or if the result field of the operation
1610 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1611 if it exists. */
1613 tree
1614 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
1615 tree type, tree op0, tree op1, tree op2,
1616 tree op3, vn_nary_op_t *vnresult)
1618 void **slot;
1619 struct vn_nary_op_s vno1;
1620 if (vnresult)
1621 *vnresult = NULL;
1622 vno1.opcode = code;
1623 vno1.length = length;
1624 vno1.type = type;
1625 vno1.op[0] = op0;
1626 vno1.op[1] = op1;
1627 vno1.op[2] = op2;
1628 vno1.op[3] = op3;
1629 vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1630 slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1631 NO_INSERT);
1632 if (!slot && current_info == optimistic_info)
1633 slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1634 NO_INSERT);
1635 if (!slot)
1636 return NULL_TREE;
1637 if (vnresult)
1638 *vnresult = (vn_nary_op_t)*slot;
1639 return ((vn_nary_op_t)*slot)->result;
1642 /* Lookup OP in the current hash table, and return the resulting value
1643 number if it exists in the hash table. Return NULL_TREE if it does
1644 not exist in the hash table or if the result field of the operation
1645 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1646 if it exists. */
1648 tree
1649 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
1651 void **slot;
1652 struct vn_nary_op_s vno1;
1653 unsigned i;
1655 if (vnresult)
1656 *vnresult = NULL;
1657 vno1.opcode = TREE_CODE (op);
1658 vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
1659 vno1.type = TREE_TYPE (op);
1660 for (i = 0; i < vno1.length; ++i)
1661 vno1.op[i] = TREE_OPERAND (op, i);
1662 vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1663 slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1664 NO_INSERT);
1665 if (!slot && current_info == optimistic_info)
1666 slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1667 NO_INSERT);
1668 if (!slot)
1669 return NULL_TREE;
1670 if (vnresult)
1671 *vnresult = (vn_nary_op_t)*slot;
1672 return ((vn_nary_op_t)*slot)->result;
1675 /* Lookup the rhs of STMT in the current hash table, and return the resulting
1676 value number if it exists in the hash table. Return NULL_TREE if
1677 it does not exist in the hash table. VNRESULT will contain the
1678 vn_nary_op_t from the hashtable if it exists. */
1680 tree
1681 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
1683 void **slot;
1684 struct vn_nary_op_s vno1;
1685 unsigned i;
1687 if (vnresult)
1688 *vnresult = NULL;
1689 vno1.opcode = gimple_assign_rhs_code (stmt);
1690 vno1.length = gimple_num_ops (stmt) - 1;
1691 vno1.type = gimple_expr_type (stmt);
1692 for (i = 0; i < vno1.length; ++i)
1693 vno1.op[i] = gimple_op (stmt, i + 1);
1694 if (vno1.opcode == REALPART_EXPR
1695 || vno1.opcode == IMAGPART_EXPR
1696 || vno1.opcode == VIEW_CONVERT_EXPR)
1697 vno1.op[0] = TREE_OPERAND (vno1.op[0], 0);
1698 vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1699 slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1700 NO_INSERT);
1701 if (!slot && current_info == optimistic_info)
1702 slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1703 NO_INSERT);
1704 if (!slot)
1705 return NULL_TREE;
1706 if (vnresult)
1707 *vnresult = (vn_nary_op_t)*slot;
1708 return ((vn_nary_op_t)*slot)->result;
1711 /* Insert a n-ary operation into the current hash table using it's
1712 pieces. Return the vn_nary_op_t structure we created and put in
1713 the hashtable. */
1715 vn_nary_op_t
1716 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
1717 tree type, tree op0,
1718 tree op1, tree op2, tree op3,
1719 tree result,
1720 unsigned int value_id)
1722 void **slot;
1723 vn_nary_op_t vno1;
1725 vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1726 (sizeof (struct vn_nary_op_s)
1727 - sizeof (tree) * (4 - length)));
1728 vno1->value_id = value_id;
1729 vno1->opcode = code;
1730 vno1->length = length;
1731 vno1->type = type;
1732 if (length >= 1)
1733 vno1->op[0] = op0;
1734 if (length >= 2)
1735 vno1->op[1] = op1;
1736 if (length >= 3)
1737 vno1->op[2] = op2;
1738 if (length >= 4)
1739 vno1->op[3] = op3;
1740 vno1->result = result;
1741 vno1->hashcode = vn_nary_op_compute_hash (vno1);
1742 slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1743 INSERT);
1744 gcc_assert (!*slot);
1746 *slot = vno1;
1747 return vno1;
1751 /* Insert OP into the current hash table with a value number of
1752 RESULT. Return the vn_nary_op_t structure we created and put in
1753 the hashtable. */
1755 vn_nary_op_t
1756 vn_nary_op_insert (tree op, tree result)
1758 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
1759 void **slot;
1760 vn_nary_op_t vno1;
1761 unsigned i;
1763 vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1764 (sizeof (struct vn_nary_op_s)
1765 - sizeof (tree) * (4 - length)));
1766 vno1->value_id = VN_INFO (result)->value_id;
1767 vno1->opcode = TREE_CODE (op);
1768 vno1->length = length;
1769 vno1->type = TREE_TYPE (op);
1770 for (i = 0; i < vno1->length; ++i)
1771 vno1->op[i] = TREE_OPERAND (op, i);
1772 vno1->result = result;
1773 vno1->hashcode = vn_nary_op_compute_hash (vno1);
1774 slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1775 INSERT);
1776 gcc_assert (!*slot);
1778 *slot = vno1;
1779 return vno1;
1782 /* Insert the rhs of STMT into the current hash table with a value number of
1783 RESULT. */
1785 vn_nary_op_t
1786 vn_nary_op_insert_stmt (gimple stmt, tree result)
1788 unsigned length = gimple_num_ops (stmt) - 1;
1789 void **slot;
1790 vn_nary_op_t vno1;
1791 unsigned i;
1793 vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1794 (sizeof (struct vn_nary_op_s)
1795 - sizeof (tree) * (4 - length)));
1796 vno1->value_id = VN_INFO (result)->value_id;
1797 vno1->opcode = gimple_assign_rhs_code (stmt);
1798 vno1->length = length;
1799 vno1->type = gimple_expr_type (stmt);
1800 for (i = 0; i < vno1->length; ++i)
1801 vno1->op[i] = gimple_op (stmt, i + 1);
1802 if (vno1->opcode == REALPART_EXPR
1803 || vno1->opcode == IMAGPART_EXPR
1804 || vno1->opcode == VIEW_CONVERT_EXPR)
1805 vno1->op[0] = TREE_OPERAND (vno1->op[0], 0);
1806 vno1->result = result;
1807 vno1->hashcode = vn_nary_op_compute_hash (vno1);
1808 slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1809 INSERT);
1810 gcc_assert (!*slot);
1812 *slot = vno1;
1813 return vno1;
1816 /* Compute a hashcode for PHI operation VP1 and return it. */
1818 static inline hashval_t
1819 vn_phi_compute_hash (vn_phi_t vp1)
1821 hashval_t result;
1822 int i;
1823 tree phi1op;
1824 tree type;
1826 result = vp1->block->index;
1828 /* If all PHI arguments are constants we need to distinguish
1829 the PHI node via its type. */
1830 type = TREE_TYPE (VEC_index (tree, vp1->phiargs, 0));
1831 result += (INTEGRAL_TYPE_P (type)
1832 + (INTEGRAL_TYPE_P (type)
1833 ? TYPE_PRECISION (type) + TYPE_UNSIGNED (type) : 0));
1835 for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
1837 if (phi1op == VN_TOP)
1838 continue;
1839 result = iterative_hash_expr (phi1op, result);
1842 return result;
1845 /* Return the computed hashcode for phi operation P1. */
1847 static hashval_t
1848 vn_phi_hash (const void *p1)
1850 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1851 return vp1->hashcode;
1854 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
1856 static int
1857 vn_phi_eq (const void *p1, const void *p2)
1859 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1860 const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
1862 if (vp1->hashcode != vp2->hashcode)
1863 return false;
1865 if (vp1->block == vp2->block)
1867 int i;
1868 tree phi1op;
1870 /* If the PHI nodes do not have compatible types
1871 they are not the same. */
1872 if (!types_compatible_p (TREE_TYPE (VEC_index (tree, vp1->phiargs, 0)),
1873 TREE_TYPE (VEC_index (tree, vp2->phiargs, 0))))
1874 return false;
1876 /* Any phi in the same block will have it's arguments in the
1877 same edge order, because of how we store phi nodes. */
1878 for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
1880 tree phi2op = VEC_index (tree, vp2->phiargs, i);
1881 if (phi1op == VN_TOP || phi2op == VN_TOP)
1882 continue;
1883 if (!expressions_equal_p (phi1op, phi2op))
1884 return false;
1886 return true;
1888 return false;
1891 static VEC(tree, heap) *shared_lookup_phiargs;
1893 /* Lookup PHI in the current hash table, and return the resulting
1894 value number if it exists in the hash table. Return NULL_TREE if
1895 it does not exist in the hash table. */
1897 static tree
1898 vn_phi_lookup (gimple phi)
1900 void **slot;
1901 struct vn_phi_s vp1;
1902 unsigned i;
1904 VEC_truncate (tree, shared_lookup_phiargs, 0);
1906 /* Canonicalize the SSA_NAME's to their value number. */
1907 for (i = 0; i < gimple_phi_num_args (phi); i++)
1909 tree def = PHI_ARG_DEF (phi, i);
1910 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1911 VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
1913 vp1.phiargs = shared_lookup_phiargs;
1914 vp1.block = gimple_bb (phi);
1915 vp1.hashcode = vn_phi_compute_hash (&vp1);
1916 slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
1917 NO_INSERT);
1918 if (!slot && current_info == optimistic_info)
1919 slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
1920 NO_INSERT);
1921 if (!slot)
1922 return NULL_TREE;
1923 return ((vn_phi_t)*slot)->result;
1926 /* Insert PHI into the current hash table with a value number of
1927 RESULT. */
1929 static vn_phi_t
1930 vn_phi_insert (gimple phi, tree result)
1932 void **slot;
1933 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
1934 unsigned i;
1935 VEC (tree, heap) *args = NULL;
1937 /* Canonicalize the SSA_NAME's to their value number. */
1938 for (i = 0; i < gimple_phi_num_args (phi); i++)
1940 tree def = PHI_ARG_DEF (phi, i);
1941 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1942 VEC_safe_push (tree, heap, args, def);
1944 vp1->value_id = VN_INFO (result)->value_id;
1945 vp1->phiargs = args;
1946 vp1->block = gimple_bb (phi);
1947 vp1->result = result;
1948 vp1->hashcode = vn_phi_compute_hash (vp1);
1950 slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
1951 INSERT);
1953 /* Because we iterate over phi operations more than once, it's
1954 possible the slot might already exist here, hence no assert.*/
1955 *slot = vp1;
1956 return vp1;
1960 /* Print set of components in strongly connected component SCC to OUT. */
1962 static void
1963 print_scc (FILE *out, VEC (tree, heap) *scc)
1965 tree var;
1966 unsigned int i;
1968 fprintf (out, "SCC consists of: ");
1969 for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1971 print_generic_expr (out, var, 0);
1972 fprintf (out, " ");
1974 fprintf (out, "\n");
1977 /* Set the value number of FROM to TO, return true if it has changed
1978 as a result. */
1980 static inline bool
1981 set_ssa_val_to (tree from, tree to)
1983 tree currval;
1985 if (from != to
1986 && TREE_CODE (to) == SSA_NAME
1987 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
1988 to = from;
1990 /* The only thing we allow as value numbers are VN_TOP, ssa_names
1991 and invariants. So assert that here. */
1992 gcc_assert (to != NULL_TREE
1993 && (to == VN_TOP
1994 || TREE_CODE (to) == SSA_NAME
1995 || is_gimple_min_invariant (to)));
1997 if (dump_file && (dump_flags & TDF_DETAILS))
1999 fprintf (dump_file, "Setting value number of ");
2000 print_generic_expr (dump_file, from, 0);
2001 fprintf (dump_file, " to ");
2002 print_generic_expr (dump_file, to, 0);
2005 currval = SSA_VAL (from);
2007 if (currval != to && !operand_equal_p (currval, to, OEP_PURE_SAME))
2009 VN_INFO (from)->valnum = to;
2010 if (dump_file && (dump_flags & TDF_DETAILS))
2011 fprintf (dump_file, " (changed)\n");
2012 return true;
2014 if (dump_file && (dump_flags & TDF_DETAILS))
2015 fprintf (dump_file, "\n");
2016 return false;
2019 /* Set all definitions in STMT to value number to themselves.
2020 Return true if a value number changed. */
2022 static bool
2023 defs_to_varying (gimple stmt)
2025 bool changed = false;
2026 ssa_op_iter iter;
2027 def_operand_p defp;
2029 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2031 tree def = DEF_FROM_PTR (defp);
2033 VN_INFO (def)->use_processed = true;
2034 changed |= set_ssa_val_to (def, def);
2036 return changed;
2039 static bool expr_has_constants (tree expr);
2040 static tree valueize_expr (tree expr);
2042 /* Visit a copy between LHS and RHS, return true if the value number
2043 changed. */
2045 static bool
2046 visit_copy (tree lhs, tree rhs)
2048 /* Follow chains of copies to their destination. */
2049 while (TREE_CODE (rhs) == SSA_NAME
2050 && SSA_VAL (rhs) != rhs)
2051 rhs = SSA_VAL (rhs);
2053 /* The copy may have a more interesting constant filled expression
2054 (we don't, since we know our RHS is just an SSA name). */
2055 if (TREE_CODE (rhs) == SSA_NAME)
2057 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2058 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2061 return set_ssa_val_to (lhs, rhs);
2064 /* Visit a unary operator RHS, value number it, and return true if the
2065 value number of LHS has changed as a result. */
2067 static bool
2068 visit_unary_op (tree lhs, gimple stmt)
2070 bool changed = false;
2071 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2073 if (result)
2075 changed = set_ssa_val_to (lhs, result);
2077 else
2079 changed = set_ssa_val_to (lhs, lhs);
2080 vn_nary_op_insert_stmt (stmt, lhs);
2083 return changed;
2086 /* Visit a binary operator RHS, value number it, and return true if the
2087 value number of LHS has changed as a result. */
2089 static bool
2090 visit_binary_op (tree lhs, gimple stmt)
2092 bool changed = false;
2093 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2095 if (result)
2097 changed = set_ssa_val_to (lhs, result);
2099 else
2101 changed = set_ssa_val_to (lhs, lhs);
2102 vn_nary_op_insert_stmt (stmt, lhs);
2105 return changed;
2108 /* Visit a call STMT storing into LHS. Return true if the value number
2109 of the LHS has changed as a result. */
2111 static bool
2112 visit_reference_op_call (tree lhs, gimple stmt)
2114 bool changed = false;
2115 struct vn_reference_s vr1;
2116 tree result;
2117 tree vuse = gimple_vuse (stmt);
2119 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2120 vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2121 vr1.type = gimple_expr_type (stmt);
2122 vr1.set = 0;
2123 vr1.hashcode = vn_reference_compute_hash (&vr1);
2124 result = vn_reference_lookup_1 (&vr1, NULL);
2125 if (result)
2127 changed = set_ssa_val_to (lhs, result);
2128 if (TREE_CODE (result) == SSA_NAME
2129 && VN_INFO (result)->has_constants)
2130 VN_INFO (lhs)->has_constants = true;
2132 else
2134 void **slot;
2135 vn_reference_t vr2;
2136 changed = set_ssa_val_to (lhs, lhs);
2137 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2138 vr2->vuse = vr1.vuse;
2139 vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2140 vr2->type = vr1.type;
2141 vr2->set = vr1.set;
2142 vr2->hashcode = vr1.hashcode;
2143 vr2->result = lhs;
2144 slot = htab_find_slot_with_hash (current_info->references,
2145 vr2, vr2->hashcode, INSERT);
2146 if (*slot)
2147 free_reference (*slot);
2148 *slot = vr2;
2151 return changed;
2154 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2155 and return true if the value number of the LHS has changed as a result. */
2157 static bool
2158 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2160 bool changed = false;
2161 tree last_vuse;
2162 tree result;
2164 last_vuse = gimple_vuse (stmt);
2165 last_vuse_ptr = &last_vuse;
2166 result = vn_reference_lookup (op, gimple_vuse (stmt), true, NULL);
2167 last_vuse_ptr = NULL;
2169 /* If we have a VCE, try looking up its operand as it might be stored in
2170 a different type. */
2171 if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2172 result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2173 true, NULL);
2175 /* We handle type-punning through unions by value-numbering based
2176 on offset and size of the access. Be prepared to handle a
2177 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2178 if (result
2179 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2181 /* We will be setting the value number of lhs to the value number
2182 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2183 So first simplify and lookup this expression to see if it
2184 is already available. */
2185 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2186 if ((CONVERT_EXPR_P (val)
2187 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2188 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2190 tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2191 if ((CONVERT_EXPR_P (tem)
2192 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2193 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2194 TREE_TYPE (val), tem)))
2195 val = tem;
2197 result = val;
2198 if (!is_gimple_min_invariant (val)
2199 && TREE_CODE (val) != SSA_NAME)
2200 result = vn_nary_op_lookup (val, NULL);
2201 /* If the expression is not yet available, value-number lhs to
2202 a new SSA_NAME we create. */
2203 if (!result)
2205 result = make_ssa_name (SSA_NAME_VAR (lhs), gimple_build_nop ());
2206 /* Initialize value-number information properly. */
2207 VN_INFO_GET (result)->valnum = result;
2208 VN_INFO (result)->value_id = get_next_value_id ();
2209 VN_INFO (result)->expr = val;
2210 VN_INFO (result)->has_constants = expr_has_constants (val);
2211 VN_INFO (result)->needs_insertion = true;
2212 /* As all "inserted" statements are singleton SCCs, insert
2213 to the valid table. This is strictly needed to
2214 avoid re-generating new value SSA_NAMEs for the same
2215 expression during SCC iteration over and over (the
2216 optimistic table gets cleared after each iteration).
2217 We do not need to insert into the optimistic table, as
2218 lookups there will fall back to the valid table. */
2219 if (current_info == optimistic_info)
2221 current_info = valid_info;
2222 vn_nary_op_insert (val, result);
2223 current_info = optimistic_info;
2225 else
2226 vn_nary_op_insert (val, result);
2227 if (dump_file && (dump_flags & TDF_DETAILS))
2229 fprintf (dump_file, "Inserting name ");
2230 print_generic_expr (dump_file, result, 0);
2231 fprintf (dump_file, " for expression ");
2232 print_generic_expr (dump_file, val, 0);
2233 fprintf (dump_file, "\n");
2238 if (result)
2240 changed = set_ssa_val_to (lhs, result);
2241 if (TREE_CODE (result) == SSA_NAME
2242 && VN_INFO (result)->has_constants)
2244 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2245 VN_INFO (lhs)->has_constants = true;
2248 else
2250 changed = set_ssa_val_to (lhs, lhs);
2251 vn_reference_insert (op, lhs, last_vuse);
2254 return changed;
2258 /* Visit a store to a reference operator LHS, part of STMT, value number it,
2259 and return true if the value number of the LHS has changed as a result. */
2261 static bool
2262 visit_reference_op_store (tree lhs, tree op, gimple stmt)
2264 bool changed = false;
2265 tree result;
2266 bool resultsame = false;
2268 /* First we want to lookup using the *vuses* from the store and see
2269 if there the last store to this location with the same address
2270 had the same value.
2272 The vuses represent the memory state before the store. If the
2273 memory state, address, and value of the store is the same as the
2274 last store to this location, then this store will produce the
2275 same memory state as that store.
2277 In this case the vdef versions for this store are value numbered to those
2278 vuse versions, since they represent the same memory state after
2279 this store.
2281 Otherwise, the vdefs for the store are used when inserting into
2282 the table, since the store generates a new memory state. */
2284 result = vn_reference_lookup (lhs, gimple_vuse (stmt), false, NULL);
2286 if (result)
2288 if (TREE_CODE (result) == SSA_NAME)
2289 result = SSA_VAL (result);
2290 if (TREE_CODE (op) == SSA_NAME)
2291 op = SSA_VAL (op);
2292 resultsame = expressions_equal_p (result, op);
2295 if (!result || !resultsame)
2297 tree vdef;
2299 if (dump_file && (dump_flags & TDF_DETAILS))
2301 fprintf (dump_file, "No store match\n");
2302 fprintf (dump_file, "Value numbering store ");
2303 print_generic_expr (dump_file, lhs, 0);
2304 fprintf (dump_file, " to ");
2305 print_generic_expr (dump_file, op, 0);
2306 fprintf (dump_file, "\n");
2308 /* Have to set value numbers before insert, since insert is
2309 going to valueize the references in-place. */
2310 if ((vdef = gimple_vdef (stmt)))
2312 VN_INFO (vdef)->use_processed = true;
2313 changed |= set_ssa_val_to (vdef, vdef);
2316 /* Do not insert structure copies into the tables. */
2317 if (is_gimple_min_invariant (op)
2318 || is_gimple_reg (op))
2319 vn_reference_insert (lhs, op, vdef);
2321 else
2323 /* We had a match, so value number the vdef to have the value
2324 number of the vuse it came from. */
2325 tree def, use;
2327 if (dump_file && (dump_flags & TDF_DETAILS))
2328 fprintf (dump_file, "Store matched earlier value,"
2329 "value numbering store vdefs to matching vuses.\n");
2331 def = gimple_vdef (stmt);
2332 use = gimple_vuse (stmt);
2334 VN_INFO (def)->use_processed = true;
2335 changed |= set_ssa_val_to (def, SSA_VAL (use));
2338 return changed;
2341 /* Visit and value number PHI, return true if the value number
2342 changed. */
2344 static bool
2345 visit_phi (gimple phi)
2347 bool changed = false;
2348 tree result;
2349 tree sameval = VN_TOP;
2350 bool allsame = true;
2351 unsigned i;
2353 /* TODO: We could check for this in init_sccvn, and replace this
2354 with a gcc_assert. */
2355 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
2356 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2358 /* See if all non-TOP arguments have the same value. TOP is
2359 equivalent to everything, so we can ignore it. */
2360 for (i = 0; i < gimple_phi_num_args (phi); i++)
2362 tree def = PHI_ARG_DEF (phi, i);
2364 if (TREE_CODE (def) == SSA_NAME)
2365 def = SSA_VAL (def);
2366 if (def == VN_TOP)
2367 continue;
2368 if (sameval == VN_TOP)
2370 sameval = def;
2372 else
2374 if (!expressions_equal_p (def, sameval))
2376 allsame = false;
2377 break;
2382 /* If all value numbered to the same value, the phi node has that
2383 value. */
2384 if (allsame)
2386 if (is_gimple_min_invariant (sameval))
2388 VN_INFO (PHI_RESULT (phi))->has_constants = true;
2389 VN_INFO (PHI_RESULT (phi))->expr = sameval;
2391 else
2393 VN_INFO (PHI_RESULT (phi))->has_constants = false;
2394 VN_INFO (PHI_RESULT (phi))->expr = sameval;
2397 if (TREE_CODE (sameval) == SSA_NAME)
2398 return visit_copy (PHI_RESULT (phi), sameval);
2400 return set_ssa_val_to (PHI_RESULT (phi), sameval);
2403 /* Otherwise, see if it is equivalent to a phi node in this block. */
2404 result = vn_phi_lookup (phi);
2405 if (result)
2407 if (TREE_CODE (result) == SSA_NAME)
2408 changed = visit_copy (PHI_RESULT (phi), result);
2409 else
2410 changed = set_ssa_val_to (PHI_RESULT (phi), result);
2412 else
2414 vn_phi_insert (phi, PHI_RESULT (phi));
2415 VN_INFO (PHI_RESULT (phi))->has_constants = false;
2416 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
2417 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2420 return changed;
2423 /* Return true if EXPR contains constants. */
2425 static bool
2426 expr_has_constants (tree expr)
2428 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2430 case tcc_unary:
2431 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
2433 case tcc_binary:
2434 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
2435 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
2436 /* Constants inside reference ops are rarely interesting, but
2437 it can take a lot of looking to find them. */
2438 case tcc_reference:
2439 case tcc_declaration:
2440 return false;
2441 default:
2442 return is_gimple_min_invariant (expr);
2444 return false;
2447 /* Return true if STMT contains constants. */
2449 static bool
2450 stmt_has_constants (gimple stmt)
2452 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2453 return false;
2455 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2457 case GIMPLE_UNARY_RHS:
2458 return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2460 case GIMPLE_BINARY_RHS:
2461 return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
2462 || is_gimple_min_invariant (gimple_assign_rhs2 (stmt)));
2463 case GIMPLE_TERNARY_RHS:
2464 return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
2465 || is_gimple_min_invariant (gimple_assign_rhs2 (stmt))
2466 || is_gimple_min_invariant (gimple_assign_rhs3 (stmt)));
2467 case GIMPLE_SINGLE_RHS:
2468 /* Constants inside reference ops are rarely interesting, but
2469 it can take a lot of looking to find them. */
2470 return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2471 default:
2472 gcc_unreachable ();
2474 return false;
2477 /* Replace SSA_NAMES in expr with their value numbers, and return the
2478 result.
2479 This is performed in place. */
2481 static tree
2482 valueize_expr (tree expr)
2484 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2486 case tcc_unary:
2487 if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2488 && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2489 TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2490 break;
2491 case tcc_binary:
2492 if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2493 && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2494 TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2495 if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
2496 && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
2497 TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
2498 break;
2499 default:
2500 break;
2502 return expr;
2505 /* Simplify the binary expression RHS, and return the result if
2506 simplified. */
2508 static tree
2509 simplify_binary_expression (gimple stmt)
2511 tree result = NULL_TREE;
2512 tree op0 = gimple_assign_rhs1 (stmt);
2513 tree op1 = gimple_assign_rhs2 (stmt);
2515 /* This will not catch every single case we could combine, but will
2516 catch those with constants. The goal here is to simultaneously
2517 combine constants between expressions, but avoid infinite
2518 expansion of expressions during simplification. */
2519 if (TREE_CODE (op0) == SSA_NAME)
2521 if (VN_INFO (op0)->has_constants
2522 || TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
2523 op0 = valueize_expr (vn_get_expr_for (op0));
2524 else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
2525 op0 = SSA_VAL (op0);
2528 if (TREE_CODE (op1) == SSA_NAME)
2530 if (VN_INFO (op1)->has_constants)
2531 op1 = valueize_expr (vn_get_expr_for (op1));
2532 else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
2533 op1 = SSA_VAL (op1);
2536 /* Avoid folding if nothing changed. */
2537 if (op0 == gimple_assign_rhs1 (stmt)
2538 && op1 == gimple_assign_rhs2 (stmt))
2539 return NULL_TREE;
2541 fold_defer_overflow_warnings ();
2543 result = fold_binary (gimple_assign_rhs_code (stmt),
2544 gimple_expr_type (stmt), op0, op1);
2545 if (result)
2546 STRIP_USELESS_TYPE_CONVERSION (result);
2548 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
2549 stmt, 0);
2551 /* Make sure result is not a complex expression consisting
2552 of operators of operators (IE (a + b) + (a + c))
2553 Otherwise, we will end up with unbounded expressions if
2554 fold does anything at all. */
2555 if (result && valid_gimple_rhs_p (result))
2556 return result;
2558 return NULL_TREE;
2561 /* Simplify the unary expression RHS, and return the result if
2562 simplified. */
2564 static tree
2565 simplify_unary_expression (gimple stmt)
2567 tree result = NULL_TREE;
2568 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
2570 /* We handle some tcc_reference codes here that are all
2571 GIMPLE_ASSIGN_SINGLE codes. */
2572 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
2573 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2574 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2575 op0 = TREE_OPERAND (op0, 0);
2577 if (TREE_CODE (op0) != SSA_NAME)
2578 return NULL_TREE;
2580 orig_op0 = op0;
2581 if (VN_INFO (op0)->has_constants)
2582 op0 = valueize_expr (vn_get_expr_for (op0));
2583 else if (gimple_assign_cast_p (stmt)
2584 || gimple_assign_rhs_code (stmt) == REALPART_EXPR
2585 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2586 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2588 /* We want to do tree-combining on conversion-like expressions.
2589 Make sure we feed only SSA_NAMEs or constants to fold though. */
2590 tree tem = valueize_expr (vn_get_expr_for (op0));
2591 if (UNARY_CLASS_P (tem)
2592 || BINARY_CLASS_P (tem)
2593 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
2594 || TREE_CODE (tem) == SSA_NAME
2595 || is_gimple_min_invariant (tem))
2596 op0 = tem;
2599 /* Avoid folding if nothing changed, but remember the expression. */
2600 if (op0 == orig_op0)
2601 return NULL_TREE;
2603 result = fold_unary_ignore_overflow (gimple_assign_rhs_code (stmt),
2604 gimple_expr_type (stmt), op0);
2605 if (result)
2607 STRIP_USELESS_TYPE_CONVERSION (result);
2608 if (valid_gimple_rhs_p (result))
2609 return result;
2612 return NULL_TREE;
2615 /* Try to simplify RHS using equivalences and constant folding. */
2617 static tree
2618 try_to_simplify (gimple stmt)
2620 tree tem;
2622 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
2623 in this case, there is no point in doing extra work. */
2624 if (gimple_assign_copy_p (stmt)
2625 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2626 return NULL_TREE;
2628 switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2630 case tcc_declaration:
2631 tem = get_symbol_constant_value (gimple_assign_rhs1 (stmt));
2632 if (tem)
2633 return tem;
2634 break;
2636 case tcc_reference:
2637 /* Do not do full-blown reference lookup here, but simplify
2638 reads from constant aggregates. */
2639 tem = fold_const_aggregate_ref (gimple_assign_rhs1 (stmt));
2640 if (tem)
2641 return tem;
2643 /* Fallthrough for some codes that can operate on registers. */
2644 if (!(TREE_CODE (gimple_assign_rhs1 (stmt)) == REALPART_EXPR
2645 || TREE_CODE (gimple_assign_rhs1 (stmt)) == IMAGPART_EXPR
2646 || TREE_CODE (gimple_assign_rhs1 (stmt)) == VIEW_CONVERT_EXPR))
2647 break;
2648 /* We could do a little more with unary ops, if they expand
2649 into binary ops, but it's debatable whether it is worth it. */
2650 case tcc_unary:
2651 return simplify_unary_expression (stmt);
2652 break;
2653 case tcc_comparison:
2654 case tcc_binary:
2655 return simplify_binary_expression (stmt);
2656 break;
2657 default:
2658 break;
2661 return NULL_TREE;
2664 /* Visit and value number USE, return true if the value number
2665 changed. */
2667 static bool
2668 visit_use (tree use)
2670 bool changed = false;
2671 gimple stmt = SSA_NAME_DEF_STMT (use);
2673 VN_INFO (use)->use_processed = true;
2675 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
2676 if (dump_file && (dump_flags & TDF_DETAILS)
2677 && !SSA_NAME_IS_DEFAULT_DEF (use))
2679 fprintf (dump_file, "Value numbering ");
2680 print_generic_expr (dump_file, use, 0);
2681 fprintf (dump_file, " stmt = ");
2682 print_gimple_stmt (dump_file, stmt, 0, 0);
2685 /* Handle uninitialized uses. */
2686 if (SSA_NAME_IS_DEFAULT_DEF (use))
2687 changed = set_ssa_val_to (use, use);
2688 else
2690 if (gimple_code (stmt) == GIMPLE_PHI)
2691 changed = visit_phi (stmt);
2692 else if (!gimple_has_lhs (stmt)
2693 || gimple_has_volatile_ops (stmt)
2694 || stmt_could_throw_p (stmt))
2695 changed = defs_to_varying (stmt);
2696 else if (is_gimple_assign (stmt))
2698 tree lhs = gimple_assign_lhs (stmt);
2699 tree simplified;
2701 /* Shortcut for copies. Simplifying copies is pointless,
2702 since we copy the expression and value they represent. */
2703 if (gimple_assign_copy_p (stmt)
2704 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2705 && TREE_CODE (lhs) == SSA_NAME)
2707 changed = visit_copy (lhs, gimple_assign_rhs1 (stmt));
2708 goto done;
2710 simplified = try_to_simplify (stmt);
2711 if (simplified)
2713 if (dump_file && (dump_flags & TDF_DETAILS))
2715 fprintf (dump_file, "RHS ");
2716 print_gimple_expr (dump_file, stmt, 0, 0);
2717 fprintf (dump_file, " simplified to ");
2718 print_generic_expr (dump_file, simplified, 0);
2719 if (TREE_CODE (lhs) == SSA_NAME)
2720 fprintf (dump_file, " has constants %d\n",
2721 expr_has_constants (simplified));
2722 else
2723 fprintf (dump_file, "\n");
2726 /* Setting value numbers to constants will occasionally
2727 screw up phi congruence because constants are not
2728 uniquely associated with a single ssa name that can be
2729 looked up. */
2730 if (simplified
2731 && is_gimple_min_invariant (simplified)
2732 && TREE_CODE (lhs) == SSA_NAME)
2734 VN_INFO (lhs)->expr = simplified;
2735 VN_INFO (lhs)->has_constants = true;
2736 changed = set_ssa_val_to (lhs, simplified);
2737 goto done;
2739 else if (simplified
2740 && TREE_CODE (simplified) == SSA_NAME
2741 && TREE_CODE (lhs) == SSA_NAME)
2743 changed = visit_copy (lhs, simplified);
2744 goto done;
2746 else if (simplified)
2748 if (TREE_CODE (lhs) == SSA_NAME)
2750 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
2751 /* We have to unshare the expression or else
2752 valuizing may change the IL stream. */
2753 VN_INFO (lhs)->expr = unshare_expr (simplified);
2756 else if (stmt_has_constants (stmt)
2757 && TREE_CODE (lhs) == SSA_NAME)
2758 VN_INFO (lhs)->has_constants = true;
2759 else if (TREE_CODE (lhs) == SSA_NAME)
2761 /* We reset expr and constantness here because we may
2762 have been value numbering optimistically, and
2763 iterating. They may become non-constant in this case,
2764 even if they were optimistically constant. */
2766 VN_INFO (lhs)->has_constants = false;
2767 VN_INFO (lhs)->expr = NULL_TREE;
2770 if ((TREE_CODE (lhs) == SSA_NAME
2771 /* We can substitute SSA_NAMEs that are live over
2772 abnormal edges with their constant value. */
2773 && !(gimple_assign_copy_p (stmt)
2774 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2775 && !(simplified
2776 && is_gimple_min_invariant (simplified))
2777 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2778 /* Stores or copies from SSA_NAMEs that are live over
2779 abnormal edges are a problem. */
2780 || (gimple_assign_single_p (stmt)
2781 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2782 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt))))
2783 changed = defs_to_varying (stmt);
2784 else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
2786 changed = visit_reference_op_store (lhs, gimple_assign_rhs1 (stmt), stmt);
2788 else if (TREE_CODE (lhs) == SSA_NAME)
2790 if ((gimple_assign_copy_p (stmt)
2791 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2792 || (simplified
2793 && is_gimple_min_invariant (simplified)))
2795 VN_INFO (lhs)->has_constants = true;
2796 if (simplified)
2797 changed = set_ssa_val_to (lhs, simplified);
2798 else
2799 changed = set_ssa_val_to (lhs, gimple_assign_rhs1 (stmt));
2801 else
2803 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2805 case GIMPLE_UNARY_RHS:
2806 changed = visit_unary_op (lhs, stmt);
2807 break;
2808 case GIMPLE_BINARY_RHS:
2809 changed = visit_binary_op (lhs, stmt);
2810 break;
2811 case GIMPLE_SINGLE_RHS:
2812 switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2814 case tcc_reference:
2815 /* VOP-less references can go through unary case. */
2816 if ((gimple_assign_rhs_code (stmt) == REALPART_EXPR
2817 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2818 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR )
2819 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (stmt), 0)) == SSA_NAME)
2821 changed = visit_unary_op (lhs, stmt);
2822 break;
2824 /* Fallthrough. */
2825 case tcc_declaration:
2826 changed = visit_reference_op_load
2827 (lhs, gimple_assign_rhs1 (stmt), stmt);
2828 break;
2829 case tcc_expression:
2830 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
2832 changed = visit_unary_op (lhs, stmt);
2833 break;
2835 /* Fallthrough. */
2836 default:
2837 changed = defs_to_varying (stmt);
2839 break;
2840 default:
2841 changed = defs_to_varying (stmt);
2842 break;
2846 else
2847 changed = defs_to_varying (stmt);
2849 else if (is_gimple_call (stmt))
2851 tree lhs = gimple_call_lhs (stmt);
2853 /* ??? We could try to simplify calls. */
2855 if (stmt_has_constants (stmt)
2856 && TREE_CODE (lhs) == SSA_NAME)
2857 VN_INFO (lhs)->has_constants = true;
2858 else if (TREE_CODE (lhs) == SSA_NAME)
2860 /* We reset expr and constantness here because we may
2861 have been value numbering optimistically, and
2862 iterating. They may become non-constant in this case,
2863 even if they were optimistically constant. */
2864 VN_INFO (lhs)->has_constants = false;
2865 VN_INFO (lhs)->expr = NULL_TREE;
2868 if (TREE_CODE (lhs) == SSA_NAME
2869 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2870 changed = defs_to_varying (stmt);
2871 /* ??? We should handle stores from calls. */
2872 else if (TREE_CODE (lhs) == SSA_NAME)
2874 if (gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST))
2875 changed = visit_reference_op_call (lhs, stmt);
2876 else
2877 changed = defs_to_varying (stmt);
2879 else
2880 changed = defs_to_varying (stmt);
2883 done:
2884 return changed;
2887 /* Compare two operands by reverse postorder index */
2889 static int
2890 compare_ops (const void *pa, const void *pb)
2892 const tree opa = *((const tree *)pa);
2893 const tree opb = *((const tree *)pb);
2894 gimple opstmta = SSA_NAME_DEF_STMT (opa);
2895 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
2896 basic_block bba;
2897 basic_block bbb;
2899 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
2900 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2901 else if (gimple_nop_p (opstmta))
2902 return -1;
2903 else if (gimple_nop_p (opstmtb))
2904 return 1;
2906 bba = gimple_bb (opstmta);
2907 bbb = gimple_bb (opstmtb);
2909 if (!bba && !bbb)
2910 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2911 else if (!bba)
2912 return -1;
2913 else if (!bbb)
2914 return 1;
2916 if (bba == bbb)
2918 if (gimple_code (opstmta) == GIMPLE_PHI
2919 && gimple_code (opstmtb) == GIMPLE_PHI)
2920 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2921 else if (gimple_code (opstmta) == GIMPLE_PHI)
2922 return -1;
2923 else if (gimple_code (opstmtb) == GIMPLE_PHI)
2924 return 1;
2925 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
2926 return gimple_uid (opstmta) - gimple_uid (opstmtb);
2927 else
2928 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2930 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
2933 /* Sort an array containing members of a strongly connected component
2934 SCC so that the members are ordered by RPO number.
2935 This means that when the sort is complete, iterating through the
2936 array will give you the members in RPO order. */
2938 static void
2939 sort_scc (VEC (tree, heap) *scc)
2941 qsort (VEC_address (tree, scc),
2942 VEC_length (tree, scc),
2943 sizeof (tree),
2944 compare_ops);
2947 /* Insert the no longer used nary ONARY to the hash INFO. */
2949 static void
2950 copy_nary (vn_nary_op_t onary, vn_tables_t info)
2952 size_t size = (sizeof (struct vn_nary_op_s)
2953 - sizeof (tree) * (4 - onary->length));
2954 vn_nary_op_t nary = (vn_nary_op_t) obstack_alloc (&info->nary_obstack, size);
2955 void **slot;
2956 memcpy (nary, onary, size);
2957 slot = htab_find_slot_with_hash (info->nary, nary, nary->hashcode, INSERT);
2958 gcc_assert (!*slot);
2959 *slot = nary;
2962 /* Insert the no longer used phi OPHI to the hash INFO. */
2964 static void
2965 copy_phi (vn_phi_t ophi, vn_tables_t info)
2967 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
2968 void **slot;
2969 memcpy (phi, ophi, sizeof (*phi));
2970 ophi->phiargs = NULL;
2971 slot = htab_find_slot_with_hash (info->phis, phi, phi->hashcode, INSERT);
2972 gcc_assert (!*slot);
2973 *slot = phi;
2976 /* Insert the no longer used reference OREF to the hash INFO. */
2978 static void
2979 copy_reference (vn_reference_t oref, vn_tables_t info)
2981 vn_reference_t ref;
2982 void **slot;
2983 ref = (vn_reference_t) pool_alloc (info->references_pool);
2984 memcpy (ref, oref, sizeof (*ref));
2985 oref->operands = NULL;
2986 slot = htab_find_slot_with_hash (info->references, ref, ref->hashcode,
2987 INSERT);
2988 if (*slot)
2989 free_reference (*slot);
2990 *slot = ref;
2993 /* Process a strongly connected component in the SSA graph. */
2995 static void
2996 process_scc (VEC (tree, heap) *scc)
2998 tree var;
2999 unsigned int i;
3000 unsigned int iterations = 0;
3001 bool changed = true;
3002 htab_iterator hi;
3003 vn_nary_op_t nary;
3004 vn_phi_t phi;
3005 vn_reference_t ref;
3007 /* If the SCC has a single member, just visit it. */
3008 if (VEC_length (tree, scc) == 1)
3010 tree use = VEC_index (tree, scc, 0);
3011 if (!VN_INFO (use)->use_processed)
3012 visit_use (use);
3013 return;
3016 /* Iterate over the SCC with the optimistic table until it stops
3017 changing. */
3018 current_info = optimistic_info;
3019 while (changed)
3021 changed = false;
3022 iterations++;
3023 /* As we are value-numbering optimistically we have to
3024 clear the expression tables and the simplified expressions
3025 in each iteration until we converge. */
3026 htab_empty (optimistic_info->nary);
3027 htab_empty (optimistic_info->phis);
3028 htab_empty (optimistic_info->references);
3029 obstack_free (&optimistic_info->nary_obstack, NULL);
3030 gcc_obstack_init (&optimistic_info->nary_obstack);
3031 empty_alloc_pool (optimistic_info->phis_pool);
3032 empty_alloc_pool (optimistic_info->references_pool);
3033 for (i = 0; VEC_iterate (tree, scc, i, var); i++)
3034 VN_INFO (var)->expr = NULL_TREE;
3035 for (i = 0; VEC_iterate (tree, scc, i, var); i++)
3036 changed |= visit_use (var);
3039 statistics_histogram_event (cfun, "SCC iterations", iterations);
3041 /* Finally, copy the contents of the no longer used optimistic
3042 table to the valid table. */
3043 FOR_EACH_HTAB_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hi)
3044 copy_nary (nary, valid_info);
3045 FOR_EACH_HTAB_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hi)
3046 copy_phi (phi, valid_info);
3047 FOR_EACH_HTAB_ELEMENT (optimistic_info->references, ref, vn_reference_t, hi)
3048 copy_reference (ref, valid_info);
3050 current_info = valid_info;
3053 DEF_VEC_O(ssa_op_iter);
3054 DEF_VEC_ALLOC_O(ssa_op_iter,heap);
3056 /* Pop the components of the found SCC for NAME off the SCC stack
3057 and process them. Returns true if all went well, false if
3058 we run into resource limits. */
3060 static bool
3061 extract_and_process_scc_for_name (tree name)
3063 VEC (tree, heap) *scc = NULL;
3064 tree x;
3066 /* Found an SCC, pop the components off the SCC stack and
3067 process them. */
3070 x = VEC_pop (tree, sccstack);
3072 VN_INFO (x)->on_sccstack = false;
3073 VEC_safe_push (tree, heap, scc, x);
3074 } while (x != name);
3076 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3077 if (VEC_length (tree, scc)
3078 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3080 if (dump_file)
3081 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3082 "SCC size %u exceeding %u\n", VEC_length (tree, scc),
3083 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3084 return false;
3087 if (VEC_length (tree, scc) > 1)
3088 sort_scc (scc);
3090 if (dump_file && (dump_flags & TDF_DETAILS))
3091 print_scc (dump_file, scc);
3093 process_scc (scc);
3095 VEC_free (tree, heap, scc);
3097 return true;
3100 /* Depth first search on NAME to discover and process SCC's in the SSA
3101 graph.
3102 Execution of this algorithm relies on the fact that the SCC's are
3103 popped off the stack in topological order.
3104 Returns true if successful, false if we stopped processing SCC's due
3105 to resource constraints. */
3107 static bool
3108 DFS (tree name)
3110 VEC(ssa_op_iter, heap) *itervec = NULL;
3111 VEC(tree, heap) *namevec = NULL;
3112 use_operand_p usep = NULL;
3113 gimple defstmt;
3114 tree use;
3115 ssa_op_iter iter;
3117 start_over:
3118 /* SCC info */
3119 VN_INFO (name)->dfsnum = next_dfs_num++;
3120 VN_INFO (name)->visited = true;
3121 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3123 VEC_safe_push (tree, heap, sccstack, name);
3124 VN_INFO (name)->on_sccstack = true;
3125 defstmt = SSA_NAME_DEF_STMT (name);
3127 /* Recursively DFS on our operands, looking for SCC's. */
3128 if (!gimple_nop_p (defstmt))
3130 /* Push a new iterator. */
3131 if (gimple_code (defstmt) == GIMPLE_PHI)
3132 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3133 else
3134 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3136 else
3137 clear_and_done_ssa_iter (&iter);
3139 while (1)
3141 /* If we are done processing uses of a name, go up the stack
3142 of iterators and process SCCs as we found them. */
3143 if (op_iter_done (&iter))
3145 /* See if we found an SCC. */
3146 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3147 if (!extract_and_process_scc_for_name (name))
3149 VEC_free (tree, heap, namevec);
3150 VEC_free (ssa_op_iter, heap, itervec);
3151 return false;
3154 /* Check if we are done. */
3155 if (VEC_empty (tree, namevec))
3157 VEC_free (tree, heap, namevec);
3158 VEC_free (ssa_op_iter, heap, itervec);
3159 return true;
3162 /* Restore the last use walker and continue walking there. */
3163 use = name;
3164 name = VEC_pop (tree, namevec);
3165 memcpy (&iter, VEC_last (ssa_op_iter, itervec),
3166 sizeof (ssa_op_iter));
3167 VEC_pop (ssa_op_iter, itervec);
3168 goto continue_walking;
3171 use = USE_FROM_PTR (usep);
3173 /* Since we handle phi nodes, we will sometimes get
3174 invariants in the use expression. */
3175 if (TREE_CODE (use) == SSA_NAME)
3177 if (! (VN_INFO (use)->visited))
3179 /* Recurse by pushing the current use walking state on
3180 the stack and starting over. */
3181 VEC_safe_push(ssa_op_iter, heap, itervec, &iter);
3182 VEC_safe_push(tree, heap, namevec, name);
3183 name = use;
3184 goto start_over;
3186 continue_walking:
3187 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3188 VN_INFO (use)->low);
3190 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3191 && VN_INFO (use)->on_sccstack)
3193 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3194 VN_INFO (name)->low);
3198 usep = op_iter_next_use (&iter);
3202 /* Allocate a value number table. */
3204 static void
3205 allocate_vn_table (vn_tables_t table)
3207 table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
3208 table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
3209 table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
3210 free_reference);
3212 gcc_obstack_init (&table->nary_obstack);
3213 table->phis_pool = create_alloc_pool ("VN phis",
3214 sizeof (struct vn_phi_s),
3215 30);
3216 table->references_pool = create_alloc_pool ("VN references",
3217 sizeof (struct vn_reference_s),
3218 30);
3221 /* Free a value number table. */
3223 static void
3224 free_vn_table (vn_tables_t table)
3226 htab_delete (table->phis);
3227 htab_delete (table->nary);
3228 htab_delete (table->references);
3229 obstack_free (&table->nary_obstack, NULL);
3230 free_alloc_pool (table->phis_pool);
3231 free_alloc_pool (table->references_pool);
3234 static void
3235 init_scc_vn (void)
3237 size_t i;
3238 int j;
3239 int *rpo_numbers_temp;
3241 calculate_dominance_info (CDI_DOMINATORS);
3242 sccstack = NULL;
3243 constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
3244 free);
3246 constant_value_ids = BITMAP_ALLOC (NULL);
3248 next_dfs_num = 1;
3249 next_value_id = 1;
3251 vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
3252 /* VEC_alloc doesn't actually grow it to the right size, it just
3253 preallocates the space to do so. */
3254 VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
3255 gcc_obstack_init (&vn_ssa_aux_obstack);
3257 shared_lookup_phiargs = NULL;
3258 shared_lookup_references = NULL;
3259 rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3260 rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3261 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3263 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3264 the i'th block in RPO order is bb. We want to map bb's to RPO
3265 numbers, so we need to rearrange this array. */
3266 for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3267 rpo_numbers[rpo_numbers_temp[j]] = j;
3269 XDELETE (rpo_numbers_temp);
3271 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3273 /* Create the VN_INFO structures, and initialize value numbers to
3274 TOP. */
3275 for (i = 0; i < num_ssa_names; i++)
3277 tree name = ssa_name (i);
3278 if (name)
3280 VN_INFO_GET (name)->valnum = VN_TOP;
3281 VN_INFO (name)->expr = NULL_TREE;
3282 VN_INFO (name)->value_id = 0;
3286 renumber_gimple_stmt_uids ();
3288 /* Create the valid and optimistic value numbering tables. */
3289 valid_info = XCNEW (struct vn_tables_s);
3290 allocate_vn_table (valid_info);
3291 optimistic_info = XCNEW (struct vn_tables_s);
3292 allocate_vn_table (optimistic_info);
3295 void
3296 free_scc_vn (void)
3298 size_t i;
3300 htab_delete (constant_to_value_id);
3301 BITMAP_FREE (constant_value_ids);
3302 VEC_free (tree, heap, shared_lookup_phiargs);
3303 VEC_free (vn_reference_op_s, heap, shared_lookup_references);
3304 XDELETEVEC (rpo_numbers);
3306 for (i = 0; i < num_ssa_names; i++)
3308 tree name = ssa_name (i);
3309 if (name
3310 && VN_INFO (name)->needs_insertion)
3311 release_ssa_name (name);
3313 obstack_free (&vn_ssa_aux_obstack, NULL);
3314 VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
3316 VEC_free (tree, heap, sccstack);
3317 free_vn_table (valid_info);
3318 XDELETE (valid_info);
3319 free_vn_table (optimistic_info);
3320 XDELETE (optimistic_info);
3323 /* Set the value ids in the valid hash tables. */
3325 static void
3326 set_hashtable_value_ids (void)
3328 htab_iterator hi;
3329 vn_nary_op_t vno;
3330 vn_reference_t vr;
3331 vn_phi_t vp;
3333 /* Now set the value ids of the things we had put in the hash
3334 table. */
3336 FOR_EACH_HTAB_ELEMENT (valid_info->nary,
3337 vno, vn_nary_op_t, hi)
3339 if (vno->result)
3341 if (TREE_CODE (vno->result) == SSA_NAME)
3342 vno->value_id = VN_INFO (vno->result)->value_id;
3343 else if (is_gimple_min_invariant (vno->result))
3344 vno->value_id = get_or_alloc_constant_value_id (vno->result);
3348 FOR_EACH_HTAB_ELEMENT (valid_info->phis,
3349 vp, vn_phi_t, hi)
3351 if (vp->result)
3353 if (TREE_CODE (vp->result) == SSA_NAME)
3354 vp->value_id = VN_INFO (vp->result)->value_id;
3355 else if (is_gimple_min_invariant (vp->result))
3356 vp->value_id = get_or_alloc_constant_value_id (vp->result);
3360 FOR_EACH_HTAB_ELEMENT (valid_info->references,
3361 vr, vn_reference_t, hi)
3363 if (vr->result)
3365 if (TREE_CODE (vr->result) == SSA_NAME)
3366 vr->value_id = VN_INFO (vr->result)->value_id;
3367 else if (is_gimple_min_invariant (vr->result))
3368 vr->value_id = get_or_alloc_constant_value_id (vr->result);
3373 /* Do SCCVN. Returns true if it finished, false if we bailed out
3374 due to resource constraints. */
3376 bool
3377 run_scc_vn (void)
3379 size_t i;
3380 tree param;
3381 bool changed = true;
3383 init_scc_vn ();
3384 current_info = valid_info;
3386 for (param = DECL_ARGUMENTS (current_function_decl);
3387 param;
3388 param = TREE_CHAIN (param))
3390 if (gimple_default_def (cfun, param) != NULL)
3392 tree def = gimple_default_def (cfun, param);
3393 VN_INFO (def)->valnum = def;
3397 for (i = 1; i < num_ssa_names; ++i)
3399 tree name = ssa_name (i);
3400 if (name
3401 && VN_INFO (name)->visited == false
3402 && !has_zero_uses (name))
3403 if (!DFS (name))
3405 free_scc_vn ();
3406 return false;
3410 /* Initialize the value ids. */
3412 for (i = 1; i < num_ssa_names; ++i)
3414 tree name = ssa_name (i);
3415 vn_ssa_aux_t info;
3416 if (!name)
3417 continue;
3418 info = VN_INFO (name);
3419 if (info->valnum == name
3420 || info->valnum == VN_TOP)
3421 info->value_id = get_next_value_id ();
3422 else if (is_gimple_min_invariant (info->valnum))
3423 info->value_id = get_or_alloc_constant_value_id (info->valnum);
3426 /* Propagate until they stop changing. */
3427 while (changed)
3429 changed = false;
3430 for (i = 1; i < num_ssa_names; ++i)
3432 tree name = ssa_name (i);
3433 vn_ssa_aux_t info;
3434 if (!name)
3435 continue;
3436 info = VN_INFO (name);
3437 if (TREE_CODE (info->valnum) == SSA_NAME
3438 && info->valnum != name
3439 && info->value_id != VN_INFO (info->valnum)->value_id)
3441 changed = true;
3442 info->value_id = VN_INFO (info->valnum)->value_id;
3447 set_hashtable_value_ids ();
3449 if (dump_file && (dump_flags & TDF_DETAILS))
3451 fprintf (dump_file, "Value numbers:\n");
3452 for (i = 0; i < num_ssa_names; i++)
3454 tree name = ssa_name (i);
3455 if (name
3456 && VN_INFO (name)->visited
3457 && SSA_VAL (name) != name)
3459 print_generic_expr (dump_file, name, 0);
3460 fprintf (dump_file, " = ");
3461 print_generic_expr (dump_file, SSA_VAL (name), 0);
3462 fprintf (dump_file, "\n");
3467 return true;
3470 /* Return the maximum value id we have ever seen. */
3472 unsigned int
3473 get_max_value_id (void)
3475 return next_value_id;
3478 /* Return the next unique value id. */
3480 unsigned int
3481 get_next_value_id (void)
3483 return next_value_id++;
3487 /* Compare two expressions E1 and E2 and return true if they are equal. */
3489 bool
3490 expressions_equal_p (tree e1, tree e2)
3492 /* The obvious case. */
3493 if (e1 == e2)
3494 return true;
3496 /* If only one of them is null, they cannot be equal. */
3497 if (!e1 || !e2)
3498 return false;
3500 /* Now perform the actual comparison. */
3501 if (TREE_CODE (e1) == TREE_CODE (e2)
3502 && operand_equal_p (e1, e2, OEP_PURE_SAME))
3503 return true;
3505 return false;
3509 /* Return true if the nary operation NARY may trap. This is a copy
3510 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
3512 bool
3513 vn_nary_may_trap (vn_nary_op_t nary)
3515 tree type;
3516 tree rhs2 = NULL_TREE;
3517 bool honor_nans = false;
3518 bool honor_snans = false;
3519 bool fp_operation = false;
3520 bool honor_trapv = false;
3521 bool handled, ret;
3522 unsigned i;
3524 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
3525 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
3526 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
3528 type = nary->type;
3529 fp_operation = FLOAT_TYPE_P (type);
3530 if (fp_operation)
3532 honor_nans = flag_trapping_math && !flag_finite_math_only;
3533 honor_snans = flag_signaling_nans != 0;
3535 else if (INTEGRAL_TYPE_P (type)
3536 && TYPE_OVERFLOW_TRAPS (type))
3537 honor_trapv = true;
3539 if (nary->length >= 2)
3540 rhs2 = nary->op[1];
3541 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
3542 honor_trapv,
3543 honor_nans, honor_snans, rhs2,
3544 &handled);
3545 if (handled
3546 && ret)
3547 return true;
3549 for (i = 0; i < nary->length; ++i)
3550 if (tree_could_trap_p (nary->op[i]))
3551 return true;
3553 return false;