Reverting merge from trunk
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob1be324e8d5da1292dde6d1ad0bf4bb3693009319
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "basic-block.h"
27 #include "gimple-pretty-print.h"
28 #include "tree-inline.h"
29 #include "gimple.h"
30 #include "gimplify.h"
31 #include "gimple-ssa.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-ssanames.h"
35 #include "tree-dfa.h"
36 #include "tree-ssa.h"
37 #include "dumpfile.h"
38 #include "hash-table.h"
39 #include "alloc-pool.h"
40 #include "flags.h"
41 #include "cfgloop.h"
42 #include "params.h"
43 #include "tree-ssa-propagate.h"
44 #include "tree-ssa-sccvn.h"
46 /* This algorithm is based on the SCC algorithm presented by Keith
47 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
48 (http://citeseer.ist.psu.edu/41805.html). In
49 straight line code, it is equivalent to a regular hash based value
50 numbering that is performed in reverse postorder.
52 For code with cycles, there are two alternatives, both of which
53 require keeping the hashtables separate from the actual list of
54 value numbers for SSA names.
56 1. Iterate value numbering in an RPO walk of the blocks, removing
57 all the entries from the hashtable after each iteration (but
58 keeping the SSA name->value number mapping between iterations).
59 Iterate until it does not change.
61 2. Perform value numbering as part of an SCC walk on the SSA graph,
62 iterating only the cycles in the SSA graph until they do not change
63 (using a separate, optimistic hashtable for value numbering the SCC
64 operands).
66 The second is not just faster in practice (because most SSA graph
67 cycles do not involve all the variables in the graph), it also has
68 some nice properties.
70 One of these nice properties is that when we pop an SCC off the
71 stack, we are guaranteed to have processed all the operands coming from
72 *outside of that SCC*, so we do not need to do anything special to
73 ensure they have value numbers.
75 Another nice property is that the SCC walk is done as part of a DFS
76 of the SSA graph, which makes it easy to perform combining and
77 simplifying operations at the same time.
79 The code below is deliberately written in a way that makes it easy
80 to separate the SCC walk from the other work it does.
82 In order to propagate constants through the code, we track which
83 expressions contain constants, and use those while folding. In
84 theory, we could also track expressions whose value numbers are
85 replaced, in case we end up folding based on expression
86 identities.
88 In order to value number memory, we assign value numbers to vuses.
89 This enables us to note that, for example, stores to the same
90 address of the same value from the same starting memory states are
91 equivalent.
92 TODO:
94 1. We can iterate only the changing portions of the SCC's, but
95 I have not seen an SCC big enough for this to be a win.
96 2. If you differentiate between phi nodes for loops and phi nodes
97 for if-then-else, you can properly consider phi nodes in different
98 blocks for equivalence.
99 3. We could value number vuses in more cases, particularly, whole
100 structure copies.
104 /* vn_nary_op hashtable helpers. */
106 struct vn_nary_op_hasher : typed_noop_remove <vn_nary_op_s>
108 typedef vn_nary_op_s value_type;
109 typedef vn_nary_op_s compare_type;
110 static inline hashval_t hash (const value_type *);
111 static inline bool equal (const value_type *, const compare_type *);
114 /* Return the computed hashcode for nary operation P1. */
116 inline hashval_t
117 vn_nary_op_hasher::hash (const value_type *vno1)
119 return vno1->hashcode;
122 /* Compare nary operations P1 and P2 and return true if they are
123 equivalent. */
125 inline bool
126 vn_nary_op_hasher::equal (const value_type *vno1, const compare_type *vno2)
128 return vn_nary_op_eq (vno1, vno2);
131 typedef hash_table <vn_nary_op_hasher> vn_nary_op_table_type;
132 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
135 /* vn_phi hashtable helpers. */
137 static int
138 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
140 struct vn_phi_hasher
142 typedef vn_phi_s value_type;
143 typedef vn_phi_s compare_type;
144 static inline hashval_t hash (const value_type *);
145 static inline bool equal (const value_type *, const compare_type *);
146 static inline void remove (value_type *);
149 /* Return the computed hashcode for phi operation P1. */
151 inline hashval_t
152 vn_phi_hasher::hash (const value_type *vp1)
154 return vp1->hashcode;
157 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
159 inline bool
160 vn_phi_hasher::equal (const value_type *vp1, const compare_type *vp2)
162 return vn_phi_eq (vp1, vp2);
165 /* Free a phi operation structure VP. */
167 inline void
168 vn_phi_hasher::remove (value_type *phi)
170 phi->phiargs.release ();
173 typedef hash_table <vn_phi_hasher> vn_phi_table_type;
174 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
177 /* Compare two reference operands P1 and P2 for equality. Return true if
178 they are equal, and false otherwise. */
180 static int
181 vn_reference_op_eq (const void *p1, const void *p2)
183 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
184 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
186 return (vro1->opcode == vro2->opcode
187 /* We do not care for differences in type qualification. */
188 && (vro1->type == vro2->type
189 || (vro1->type && vro2->type
190 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
191 TYPE_MAIN_VARIANT (vro2->type))))
192 && expressions_equal_p (vro1->op0, vro2->op0)
193 && expressions_equal_p (vro1->op1, vro2->op1)
194 && expressions_equal_p (vro1->op2, vro2->op2));
197 /* Free a reference operation structure VP. */
199 static inline void
200 free_reference (vn_reference_s *vr)
202 vr->operands.release ();
206 /* vn_reference hashtable helpers. */
208 struct vn_reference_hasher
210 typedef vn_reference_s value_type;
211 typedef vn_reference_s compare_type;
212 static inline hashval_t hash (const value_type *);
213 static inline bool equal (const value_type *, const compare_type *);
214 static inline void remove (value_type *);
217 /* Return the hashcode for a given reference operation P1. */
219 inline hashval_t
220 vn_reference_hasher::hash (const value_type *vr1)
222 return vr1->hashcode;
225 inline bool
226 vn_reference_hasher::equal (const value_type *v, const compare_type *c)
228 return vn_reference_eq (v, c);
231 inline void
232 vn_reference_hasher::remove (value_type *v)
234 free_reference (v);
237 typedef hash_table <vn_reference_hasher> vn_reference_table_type;
238 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
241 /* The set of hashtables and alloc_pool's for their items. */
243 typedef struct vn_tables_s
245 vn_nary_op_table_type nary;
246 vn_phi_table_type phis;
247 vn_reference_table_type references;
248 struct obstack nary_obstack;
249 alloc_pool phis_pool;
250 alloc_pool references_pool;
251 } *vn_tables_t;
254 /* vn_constant hashtable helpers. */
256 struct vn_constant_hasher : typed_free_remove <vn_constant_s>
258 typedef vn_constant_s value_type;
259 typedef vn_constant_s compare_type;
260 static inline hashval_t hash (const value_type *);
261 static inline bool equal (const value_type *, const compare_type *);
264 /* Hash table hash function for vn_constant_t. */
266 inline hashval_t
267 vn_constant_hasher::hash (const value_type *vc1)
269 return vc1->hashcode;
272 /* Hash table equality function for vn_constant_t. */
274 inline bool
275 vn_constant_hasher::equal (const value_type *vc1, const compare_type *vc2)
277 if (vc1->hashcode != vc2->hashcode)
278 return false;
280 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
283 static hash_table <vn_constant_hasher> constant_to_value_id;
284 static bitmap constant_value_ids;
287 /* Valid hashtables storing information we have proven to be
288 correct. */
290 static vn_tables_t valid_info;
292 /* Optimistic hashtables storing information we are making assumptions about
293 during iterations. */
295 static vn_tables_t optimistic_info;
297 /* Pointer to the set of hashtables that is currently being used.
298 Should always point to either the optimistic_info, or the
299 valid_info. */
301 static vn_tables_t current_info;
304 /* Reverse post order index for each basic block. */
306 static int *rpo_numbers;
308 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
310 /* This represents the top of the VN lattice, which is the universal
311 value. */
313 tree VN_TOP;
315 /* Unique counter for our value ids. */
317 static unsigned int next_value_id;
319 /* Next DFS number and the stack for strongly connected component
320 detection. */
322 static unsigned int next_dfs_num;
323 static vec<tree> sccstack;
327 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
328 are allocated on an obstack for locality reasons, and to free them
329 without looping over the vec. */
331 static vec<vn_ssa_aux_t> vn_ssa_aux_table;
332 static struct obstack vn_ssa_aux_obstack;
334 /* Return the value numbering information for a given SSA name. */
336 vn_ssa_aux_t
337 VN_INFO (tree name)
339 vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)];
340 gcc_checking_assert (res);
341 return res;
344 /* Set the value numbering info for a given SSA name to a given
345 value. */
347 static inline void
348 VN_INFO_SET (tree name, vn_ssa_aux_t value)
350 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value;
353 /* Initialize the value numbering info for a given SSA name.
354 This should be called just once for every SSA name. */
356 vn_ssa_aux_t
357 VN_INFO_GET (tree name)
359 vn_ssa_aux_t newinfo;
361 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
362 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
363 if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ())
364 vn_ssa_aux_table.safe_grow (SSA_NAME_VERSION (name) + 1);
365 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo;
366 return newinfo;
370 /* Get the representative expression for the SSA_NAME NAME. Returns
371 the representative SSA_NAME if there is no expression associated with it. */
373 tree
374 vn_get_expr_for (tree name)
376 vn_ssa_aux_t vn = VN_INFO (name);
377 gimple def_stmt;
378 tree expr = NULL_TREE;
379 enum tree_code code;
381 if (vn->valnum == VN_TOP)
382 return name;
384 /* If the value-number is a constant it is the representative
385 expression. */
386 if (TREE_CODE (vn->valnum) != SSA_NAME)
387 return vn->valnum;
389 /* Get to the information of the value of this SSA_NAME. */
390 vn = VN_INFO (vn->valnum);
392 /* If the value-number is a constant it is the representative
393 expression. */
394 if (TREE_CODE (vn->valnum) != SSA_NAME)
395 return vn->valnum;
397 /* Else if we have an expression, return it. */
398 if (vn->expr != NULL_TREE)
399 return vn->expr;
401 /* Otherwise use the defining statement to build the expression. */
402 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
404 /* If the value number is not an assignment use it directly. */
405 if (!is_gimple_assign (def_stmt))
406 return vn->valnum;
408 /* FIXME tuples. This is incomplete and likely will miss some
409 simplifications. */
410 code = gimple_assign_rhs_code (def_stmt);
411 switch (TREE_CODE_CLASS (code))
413 case tcc_reference:
414 if ((code == REALPART_EXPR
415 || code == IMAGPART_EXPR
416 || code == VIEW_CONVERT_EXPR)
417 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
418 0)) == SSA_NAME)
419 expr = fold_build1 (code,
420 gimple_expr_type (def_stmt),
421 TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
422 break;
424 case tcc_unary:
425 expr = fold_build1 (code,
426 gimple_expr_type (def_stmt),
427 gimple_assign_rhs1 (def_stmt));
428 break;
430 case tcc_binary:
431 expr = fold_build2 (code,
432 gimple_expr_type (def_stmt),
433 gimple_assign_rhs1 (def_stmt),
434 gimple_assign_rhs2 (def_stmt));
435 break;
437 case tcc_exceptional:
438 if (code == CONSTRUCTOR
439 && TREE_CODE
440 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
441 expr = gimple_assign_rhs1 (def_stmt);
442 break;
444 default:;
446 if (expr == NULL_TREE)
447 return vn->valnum;
449 /* Cache the expression. */
450 vn->expr = expr;
452 return expr;
455 /* Return the vn_kind the expression computed by the stmt should be
456 associated with. */
458 enum vn_kind
459 vn_get_stmt_kind (gimple stmt)
461 switch (gimple_code (stmt))
463 case GIMPLE_CALL:
464 return VN_REFERENCE;
465 case GIMPLE_PHI:
466 return VN_PHI;
467 case GIMPLE_ASSIGN:
469 enum tree_code code = gimple_assign_rhs_code (stmt);
470 tree rhs1 = gimple_assign_rhs1 (stmt);
471 switch (get_gimple_rhs_class (code))
473 case GIMPLE_UNARY_RHS:
474 case GIMPLE_BINARY_RHS:
475 case GIMPLE_TERNARY_RHS:
476 return VN_NARY;
477 case GIMPLE_SINGLE_RHS:
478 switch (TREE_CODE_CLASS (code))
480 case tcc_reference:
481 /* VOP-less references can go through unary case. */
482 if ((code == REALPART_EXPR
483 || code == IMAGPART_EXPR
484 || code == VIEW_CONVERT_EXPR
485 || code == BIT_FIELD_REF)
486 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
487 return VN_NARY;
489 /* Fallthrough. */
490 case tcc_declaration:
491 return VN_REFERENCE;
493 case tcc_constant:
494 return VN_CONSTANT;
496 default:
497 if (code == ADDR_EXPR)
498 return (is_gimple_min_invariant (rhs1)
499 ? VN_CONSTANT : VN_REFERENCE);
500 else if (code == CONSTRUCTOR)
501 return VN_NARY;
502 return VN_NONE;
504 default:
505 return VN_NONE;
508 default:
509 return VN_NONE;
513 /* Lookup a value id for CONSTANT and return it. If it does not
514 exist returns 0. */
516 unsigned int
517 get_constant_value_id (tree constant)
519 vn_constant_s **slot;
520 struct vn_constant_s vc;
522 vc.hashcode = vn_hash_constant_with_type (constant);
523 vc.constant = constant;
524 slot = constant_to_value_id.find_slot_with_hash (&vc, vc.hashcode, NO_INSERT);
525 if (slot)
526 return (*slot)->value_id;
527 return 0;
530 /* Lookup a value id for CONSTANT, and if it does not exist, create a
531 new one and return it. If it does exist, return it. */
533 unsigned int
534 get_or_alloc_constant_value_id (tree constant)
536 vn_constant_s **slot;
537 struct vn_constant_s vc;
538 vn_constant_t vcp;
540 vc.hashcode = vn_hash_constant_with_type (constant);
541 vc.constant = constant;
542 slot = constant_to_value_id.find_slot_with_hash (&vc, vc.hashcode, INSERT);
543 if (*slot)
544 return (*slot)->value_id;
546 vcp = XNEW (struct vn_constant_s);
547 vcp->hashcode = vc.hashcode;
548 vcp->constant = constant;
549 vcp->value_id = get_next_value_id ();
550 *slot = vcp;
551 bitmap_set_bit (constant_value_ids, vcp->value_id);
552 return vcp->value_id;
555 /* Return true if V is a value id for a constant. */
557 bool
558 value_id_constant_p (unsigned int v)
560 return bitmap_bit_p (constant_value_ids, v);
563 /* Compute the hash for a reference operand VRO1. */
565 static hashval_t
566 vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
568 result = iterative_hash_hashval_t (vro1->opcode, result);
569 if (vro1->op0)
570 result = iterative_hash_expr (vro1->op0, result);
571 if (vro1->op1)
572 result = iterative_hash_expr (vro1->op1, result);
573 if (vro1->op2)
574 result = iterative_hash_expr (vro1->op2, result);
575 return result;
578 /* Compute a hash for the reference operation VR1 and return it. */
580 hashval_t
581 vn_reference_compute_hash (const vn_reference_t vr1)
583 hashval_t result = 0;
584 int i;
585 vn_reference_op_t vro;
586 HOST_WIDE_INT off = -1;
587 bool deref = false;
589 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
591 if (vro->opcode == MEM_REF)
592 deref = true;
593 else if (vro->opcode != ADDR_EXPR)
594 deref = false;
595 if (vro->off != -1)
597 if (off == -1)
598 off = 0;
599 off += vro->off;
601 else
603 if (off != -1
604 && off != 0)
605 result = iterative_hash_hashval_t (off, result);
606 off = -1;
607 if (deref
608 && vro->opcode == ADDR_EXPR)
610 if (vro->op0)
612 tree op = TREE_OPERAND (vro->op0, 0);
613 result = iterative_hash_hashval_t (TREE_CODE (op), result);
614 result = iterative_hash_expr (op, result);
617 else
618 result = vn_reference_op_compute_hash (vro, result);
621 if (vr1->vuse)
622 result += SSA_NAME_VERSION (vr1->vuse);
624 return result;
627 /* Return true if reference operations VR1 and VR2 are equivalent. This
628 means they have the same set of operands and vuses. */
630 bool
631 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
633 unsigned i, j;
635 if (vr1->hashcode != vr2->hashcode)
636 return false;
638 /* Early out if this is not a hash collision. */
639 if (vr1->hashcode != vr2->hashcode)
640 return false;
642 /* The VOP needs to be the same. */
643 if (vr1->vuse != vr2->vuse)
644 return false;
646 /* If the operands are the same we are done. */
647 if (vr1->operands == vr2->operands)
648 return true;
650 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
651 return false;
653 if (INTEGRAL_TYPE_P (vr1->type)
654 && INTEGRAL_TYPE_P (vr2->type))
656 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
657 return false;
659 else if (INTEGRAL_TYPE_P (vr1->type)
660 && (TYPE_PRECISION (vr1->type)
661 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
662 return false;
663 else if (INTEGRAL_TYPE_P (vr2->type)
664 && (TYPE_PRECISION (vr2->type)
665 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
666 return false;
668 i = 0;
669 j = 0;
672 HOST_WIDE_INT off1 = 0, off2 = 0;
673 vn_reference_op_t vro1, vro2;
674 vn_reference_op_s tem1, tem2;
675 bool deref1 = false, deref2 = false;
676 for (; vr1->operands.iterate (i, &vro1); i++)
678 if (vro1->opcode == MEM_REF)
679 deref1 = true;
680 if (vro1->off == -1)
681 break;
682 off1 += vro1->off;
684 for (; vr2->operands.iterate (j, &vro2); j++)
686 if (vro2->opcode == MEM_REF)
687 deref2 = true;
688 if (vro2->off == -1)
689 break;
690 off2 += vro2->off;
692 if (off1 != off2)
693 return false;
694 if (deref1 && vro1->opcode == ADDR_EXPR)
696 memset (&tem1, 0, sizeof (tem1));
697 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
698 tem1.type = TREE_TYPE (tem1.op0);
699 tem1.opcode = TREE_CODE (tem1.op0);
700 vro1 = &tem1;
701 deref1 = false;
703 if (deref2 && vro2->opcode == ADDR_EXPR)
705 memset (&tem2, 0, sizeof (tem2));
706 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
707 tem2.type = TREE_TYPE (tem2.op0);
708 tem2.opcode = TREE_CODE (tem2.op0);
709 vro2 = &tem2;
710 deref2 = false;
712 if (deref1 != deref2)
713 return false;
714 if (!vn_reference_op_eq (vro1, vro2))
715 return false;
716 ++j;
717 ++i;
719 while (vr1->operands.length () != i
720 || vr2->operands.length () != j);
722 return true;
725 /* Copy the operations present in load/store REF into RESULT, a vector of
726 vn_reference_op_s's. */
728 void
729 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
731 if (TREE_CODE (ref) == TARGET_MEM_REF)
733 vn_reference_op_s temp;
735 result->reserve (3);
737 memset (&temp, 0, sizeof (temp));
738 temp.type = TREE_TYPE (ref);
739 temp.opcode = TREE_CODE (ref);
740 temp.op0 = TMR_INDEX (ref);
741 temp.op1 = TMR_STEP (ref);
742 temp.op2 = TMR_OFFSET (ref);
743 temp.off = -1;
744 result->quick_push (temp);
746 memset (&temp, 0, sizeof (temp));
747 temp.type = NULL_TREE;
748 temp.opcode = ERROR_MARK;
749 temp.op0 = TMR_INDEX2 (ref);
750 temp.off = -1;
751 result->quick_push (temp);
753 memset (&temp, 0, sizeof (temp));
754 temp.type = NULL_TREE;
755 temp.opcode = TREE_CODE (TMR_BASE (ref));
756 temp.op0 = TMR_BASE (ref);
757 temp.off = -1;
758 result->quick_push (temp);
759 return;
762 /* For non-calls, store the information that makes up the address. */
764 while (ref)
766 vn_reference_op_s temp;
768 memset (&temp, 0, sizeof (temp));
769 temp.type = TREE_TYPE (ref);
770 temp.opcode = TREE_CODE (ref);
771 temp.off = -1;
773 switch (temp.opcode)
775 case MODIFY_EXPR:
776 temp.op0 = TREE_OPERAND (ref, 1);
777 break;
778 case WITH_SIZE_EXPR:
779 temp.op0 = TREE_OPERAND (ref, 1);
780 temp.off = 0;
781 break;
782 case MEM_REF:
783 /* The base address gets its own vn_reference_op_s structure. */
784 temp.op0 = TREE_OPERAND (ref, 1);
785 if (host_integerp (TREE_OPERAND (ref, 1), 0))
786 temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
787 break;
788 case BIT_FIELD_REF:
789 /* Record bits and position. */
790 temp.op0 = TREE_OPERAND (ref, 1);
791 temp.op1 = TREE_OPERAND (ref, 2);
792 break;
793 case COMPONENT_REF:
794 /* The field decl is enough to unambiguously specify the field,
795 a matching type is not necessary and a mismatching type
796 is always a spurious difference. */
797 temp.type = NULL_TREE;
798 temp.op0 = TREE_OPERAND (ref, 1);
799 temp.op1 = TREE_OPERAND (ref, 2);
801 tree this_offset = component_ref_field_offset (ref);
802 if (this_offset
803 && TREE_CODE (this_offset) == INTEGER_CST)
805 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
806 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
808 double_int off
809 = tree_to_double_int (this_offset)
810 + tree_to_double_int (bit_offset)
811 .rshift (BITS_PER_UNIT == 8
812 ? 3 : exact_log2 (BITS_PER_UNIT));
813 if (off.fits_shwi ())
814 temp.off = off.low;
818 break;
819 case ARRAY_RANGE_REF:
820 case ARRAY_REF:
821 /* Record index as operand. */
822 temp.op0 = TREE_OPERAND (ref, 1);
823 /* Always record lower bounds and element size. */
824 temp.op1 = array_ref_low_bound (ref);
825 temp.op2 = array_ref_element_size (ref);
826 if (TREE_CODE (temp.op0) == INTEGER_CST
827 && TREE_CODE (temp.op1) == INTEGER_CST
828 && TREE_CODE (temp.op2) == INTEGER_CST)
830 double_int off = tree_to_double_int (temp.op0);
831 off += -tree_to_double_int (temp.op1);
832 off *= tree_to_double_int (temp.op2);
833 if (off.fits_shwi ())
834 temp.off = off.low;
836 break;
837 case VAR_DECL:
838 if (DECL_HARD_REGISTER (ref))
840 temp.op0 = ref;
841 break;
843 /* Fallthru. */
844 case PARM_DECL:
845 case CONST_DECL:
846 case RESULT_DECL:
847 /* Canonicalize decls to MEM[&decl] which is what we end up with
848 when valueizing MEM[ptr] with ptr = &decl. */
849 temp.opcode = MEM_REF;
850 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
851 temp.off = 0;
852 result->safe_push (temp);
853 temp.opcode = ADDR_EXPR;
854 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
855 temp.type = TREE_TYPE (temp.op0);
856 temp.off = -1;
857 break;
858 case STRING_CST:
859 case INTEGER_CST:
860 case COMPLEX_CST:
861 case VECTOR_CST:
862 case REAL_CST:
863 case FIXED_CST:
864 case CONSTRUCTOR:
865 case SSA_NAME:
866 temp.op0 = ref;
867 break;
868 case ADDR_EXPR:
869 if (is_gimple_min_invariant (ref))
871 temp.op0 = ref;
872 break;
874 /* Fallthrough. */
875 /* These are only interesting for their operands, their
876 existence, and their type. They will never be the last
877 ref in the chain of references (IE they require an
878 operand), so we don't have to put anything
879 for op* as it will be handled by the iteration */
880 case REALPART_EXPR:
881 case VIEW_CONVERT_EXPR:
882 temp.off = 0;
883 break;
884 case IMAGPART_EXPR:
885 /* This is only interesting for its constant offset. */
886 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
887 break;
888 default:
889 gcc_unreachable ();
891 result->safe_push (temp);
893 if (REFERENCE_CLASS_P (ref)
894 || TREE_CODE (ref) == MODIFY_EXPR
895 || TREE_CODE (ref) == WITH_SIZE_EXPR
896 || (TREE_CODE (ref) == ADDR_EXPR
897 && !is_gimple_min_invariant (ref)))
898 ref = TREE_OPERAND (ref, 0);
899 else
900 ref = NULL_TREE;
904 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
905 operands in *OPS, the reference alias set SET and the reference type TYPE.
906 Return true if something useful was produced. */
908 bool
909 ao_ref_init_from_vn_reference (ao_ref *ref,
910 alias_set_type set, tree type,
911 vec<vn_reference_op_s> ops)
913 vn_reference_op_t op;
914 unsigned i;
915 tree base = NULL_TREE;
916 tree *op0_p = &base;
917 HOST_WIDE_INT offset = 0;
918 HOST_WIDE_INT max_size;
919 HOST_WIDE_INT size = -1;
920 tree size_tree = NULL_TREE;
921 alias_set_type base_alias_set = -1;
923 /* First get the final access size from just the outermost expression. */
924 op = &ops[0];
925 if (op->opcode == COMPONENT_REF)
926 size_tree = DECL_SIZE (op->op0);
927 else if (op->opcode == BIT_FIELD_REF)
928 size_tree = op->op0;
929 else
931 enum machine_mode mode = TYPE_MODE (type);
932 if (mode == BLKmode)
933 size_tree = TYPE_SIZE (type);
934 else
935 size = GET_MODE_BITSIZE (mode);
937 if (size_tree != NULL_TREE)
939 if (!host_integerp (size_tree, 1))
940 size = -1;
941 else
942 size = TREE_INT_CST_LOW (size_tree);
945 /* Initially, maxsize is the same as the accessed element size.
946 In the following it will only grow (or become -1). */
947 max_size = size;
949 /* Compute cumulative bit-offset for nested component-refs and array-refs,
950 and find the ultimate containing object. */
951 FOR_EACH_VEC_ELT (ops, i, op)
953 switch (op->opcode)
955 /* These may be in the reference ops, but we cannot do anything
956 sensible with them here. */
957 case ADDR_EXPR:
958 /* Apart from ADDR_EXPR arguments to MEM_REF. */
959 if (base != NULL_TREE
960 && TREE_CODE (base) == MEM_REF
961 && op->op0
962 && DECL_P (TREE_OPERAND (op->op0, 0)))
964 vn_reference_op_t pop = &ops[i-1];
965 base = TREE_OPERAND (op->op0, 0);
966 if (pop->off == -1)
968 max_size = -1;
969 offset = 0;
971 else
972 offset += pop->off * BITS_PER_UNIT;
973 op0_p = NULL;
974 break;
976 /* Fallthru. */
977 case CALL_EXPR:
978 return false;
980 /* Record the base objects. */
981 case MEM_REF:
982 base_alias_set = get_deref_alias_set (op->op0);
983 *op0_p = build2 (MEM_REF, op->type,
984 NULL_TREE, op->op0);
985 op0_p = &TREE_OPERAND (*op0_p, 0);
986 break;
988 case VAR_DECL:
989 case PARM_DECL:
990 case RESULT_DECL:
991 case SSA_NAME:
992 *op0_p = op->op0;
993 op0_p = NULL;
994 break;
996 /* And now the usual component-reference style ops. */
997 case BIT_FIELD_REF:
998 offset += tree_low_cst (op->op1, 0);
999 break;
1001 case COMPONENT_REF:
1003 tree field = op->op0;
1004 /* We do not have a complete COMPONENT_REF tree here so we
1005 cannot use component_ref_field_offset. Do the interesting
1006 parts manually. */
1008 if (op->op1
1009 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
1010 max_size = -1;
1011 else
1013 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1014 * BITS_PER_UNIT);
1015 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
1017 break;
1020 case ARRAY_RANGE_REF:
1021 case ARRAY_REF:
1022 /* We recorded the lower bound and the element size. */
1023 if (!host_integerp (op->op0, 0)
1024 || !host_integerp (op->op1, 0)
1025 || !host_integerp (op->op2, 0))
1026 max_size = -1;
1027 else
1029 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
1030 hindex -= TREE_INT_CST_LOW (op->op1);
1031 hindex *= TREE_INT_CST_LOW (op->op2);
1032 hindex *= BITS_PER_UNIT;
1033 offset += hindex;
1035 break;
1037 case REALPART_EXPR:
1038 break;
1040 case IMAGPART_EXPR:
1041 offset += size;
1042 break;
1044 case VIEW_CONVERT_EXPR:
1045 break;
1047 case STRING_CST:
1048 case INTEGER_CST:
1049 case COMPLEX_CST:
1050 case VECTOR_CST:
1051 case REAL_CST:
1052 case CONSTRUCTOR:
1053 case CONST_DECL:
1054 return false;
1056 default:
1057 return false;
1061 if (base == NULL_TREE)
1062 return false;
1064 ref->ref = NULL_TREE;
1065 ref->base = base;
1066 ref->offset = offset;
1067 ref->size = size;
1068 ref->max_size = max_size;
1069 ref->ref_alias_set = set;
1070 if (base_alias_set != -1)
1071 ref->base_alias_set = base_alias_set;
1072 else
1073 ref->base_alias_set = get_alias_set (base);
1074 /* We discount volatiles from value-numbering elsewhere. */
1075 ref->volatile_p = false;
1077 return true;
1080 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1081 vn_reference_op_s's. */
1083 void
1084 copy_reference_ops_from_call (gimple call,
1085 vec<vn_reference_op_s> *result)
1087 vn_reference_op_s temp;
1088 unsigned i;
1089 tree lhs = gimple_call_lhs (call);
1091 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1092 different. By adding the lhs here in the vector, we ensure that the
1093 hashcode is different, guaranteeing a different value number. */
1094 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1096 memset (&temp, 0, sizeof (temp));
1097 temp.opcode = MODIFY_EXPR;
1098 temp.type = TREE_TYPE (lhs);
1099 temp.op0 = lhs;
1100 temp.off = -1;
1101 result->safe_push (temp);
1104 /* Copy the type, opcode, function being called and static chain. */
1105 memset (&temp, 0, sizeof (temp));
1106 temp.type = gimple_call_return_type (call);
1107 temp.opcode = CALL_EXPR;
1108 temp.op0 = gimple_call_fn (call);
1109 temp.op1 = gimple_call_chain (call);
1110 temp.off = -1;
1111 result->safe_push (temp);
1113 /* Copy the call arguments. As they can be references as well,
1114 just chain them together. */
1115 for (i = 0; i < gimple_call_num_args (call); ++i)
1117 tree callarg = gimple_call_arg (call, i);
1118 copy_reference_ops_from_ref (callarg, result);
1122 /* Create a vector of vn_reference_op_s structures from CALL, a
1123 call statement. The vector is not shared. */
1125 static vec<vn_reference_op_s>
1126 create_reference_ops_from_call (gimple call)
1128 vec<vn_reference_op_s> result = vNULL;
1130 copy_reference_ops_from_call (call, &result);
1131 return result;
1134 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1135 *I_P to point to the last element of the replacement. */
1136 void
1137 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1138 unsigned int *i_p)
1140 unsigned int i = *i_p;
1141 vn_reference_op_t op = &(*ops)[i];
1142 vn_reference_op_t mem_op = &(*ops)[i - 1];
1143 tree addr_base;
1144 HOST_WIDE_INT addr_offset = 0;
1146 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1147 from .foo.bar to the preceding MEM_REF offset and replace the
1148 address with &OBJ. */
1149 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1150 &addr_offset);
1151 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1152 if (addr_base != TREE_OPERAND (op->op0, 0))
1154 double_int off = tree_to_double_int (mem_op->op0);
1155 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1156 off += double_int::from_shwi (addr_offset);
1157 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1158 op->op0 = build_fold_addr_expr (addr_base);
1159 if (host_integerp (mem_op->op0, 0))
1160 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1161 else
1162 mem_op->off = -1;
1166 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1167 *I_P to point to the last element of the replacement. */
1168 static void
1169 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1170 unsigned int *i_p)
1172 unsigned int i = *i_p;
1173 vn_reference_op_t op = &(*ops)[i];
1174 vn_reference_op_t mem_op = &(*ops)[i - 1];
1175 gimple def_stmt;
1176 enum tree_code code;
1177 double_int off;
1179 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1180 if (!is_gimple_assign (def_stmt))
1181 return;
1183 code = gimple_assign_rhs_code (def_stmt);
1184 if (code != ADDR_EXPR
1185 && code != POINTER_PLUS_EXPR)
1186 return;
1188 off = tree_to_double_int (mem_op->op0);
1189 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1191 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1192 from .foo.bar to the preceding MEM_REF offset and replace the
1193 address with &OBJ. */
1194 if (code == ADDR_EXPR)
1196 tree addr, addr_base;
1197 HOST_WIDE_INT addr_offset;
1199 addr = gimple_assign_rhs1 (def_stmt);
1200 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1201 &addr_offset);
1202 if (!addr_base
1203 || TREE_CODE (addr_base) != MEM_REF)
1204 return;
1206 off += double_int::from_shwi (addr_offset);
1207 off += mem_ref_offset (addr_base);
1208 op->op0 = TREE_OPERAND (addr_base, 0);
1210 else
1212 tree ptr, ptroff;
1213 ptr = gimple_assign_rhs1 (def_stmt);
1214 ptroff = gimple_assign_rhs2 (def_stmt);
1215 if (TREE_CODE (ptr) != SSA_NAME
1216 || TREE_CODE (ptroff) != INTEGER_CST)
1217 return;
1219 off += tree_to_double_int (ptroff);
1220 op->op0 = ptr;
1223 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1224 if (host_integerp (mem_op->op0, 0))
1225 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1226 else
1227 mem_op->off = -1;
1228 if (TREE_CODE (op->op0) == SSA_NAME)
1229 op->op0 = SSA_VAL (op->op0);
1230 if (TREE_CODE (op->op0) != SSA_NAME)
1231 op->opcode = TREE_CODE (op->op0);
1233 /* And recurse. */
1234 if (TREE_CODE (op->op0) == SSA_NAME)
1235 vn_reference_maybe_forwprop_address (ops, i_p);
1236 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1237 vn_reference_fold_indirect (ops, i_p);
1240 /* Optimize the reference REF to a constant if possible or return
1241 NULL_TREE if not. */
1243 tree
1244 fully_constant_vn_reference_p (vn_reference_t ref)
1246 vec<vn_reference_op_s> operands = ref->operands;
1247 vn_reference_op_t op;
1249 /* Try to simplify the translated expression if it is
1250 a call to a builtin function with at most two arguments. */
1251 op = &operands[0];
1252 if (op->opcode == CALL_EXPR
1253 && TREE_CODE (op->op0) == ADDR_EXPR
1254 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1255 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1256 && operands.length () >= 2
1257 && operands.length () <= 3)
1259 vn_reference_op_t arg0, arg1 = NULL;
1260 bool anyconst = false;
1261 arg0 = &operands[1];
1262 if (operands.length () > 2)
1263 arg1 = &operands[2];
1264 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1265 || (arg0->opcode == ADDR_EXPR
1266 && is_gimple_min_invariant (arg0->op0)))
1267 anyconst = true;
1268 if (arg1
1269 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1270 || (arg1->opcode == ADDR_EXPR
1271 && is_gimple_min_invariant (arg1->op0))))
1272 anyconst = true;
1273 if (anyconst)
1275 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1276 arg1 ? 2 : 1,
1277 arg0->op0,
1278 arg1 ? arg1->op0 : NULL);
1279 if (folded
1280 && TREE_CODE (folded) == NOP_EXPR)
1281 folded = TREE_OPERAND (folded, 0);
1282 if (folded
1283 && is_gimple_min_invariant (folded))
1284 return folded;
1288 /* Simplify reads from constant strings. */
1289 else if (op->opcode == ARRAY_REF
1290 && TREE_CODE (op->op0) == INTEGER_CST
1291 && integer_zerop (op->op1)
1292 && operands.length () == 2)
1294 vn_reference_op_t arg0;
1295 arg0 = &operands[1];
1296 if (arg0->opcode == STRING_CST
1297 && (TYPE_MODE (op->type)
1298 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1299 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1300 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1301 && tree_int_cst_sgn (op->op0) >= 0
1302 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1303 return build_int_cst_type (op->type,
1304 (TREE_STRING_POINTER (arg0->op0)
1305 [TREE_INT_CST_LOW (op->op0)]));
1308 return NULL_TREE;
1311 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1312 structures into their value numbers. This is done in-place, and
1313 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1314 whether any operands were valueized. */
1316 static vec<vn_reference_op_s>
1317 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1319 vn_reference_op_t vro;
1320 unsigned int i;
1322 *valueized_anything = false;
1324 FOR_EACH_VEC_ELT (orig, i, vro)
1326 if (vro->opcode == SSA_NAME
1327 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1329 tree tem = SSA_VAL (vro->op0);
1330 if (tem != vro->op0)
1332 *valueized_anything = true;
1333 vro->op0 = tem;
1335 /* If it transforms from an SSA_NAME to a constant, update
1336 the opcode. */
1337 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1338 vro->opcode = TREE_CODE (vro->op0);
1340 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1342 tree tem = SSA_VAL (vro->op1);
1343 if (tem != vro->op1)
1345 *valueized_anything = true;
1346 vro->op1 = tem;
1349 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1351 tree tem = SSA_VAL (vro->op2);
1352 if (tem != vro->op2)
1354 *valueized_anything = true;
1355 vro->op2 = tem;
1358 /* If it transforms from an SSA_NAME to an address, fold with
1359 a preceding indirect reference. */
1360 if (i > 0
1361 && vro->op0
1362 && TREE_CODE (vro->op0) == ADDR_EXPR
1363 && orig[i - 1].opcode == MEM_REF)
1364 vn_reference_fold_indirect (&orig, &i);
1365 else if (i > 0
1366 && vro->opcode == SSA_NAME
1367 && orig[i - 1].opcode == MEM_REF)
1368 vn_reference_maybe_forwprop_address (&orig, &i);
1369 /* If it transforms a non-constant ARRAY_REF into a constant
1370 one, adjust the constant offset. */
1371 else if (vro->opcode == ARRAY_REF
1372 && vro->off == -1
1373 && TREE_CODE (vro->op0) == INTEGER_CST
1374 && TREE_CODE (vro->op1) == INTEGER_CST
1375 && TREE_CODE (vro->op2) == INTEGER_CST)
1377 double_int off = tree_to_double_int (vro->op0);
1378 off += -tree_to_double_int (vro->op1);
1379 off *= tree_to_double_int (vro->op2);
1380 if (off.fits_shwi ())
1381 vro->off = off.low;
1385 return orig;
1388 static vec<vn_reference_op_s>
1389 valueize_refs (vec<vn_reference_op_s> orig)
1391 bool tem;
1392 return valueize_refs_1 (orig, &tem);
1395 static vec<vn_reference_op_s> shared_lookup_references;
1397 /* Create a vector of vn_reference_op_s structures from REF, a
1398 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1399 this function. *VALUEIZED_ANYTHING will specify whether any
1400 operands were valueized. */
1402 static vec<vn_reference_op_s>
1403 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1405 if (!ref)
1406 return vNULL;
1407 shared_lookup_references.truncate (0);
1408 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1409 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1410 valueized_anything);
1411 return shared_lookup_references;
1414 /* Create a vector of vn_reference_op_s structures from CALL, a
1415 call statement. The vector is shared among all callers of
1416 this function. */
1418 static vec<vn_reference_op_s>
1419 valueize_shared_reference_ops_from_call (gimple call)
1421 if (!call)
1422 return vNULL;
1423 shared_lookup_references.truncate (0);
1424 copy_reference_ops_from_call (call, &shared_lookup_references);
1425 shared_lookup_references = valueize_refs (shared_lookup_references);
1426 return shared_lookup_references;
1429 /* Lookup a SCCVN reference operation VR in the current hash table.
1430 Returns the resulting value number if it exists in the hash table,
1431 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1432 vn_reference_t stored in the hashtable if something is found. */
1434 static tree
1435 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1437 vn_reference_s **slot;
1438 hashval_t hash;
1440 hash = vr->hashcode;
1441 slot = current_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1442 if (!slot && current_info == optimistic_info)
1443 slot = valid_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1444 if (slot)
1446 if (vnresult)
1447 *vnresult = (vn_reference_t)*slot;
1448 return ((vn_reference_t)*slot)->result;
1451 return NULL_TREE;
1454 static tree *last_vuse_ptr;
1455 static vn_lookup_kind vn_walk_kind;
1456 static vn_lookup_kind default_vn_walk_kind;
1458 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1459 with the current VUSE and performs the expression lookup. */
1461 static void *
1462 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1463 unsigned int cnt, void *vr_)
1465 vn_reference_t vr = (vn_reference_t)vr_;
1466 vn_reference_s **slot;
1467 hashval_t hash;
1469 /* This bounds the stmt walks we perform on reference lookups
1470 to O(1) instead of O(N) where N is the number of dominating
1471 stores. */
1472 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1473 return (void *)-1;
1475 if (last_vuse_ptr)
1476 *last_vuse_ptr = vuse;
1478 /* Fixup vuse and hash. */
1479 if (vr->vuse)
1480 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1481 vr->vuse = SSA_VAL (vuse);
1482 if (vr->vuse)
1483 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1485 hash = vr->hashcode;
1486 slot = current_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1487 if (!slot && current_info == optimistic_info)
1488 slot = valid_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1489 if (slot)
1490 return *slot;
1492 return NULL;
1495 /* Lookup an existing or insert a new vn_reference entry into the
1496 value table for the VUSE, SET, TYPE, OPERANDS reference which
1497 has the value VALUE which is either a constant or an SSA name. */
1499 static vn_reference_t
1500 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1501 alias_set_type set,
1502 tree type,
1503 vec<vn_reference_op_s,
1504 va_heap> operands,
1505 tree value)
1507 struct vn_reference_s vr1;
1508 vn_reference_t result;
1509 unsigned value_id;
1510 vr1.vuse = vuse;
1511 vr1.operands = operands;
1512 vr1.type = type;
1513 vr1.set = set;
1514 vr1.hashcode = vn_reference_compute_hash (&vr1);
1515 if (vn_reference_lookup_1 (&vr1, &result))
1516 return result;
1517 if (TREE_CODE (value) == SSA_NAME)
1518 value_id = VN_INFO (value)->value_id;
1519 else
1520 value_id = get_or_alloc_constant_value_id (value);
1521 return vn_reference_insert_pieces (vuse, set, type,
1522 operands.copy (), value, value_id);
1525 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1526 from the statement defining VUSE and if not successful tries to
1527 translate *REFP and VR_ through an aggregate copy at the definition
1528 of VUSE. */
1530 static void *
1531 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1533 vn_reference_t vr = (vn_reference_t)vr_;
1534 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1535 tree base;
1536 HOST_WIDE_INT offset, maxsize;
1537 static vec<vn_reference_op_s>
1538 lhs_ops = vNULL;
1539 ao_ref lhs_ref;
1540 bool lhs_ref_ok = false;
1542 /* First try to disambiguate after value-replacing in the definitions LHS. */
1543 if (is_gimple_assign (def_stmt))
1545 vec<vn_reference_op_s> tem;
1546 tree lhs = gimple_assign_lhs (def_stmt);
1547 bool valueized_anything = false;
1548 /* Avoid re-allocation overhead. */
1549 lhs_ops.truncate (0);
1550 copy_reference_ops_from_ref (lhs, &lhs_ops);
1551 tem = lhs_ops;
1552 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1553 gcc_assert (lhs_ops == tem);
1554 if (valueized_anything)
1556 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1557 get_alias_set (lhs),
1558 TREE_TYPE (lhs), lhs_ops);
1559 if (lhs_ref_ok
1560 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1561 return NULL;
1563 else
1565 ao_ref_init (&lhs_ref, lhs);
1566 lhs_ref_ok = true;
1570 base = ao_ref_base (ref);
1571 offset = ref->offset;
1572 maxsize = ref->max_size;
1574 /* If we cannot constrain the size of the reference we cannot
1575 test if anything kills it. */
1576 if (maxsize == -1)
1577 return (void *)-1;
1579 /* We can't deduce anything useful from clobbers. */
1580 if (gimple_clobber_p (def_stmt))
1581 return (void *)-1;
1583 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1584 from that definition.
1585 1) Memset. */
1586 if (is_gimple_reg_type (vr->type)
1587 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1588 && integer_zerop (gimple_call_arg (def_stmt, 1))
1589 && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1590 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1592 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1593 tree base2;
1594 HOST_WIDE_INT offset2, size2, maxsize2;
1595 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1596 size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1597 if ((unsigned HOST_WIDE_INT)size2 / 8
1598 == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1599 && maxsize2 != -1
1600 && operand_equal_p (base, base2, 0)
1601 && offset2 <= offset
1602 && offset2 + size2 >= offset + maxsize)
1604 tree val = build_zero_cst (vr->type);
1605 return vn_reference_lookup_or_insert_for_pieces
1606 (vuse, vr->set, vr->type, vr->operands, val);
1610 /* 2) Assignment from an empty CONSTRUCTOR. */
1611 else if (is_gimple_reg_type (vr->type)
1612 && gimple_assign_single_p (def_stmt)
1613 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1614 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1616 tree base2;
1617 HOST_WIDE_INT offset2, size2, maxsize2;
1618 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1619 &offset2, &size2, &maxsize2);
1620 if (maxsize2 != -1
1621 && operand_equal_p (base, base2, 0)
1622 && offset2 <= offset
1623 && offset2 + size2 >= offset + maxsize)
1625 tree val = build_zero_cst (vr->type);
1626 return vn_reference_lookup_or_insert_for_pieces
1627 (vuse, vr->set, vr->type, vr->operands, val);
1631 /* 3) Assignment from a constant. We can use folds native encode/interpret
1632 routines to extract the assigned bits. */
1633 else if (vn_walk_kind == VN_WALKREWRITE
1634 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1635 && ref->size == maxsize
1636 && maxsize % BITS_PER_UNIT == 0
1637 && offset % BITS_PER_UNIT == 0
1638 && is_gimple_reg_type (vr->type)
1639 && gimple_assign_single_p (def_stmt)
1640 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1642 tree base2;
1643 HOST_WIDE_INT offset2, size2, maxsize2;
1644 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1645 &offset2, &size2, &maxsize2);
1646 if (maxsize2 != -1
1647 && maxsize2 == size2
1648 && size2 % BITS_PER_UNIT == 0
1649 && offset2 % BITS_PER_UNIT == 0
1650 && operand_equal_p (base, base2, 0)
1651 && offset2 <= offset
1652 && offset2 + size2 >= offset + maxsize)
1654 /* We support up to 512-bit values (for V8DFmode). */
1655 unsigned char buffer[64];
1656 int len;
1658 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1659 buffer, sizeof (buffer));
1660 if (len > 0)
1662 tree val = native_interpret_expr (vr->type,
1663 buffer
1664 + ((offset - offset2)
1665 / BITS_PER_UNIT),
1666 ref->size / BITS_PER_UNIT);
1667 if (val)
1668 return vn_reference_lookup_or_insert_for_pieces
1669 (vuse, vr->set, vr->type, vr->operands, val);
1674 /* 4) Assignment from an SSA name which definition we may be able
1675 to access pieces from. */
1676 else if (ref->size == maxsize
1677 && is_gimple_reg_type (vr->type)
1678 && gimple_assign_single_p (def_stmt)
1679 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1681 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1682 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1683 if (is_gimple_assign (def_stmt2)
1684 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1685 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1686 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1688 tree base2;
1689 HOST_WIDE_INT offset2, size2, maxsize2, off;
1690 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1691 &offset2, &size2, &maxsize2);
1692 off = offset - offset2;
1693 if (maxsize2 != -1
1694 && maxsize2 == size2
1695 && operand_equal_p (base, base2, 0)
1696 && offset2 <= offset
1697 && offset2 + size2 >= offset + maxsize)
1699 tree val = NULL_TREE;
1700 HOST_WIDE_INT elsz
1701 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1702 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1704 if (off == 0)
1705 val = gimple_assign_rhs1 (def_stmt2);
1706 else if (off == elsz)
1707 val = gimple_assign_rhs2 (def_stmt2);
1709 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1710 && off % elsz == 0)
1712 tree ctor = gimple_assign_rhs1 (def_stmt2);
1713 unsigned i = off / elsz;
1714 if (i < CONSTRUCTOR_NELTS (ctor))
1716 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1717 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1719 if (TREE_CODE (TREE_TYPE (elt->value))
1720 != VECTOR_TYPE)
1721 val = elt->value;
1725 if (val)
1726 return vn_reference_lookup_or_insert_for_pieces
1727 (vuse, vr->set, vr->type, vr->operands, val);
1732 /* 5) For aggregate copies translate the reference through them if
1733 the copy kills ref. */
1734 else if (vn_walk_kind == VN_WALKREWRITE
1735 && gimple_assign_single_p (def_stmt)
1736 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1737 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1738 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1740 tree base2;
1741 HOST_WIDE_INT offset2, size2, maxsize2;
1742 int i, j;
1743 vec<vn_reference_op_s>
1744 rhs = vNULL;
1745 vn_reference_op_t vro;
1746 ao_ref r;
1748 if (!lhs_ref_ok)
1749 return (void *)-1;
1751 /* See if the assignment kills REF. */
1752 base2 = ao_ref_base (&lhs_ref);
1753 offset2 = lhs_ref.offset;
1754 size2 = lhs_ref.size;
1755 maxsize2 = lhs_ref.max_size;
1756 if (maxsize2 == -1
1757 || (base != base2 && !operand_equal_p (base, base2, 0))
1758 || offset2 > offset
1759 || offset2 + size2 < offset + maxsize)
1760 return (void *)-1;
1762 /* Find the common base of ref and the lhs. lhs_ops already
1763 contains valueized operands for the lhs. */
1764 i = vr->operands.length () - 1;
1765 j = lhs_ops.length () - 1;
1766 while (j >= 0 && i >= 0
1767 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1769 i--;
1770 j--;
1773 /* ??? The innermost op should always be a MEM_REF and we already
1774 checked that the assignment to the lhs kills vr. Thus for
1775 aggregate copies using char[] types the vn_reference_op_eq
1776 may fail when comparing types for compatibility. But we really
1777 don't care here - further lookups with the rewritten operands
1778 will simply fail if we messed up types too badly. */
1779 if (j == 0 && i >= 0
1780 && lhs_ops[0].opcode == MEM_REF
1781 && lhs_ops[0].off != -1
1782 && (lhs_ops[0].off == vr->operands[i].off))
1783 i--, j--;
1785 /* i now points to the first additional op.
1786 ??? LHS may not be completely contained in VR, one or more
1787 VIEW_CONVERT_EXPRs could be in its way. We could at least
1788 try handling outermost VIEW_CONVERT_EXPRs. */
1789 if (j != -1)
1790 return (void *)-1;
1792 /* Now re-write REF to be based on the rhs of the assignment. */
1793 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1794 /* We need to pre-pend vr->operands[0..i] to rhs. */
1795 if (i + 1 + rhs.length () > vr->operands.length ())
1797 vec<vn_reference_op_s> old = vr->operands;
1798 vr->operands.safe_grow (i + 1 + rhs.length ());
1799 if (old == shared_lookup_references
1800 && vr->operands != old)
1801 shared_lookup_references = vNULL;
1803 else
1804 vr->operands.truncate (i + 1 + rhs.length ());
1805 FOR_EACH_VEC_ELT (rhs, j, vro)
1806 vr->operands[i + 1 + j] = *vro;
1807 rhs.release ();
1808 vr->operands = valueize_refs (vr->operands);
1809 vr->hashcode = vn_reference_compute_hash (vr);
1811 /* Adjust *ref from the new operands. */
1812 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1813 return (void *)-1;
1814 /* This can happen with bitfields. */
1815 if (ref->size != r.size)
1816 return (void *)-1;
1817 *ref = r;
1819 /* Do not update last seen VUSE after translating. */
1820 last_vuse_ptr = NULL;
1822 /* Keep looking for the adjusted *REF / VR pair. */
1823 return NULL;
1826 /* 6) For memcpy copies translate the reference through them if
1827 the copy kills ref. */
1828 else if (vn_walk_kind == VN_WALKREWRITE
1829 && is_gimple_reg_type (vr->type)
1830 /* ??? Handle BCOPY as well. */
1831 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1832 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1833 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1834 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1835 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1836 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1837 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1838 && host_integerp (gimple_call_arg (def_stmt, 2), 1))
1840 tree lhs, rhs;
1841 ao_ref r;
1842 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1843 vn_reference_op_s op;
1844 HOST_WIDE_INT at;
1847 /* Only handle non-variable, addressable refs. */
1848 if (ref->size != maxsize
1849 || offset % BITS_PER_UNIT != 0
1850 || ref->size % BITS_PER_UNIT != 0)
1851 return (void *)-1;
1853 /* Extract a pointer base and an offset for the destination. */
1854 lhs = gimple_call_arg (def_stmt, 0);
1855 lhs_offset = 0;
1856 if (TREE_CODE (lhs) == SSA_NAME)
1857 lhs = SSA_VAL (lhs);
1858 if (TREE_CODE (lhs) == ADDR_EXPR)
1860 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1861 &lhs_offset);
1862 if (!tem)
1863 return (void *)-1;
1864 if (TREE_CODE (tem) == MEM_REF
1865 && host_integerp (TREE_OPERAND (tem, 1), 1))
1867 lhs = TREE_OPERAND (tem, 0);
1868 lhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1870 else if (DECL_P (tem))
1871 lhs = build_fold_addr_expr (tem);
1872 else
1873 return (void *)-1;
1875 if (TREE_CODE (lhs) != SSA_NAME
1876 && TREE_CODE (lhs) != ADDR_EXPR)
1877 return (void *)-1;
1879 /* Extract a pointer base and an offset for the source. */
1880 rhs = gimple_call_arg (def_stmt, 1);
1881 rhs_offset = 0;
1882 if (TREE_CODE (rhs) == SSA_NAME)
1883 rhs = SSA_VAL (rhs);
1884 if (TREE_CODE (rhs) == ADDR_EXPR)
1886 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1887 &rhs_offset);
1888 if (!tem)
1889 return (void *)-1;
1890 if (TREE_CODE (tem) == MEM_REF
1891 && host_integerp (TREE_OPERAND (tem, 1), 1))
1893 rhs = TREE_OPERAND (tem, 0);
1894 rhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1896 else if (DECL_P (tem))
1897 rhs = build_fold_addr_expr (tem);
1898 else
1899 return (void *)-1;
1901 if (TREE_CODE (rhs) != SSA_NAME
1902 && TREE_CODE (rhs) != ADDR_EXPR)
1903 return (void *)-1;
1905 copy_size = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2));
1907 /* The bases of the destination and the references have to agree. */
1908 if ((TREE_CODE (base) != MEM_REF
1909 && !DECL_P (base))
1910 || (TREE_CODE (base) == MEM_REF
1911 && (TREE_OPERAND (base, 0) != lhs
1912 || !host_integerp (TREE_OPERAND (base, 1), 1)))
1913 || (DECL_P (base)
1914 && (TREE_CODE (lhs) != ADDR_EXPR
1915 || TREE_OPERAND (lhs, 0) != base)))
1916 return (void *)-1;
1918 /* And the access has to be contained within the memcpy destination. */
1919 at = offset / BITS_PER_UNIT;
1920 if (TREE_CODE (base) == MEM_REF)
1921 at += TREE_INT_CST_LOW (TREE_OPERAND (base, 1));
1922 if (lhs_offset > at
1923 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1924 return (void *)-1;
1926 /* Make room for 2 operands in the new reference. */
1927 if (vr->operands.length () < 2)
1929 vec<vn_reference_op_s> old = vr->operands;
1930 vr->operands.safe_grow_cleared (2);
1931 if (old == shared_lookup_references
1932 && vr->operands != old)
1933 shared_lookup_references.create (0);
1935 else
1936 vr->operands.truncate (2);
1938 /* The looked-through reference is a simple MEM_REF. */
1939 memset (&op, 0, sizeof (op));
1940 op.type = vr->type;
1941 op.opcode = MEM_REF;
1942 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
1943 op.off = at - lhs_offset + rhs_offset;
1944 vr->operands[0] = op;
1945 op.type = TREE_TYPE (rhs);
1946 op.opcode = TREE_CODE (rhs);
1947 op.op0 = rhs;
1948 op.off = -1;
1949 vr->operands[1] = op;
1950 vr->hashcode = vn_reference_compute_hash (vr);
1952 /* Adjust *ref from the new operands. */
1953 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1954 return (void *)-1;
1955 /* This can happen with bitfields. */
1956 if (ref->size != r.size)
1957 return (void *)-1;
1958 *ref = r;
1960 /* Do not update last seen VUSE after translating. */
1961 last_vuse_ptr = NULL;
1963 /* Keep looking for the adjusted *REF / VR pair. */
1964 return NULL;
1967 /* Bail out and stop walking. */
1968 return (void *)-1;
1971 /* Lookup a reference operation by it's parts, in the current hash table.
1972 Returns the resulting value number if it exists in the hash table,
1973 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1974 vn_reference_t stored in the hashtable if something is found. */
1976 tree
1977 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1978 vec<vn_reference_op_s> operands,
1979 vn_reference_t *vnresult, vn_lookup_kind kind)
1981 struct vn_reference_s vr1;
1982 vn_reference_t tmp;
1983 tree cst;
1985 if (!vnresult)
1986 vnresult = &tmp;
1987 *vnresult = NULL;
1989 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1990 shared_lookup_references.truncate (0);
1991 shared_lookup_references.safe_grow (operands.length ());
1992 memcpy (shared_lookup_references.address (),
1993 operands.address (),
1994 sizeof (vn_reference_op_s)
1995 * operands.length ());
1996 vr1.operands = operands = shared_lookup_references
1997 = valueize_refs (shared_lookup_references);
1998 vr1.type = type;
1999 vr1.set = set;
2000 vr1.hashcode = vn_reference_compute_hash (&vr1);
2001 if ((cst = fully_constant_vn_reference_p (&vr1)))
2002 return cst;
2004 vn_reference_lookup_1 (&vr1, vnresult);
2005 if (!*vnresult
2006 && kind != VN_NOWALK
2007 && vr1.vuse)
2009 ao_ref r;
2010 vn_walk_kind = kind;
2011 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2012 *vnresult =
2013 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2014 vn_reference_lookup_2,
2015 vn_reference_lookup_3, &vr1);
2016 if (vr1.operands != operands)
2017 vr1.operands.release ();
2020 if (*vnresult)
2021 return (*vnresult)->result;
2023 return NULL_TREE;
2026 /* Lookup OP in the current hash table, and return the resulting value
2027 number if it exists in the hash table. Return NULL_TREE if it does
2028 not exist in the hash table or if the result field of the structure
2029 was NULL.. VNRESULT will be filled in with the vn_reference_t
2030 stored in the hashtable if one exists. */
2032 tree
2033 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2034 vn_reference_t *vnresult)
2036 vec<vn_reference_op_s> operands;
2037 struct vn_reference_s vr1;
2038 tree cst;
2039 bool valuezied_anything;
2041 if (vnresult)
2042 *vnresult = NULL;
2044 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2045 vr1.operands = operands
2046 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2047 vr1.type = TREE_TYPE (op);
2048 vr1.set = get_alias_set (op);
2049 vr1.hashcode = vn_reference_compute_hash (&vr1);
2050 if ((cst = fully_constant_vn_reference_p (&vr1)))
2051 return cst;
2053 if (kind != VN_NOWALK
2054 && vr1.vuse)
2056 vn_reference_t wvnresult;
2057 ao_ref r;
2058 /* Make sure to use a valueized reference if we valueized anything.
2059 Otherwise preserve the full reference for advanced TBAA. */
2060 if (!valuezied_anything
2061 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2062 vr1.operands))
2063 ao_ref_init (&r, op);
2064 vn_walk_kind = kind;
2065 wvnresult =
2066 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2067 vn_reference_lookup_2,
2068 vn_reference_lookup_3, &vr1);
2069 if (vr1.operands != operands)
2070 vr1.operands.release ();
2071 if (wvnresult)
2073 if (vnresult)
2074 *vnresult = wvnresult;
2075 return wvnresult->result;
2078 return NULL_TREE;
2081 return vn_reference_lookup_1 (&vr1, vnresult);
2085 /* Insert OP into the current hash table with a value number of
2086 RESULT, and return the resulting reference structure we created. */
2088 vn_reference_t
2089 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2091 vn_reference_s **slot;
2092 vn_reference_t vr1;
2093 bool tem;
2095 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2096 if (TREE_CODE (result) == SSA_NAME)
2097 vr1->value_id = VN_INFO (result)->value_id;
2098 else
2099 vr1->value_id = get_or_alloc_constant_value_id (result);
2100 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2101 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2102 vr1->type = TREE_TYPE (op);
2103 vr1->set = get_alias_set (op);
2104 vr1->hashcode = vn_reference_compute_hash (vr1);
2105 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2106 vr1->result_vdef = vdef;
2108 slot = current_info->references.find_slot_with_hash (vr1, vr1->hashcode,
2109 INSERT);
2111 /* Because we lookup stores using vuses, and value number failures
2112 using the vdefs (see visit_reference_op_store for how and why),
2113 it's possible that on failure we may try to insert an already
2114 inserted store. This is not wrong, there is no ssa name for a
2115 store that we could use as a differentiator anyway. Thus, unlike
2116 the other lookup functions, you cannot gcc_assert (!*slot)
2117 here. */
2119 /* But free the old slot in case of a collision. */
2120 if (*slot)
2121 free_reference (*slot);
2123 *slot = vr1;
2124 return vr1;
2127 /* Insert a reference by it's pieces into the current hash table with
2128 a value number of RESULT. Return the resulting reference
2129 structure we created. */
2131 vn_reference_t
2132 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2133 vec<vn_reference_op_s> operands,
2134 tree result, unsigned int value_id)
2137 vn_reference_s **slot;
2138 vn_reference_t vr1;
2140 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2141 vr1->value_id = value_id;
2142 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2143 vr1->operands = valueize_refs (operands);
2144 vr1->type = type;
2145 vr1->set = set;
2146 vr1->hashcode = vn_reference_compute_hash (vr1);
2147 if (result && TREE_CODE (result) == SSA_NAME)
2148 result = SSA_VAL (result);
2149 vr1->result = result;
2151 slot = current_info->references.find_slot_with_hash (vr1, vr1->hashcode,
2152 INSERT);
2154 /* At this point we should have all the things inserted that we have
2155 seen before, and we should never try inserting something that
2156 already exists. */
2157 gcc_assert (!*slot);
2158 if (*slot)
2159 free_reference (*slot);
2161 *slot = vr1;
2162 return vr1;
2165 /* Compute and return the hash value for nary operation VBO1. */
2167 hashval_t
2168 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2170 hashval_t hash;
2171 unsigned i;
2173 for (i = 0; i < vno1->length; ++i)
2174 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2175 vno1->op[i] = SSA_VAL (vno1->op[i]);
2177 if (vno1->length == 2
2178 && commutative_tree_code (vno1->opcode)
2179 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2181 tree temp = vno1->op[0];
2182 vno1->op[0] = vno1->op[1];
2183 vno1->op[1] = temp;
2186 hash = iterative_hash_hashval_t (vno1->opcode, 0);
2187 for (i = 0; i < vno1->length; ++i)
2188 hash = iterative_hash_expr (vno1->op[i], hash);
2190 return hash;
2193 /* Compare nary operations VNO1 and VNO2 and return true if they are
2194 equivalent. */
2196 bool
2197 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2199 unsigned i;
2201 if (vno1->hashcode != vno2->hashcode)
2202 return false;
2204 if (vno1->length != vno2->length)
2205 return false;
2207 if (vno1->opcode != vno2->opcode
2208 || !types_compatible_p (vno1->type, vno2->type))
2209 return false;
2211 for (i = 0; i < vno1->length; ++i)
2212 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2213 return false;
2215 return true;
2218 /* Initialize VNO from the pieces provided. */
2220 static void
2221 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2222 enum tree_code code, tree type, tree *ops)
2224 vno->opcode = code;
2225 vno->length = length;
2226 vno->type = type;
2227 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2230 /* Initialize VNO from OP. */
2232 static void
2233 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2235 unsigned i;
2237 vno->opcode = TREE_CODE (op);
2238 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2239 vno->type = TREE_TYPE (op);
2240 for (i = 0; i < vno->length; ++i)
2241 vno->op[i] = TREE_OPERAND (op, i);
2244 /* Return the number of operands for a vn_nary ops structure from STMT. */
2246 static unsigned int
2247 vn_nary_length_from_stmt (gimple stmt)
2249 switch (gimple_assign_rhs_code (stmt))
2251 case REALPART_EXPR:
2252 case IMAGPART_EXPR:
2253 case VIEW_CONVERT_EXPR:
2254 return 1;
2256 case BIT_FIELD_REF:
2257 return 3;
2259 case CONSTRUCTOR:
2260 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2262 default:
2263 return gimple_num_ops (stmt) - 1;
2267 /* Initialize VNO from STMT. */
2269 static void
2270 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2272 unsigned i;
2274 vno->opcode = gimple_assign_rhs_code (stmt);
2275 vno->type = gimple_expr_type (stmt);
2276 switch (vno->opcode)
2278 case REALPART_EXPR:
2279 case IMAGPART_EXPR:
2280 case VIEW_CONVERT_EXPR:
2281 vno->length = 1;
2282 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2283 break;
2285 case BIT_FIELD_REF:
2286 vno->length = 3;
2287 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2288 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2289 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2290 break;
2292 case CONSTRUCTOR:
2293 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2294 for (i = 0; i < vno->length; ++i)
2295 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2296 break;
2298 default:
2299 gcc_checking_assert (!gimple_assign_single_p (stmt));
2300 vno->length = gimple_num_ops (stmt) - 1;
2301 for (i = 0; i < vno->length; ++i)
2302 vno->op[i] = gimple_op (stmt, i + 1);
2306 /* Compute the hashcode for VNO and look for it in the hash table;
2307 return the resulting value number if it exists in the hash table.
2308 Return NULL_TREE if it does not exist in the hash table or if the
2309 result field of the operation is NULL. VNRESULT will contain the
2310 vn_nary_op_t from the hashtable if it exists. */
2312 static tree
2313 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2315 vn_nary_op_s **slot;
2317 if (vnresult)
2318 *vnresult = NULL;
2320 vno->hashcode = vn_nary_op_compute_hash (vno);
2321 slot = current_info->nary.find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
2322 if (!slot && current_info == optimistic_info)
2323 slot = valid_info->nary.find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
2324 if (!slot)
2325 return NULL_TREE;
2326 if (vnresult)
2327 *vnresult = *slot;
2328 return (*slot)->result;
2331 /* Lookup a n-ary operation by its pieces and return the resulting value
2332 number if it exists in the hash table. Return NULL_TREE if it does
2333 not exist in the hash table or if the result field of the operation
2334 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2335 if it exists. */
2337 tree
2338 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2339 tree type, tree *ops, vn_nary_op_t *vnresult)
2341 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2342 sizeof_vn_nary_op (length));
2343 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2344 return vn_nary_op_lookup_1 (vno1, vnresult);
2347 /* Lookup OP in the current hash table, and return the resulting value
2348 number if it exists in the hash table. Return NULL_TREE if it does
2349 not exist in the hash table or if the result field of the operation
2350 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2351 if it exists. */
2353 tree
2354 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2356 vn_nary_op_t vno1
2357 = XALLOCAVAR (struct vn_nary_op_s,
2358 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2359 init_vn_nary_op_from_op (vno1, op);
2360 return vn_nary_op_lookup_1 (vno1, vnresult);
2363 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2364 value number if it exists in the hash table. Return NULL_TREE if
2365 it does not exist in the hash table. VNRESULT will contain the
2366 vn_nary_op_t from the hashtable if it exists. */
2368 tree
2369 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2371 vn_nary_op_t vno1
2372 = XALLOCAVAR (struct vn_nary_op_s,
2373 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2374 init_vn_nary_op_from_stmt (vno1, stmt);
2375 return vn_nary_op_lookup_1 (vno1, vnresult);
2378 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2380 static vn_nary_op_t
2381 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2383 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2386 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2387 obstack. */
2389 static vn_nary_op_t
2390 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2392 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2393 &current_info->nary_obstack);
2395 vno1->value_id = value_id;
2396 vno1->length = length;
2397 vno1->result = result;
2399 return vno1;
2402 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2403 VNO->HASHCODE first. */
2405 static vn_nary_op_t
2406 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type table,
2407 bool compute_hash)
2409 vn_nary_op_s **slot;
2411 if (compute_hash)
2412 vno->hashcode = vn_nary_op_compute_hash (vno);
2414 slot = table.find_slot_with_hash (vno, vno->hashcode, INSERT);
2415 gcc_assert (!*slot);
2417 *slot = vno;
2418 return vno;
2421 /* Insert a n-ary operation into the current hash table using it's
2422 pieces. Return the vn_nary_op_t structure we created and put in
2423 the hashtable. */
2425 vn_nary_op_t
2426 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2427 tree type, tree *ops,
2428 tree result, unsigned int value_id)
2430 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2431 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2432 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2435 /* Insert OP into the current hash table with a value number of
2436 RESULT. Return the vn_nary_op_t structure we created and put in
2437 the hashtable. */
2439 vn_nary_op_t
2440 vn_nary_op_insert (tree op, tree result)
2442 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2443 vn_nary_op_t vno1;
2445 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2446 init_vn_nary_op_from_op (vno1, op);
2447 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2450 /* Insert the rhs of STMT into the current hash table with a value number of
2451 RESULT. */
2453 vn_nary_op_t
2454 vn_nary_op_insert_stmt (gimple stmt, tree result)
2456 vn_nary_op_t vno1
2457 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2458 result, VN_INFO (result)->value_id);
2459 init_vn_nary_op_from_stmt (vno1, stmt);
2460 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2463 /* Compute a hashcode for PHI operation VP1 and return it. */
2465 static inline hashval_t
2466 vn_phi_compute_hash (vn_phi_t vp1)
2468 hashval_t result;
2469 int i;
2470 tree phi1op;
2471 tree type;
2473 result = vp1->block->index;
2475 /* If all PHI arguments are constants we need to distinguish
2476 the PHI node via its type. */
2477 type = vp1->type;
2478 result += vn_hash_type (type);
2480 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2482 if (phi1op == VN_TOP)
2483 continue;
2484 result = iterative_hash_expr (phi1op, result);
2487 return result;
2490 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2492 static int
2493 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2495 if (vp1->hashcode != vp2->hashcode)
2496 return false;
2498 if (vp1->block == vp2->block)
2500 int i;
2501 tree phi1op;
2503 /* If the PHI nodes do not have compatible types
2504 they are not the same. */
2505 if (!types_compatible_p (vp1->type, vp2->type))
2506 return false;
2508 /* Any phi in the same block will have it's arguments in the
2509 same edge order, because of how we store phi nodes. */
2510 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2512 tree phi2op = vp2->phiargs[i];
2513 if (phi1op == VN_TOP || phi2op == VN_TOP)
2514 continue;
2515 if (!expressions_equal_p (phi1op, phi2op))
2516 return false;
2518 return true;
2520 return false;
2523 static vec<tree> shared_lookup_phiargs;
2525 /* Lookup PHI in the current hash table, and return the resulting
2526 value number if it exists in the hash table. Return NULL_TREE if
2527 it does not exist in the hash table. */
2529 static tree
2530 vn_phi_lookup (gimple phi)
2532 vn_phi_s **slot;
2533 struct vn_phi_s vp1;
2534 unsigned i;
2536 shared_lookup_phiargs.truncate (0);
2538 /* Canonicalize the SSA_NAME's to their value number. */
2539 for (i = 0; i < gimple_phi_num_args (phi); i++)
2541 tree def = PHI_ARG_DEF (phi, i);
2542 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2543 shared_lookup_phiargs.safe_push (def);
2545 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2546 vp1.phiargs = shared_lookup_phiargs;
2547 vp1.block = gimple_bb (phi);
2548 vp1.hashcode = vn_phi_compute_hash (&vp1);
2549 slot = current_info->phis.find_slot_with_hash (&vp1, vp1.hashcode, NO_INSERT);
2550 if (!slot && current_info == optimistic_info)
2551 slot = valid_info->phis.find_slot_with_hash (&vp1, vp1.hashcode, NO_INSERT);
2552 if (!slot)
2553 return NULL_TREE;
2554 return (*slot)->result;
2557 /* Insert PHI into the current hash table with a value number of
2558 RESULT. */
2560 static vn_phi_t
2561 vn_phi_insert (gimple phi, tree result)
2563 vn_phi_s **slot;
2564 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2565 unsigned i;
2566 vec<tree> args = vNULL;
2568 /* Canonicalize the SSA_NAME's to their value number. */
2569 for (i = 0; i < gimple_phi_num_args (phi); i++)
2571 tree def = PHI_ARG_DEF (phi, i);
2572 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2573 args.safe_push (def);
2575 vp1->value_id = VN_INFO (result)->value_id;
2576 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2577 vp1->phiargs = args;
2578 vp1->block = gimple_bb (phi);
2579 vp1->result = result;
2580 vp1->hashcode = vn_phi_compute_hash (vp1);
2582 slot = current_info->phis.find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2584 /* Because we iterate over phi operations more than once, it's
2585 possible the slot might already exist here, hence no assert.*/
2586 *slot = vp1;
2587 return vp1;
2591 /* Print set of components in strongly connected component SCC to OUT. */
2593 static void
2594 print_scc (FILE *out, vec<tree> scc)
2596 tree var;
2597 unsigned int i;
2599 fprintf (out, "SCC consists of:");
2600 FOR_EACH_VEC_ELT (scc, i, var)
2602 fprintf (out, " ");
2603 print_generic_expr (out, var, 0);
2605 fprintf (out, "\n");
2608 /* Set the value number of FROM to TO, return true if it has changed
2609 as a result. */
2611 static inline bool
2612 set_ssa_val_to (tree from, tree to)
2614 tree currval = SSA_VAL (from);
2615 HOST_WIDE_INT toff, coff;
2617 if (from != to)
2619 if (currval == from)
2621 if (dump_file && (dump_flags & TDF_DETAILS))
2623 fprintf (dump_file, "Not changing value number of ");
2624 print_generic_expr (dump_file, from, 0);
2625 fprintf (dump_file, " from VARYING to ");
2626 print_generic_expr (dump_file, to, 0);
2627 fprintf (dump_file, "\n");
2629 return false;
2631 else if (TREE_CODE (to) == SSA_NAME
2632 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2633 to = from;
2636 /* The only thing we allow as value numbers are VN_TOP, ssa_names
2637 and invariants. So assert that here. */
2638 gcc_assert (to != NULL_TREE
2639 && (to == VN_TOP
2640 || TREE_CODE (to) == SSA_NAME
2641 || is_gimple_min_invariant (to)));
2643 if (dump_file && (dump_flags & TDF_DETAILS))
2645 fprintf (dump_file, "Setting value number of ");
2646 print_generic_expr (dump_file, from, 0);
2647 fprintf (dump_file, " to ");
2648 print_generic_expr (dump_file, to, 0);
2651 if (currval != to
2652 && !operand_equal_p (currval, to, 0)
2653 /* ??? For addresses involving volatile objects or types operand_equal_p
2654 does not reliably detect ADDR_EXPRs as equal. We know we are only
2655 getting invariant gimple addresses here, so can use
2656 get_addr_base_and_unit_offset to do this comparison. */
2657 && !(TREE_CODE (currval) == ADDR_EXPR
2658 && TREE_CODE (to) == ADDR_EXPR
2659 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2660 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2661 && coff == toff))
2663 VN_INFO (from)->valnum = to;
2664 if (dump_file && (dump_flags & TDF_DETAILS))
2665 fprintf (dump_file, " (changed)\n");
2666 return true;
2668 if (dump_file && (dump_flags & TDF_DETAILS))
2669 fprintf (dump_file, "\n");
2670 return false;
2673 /* Mark as processed all the definitions in the defining stmt of USE, or
2674 the USE itself. */
2676 static void
2677 mark_use_processed (tree use)
2679 ssa_op_iter iter;
2680 def_operand_p defp;
2681 gimple stmt = SSA_NAME_DEF_STMT (use);
2683 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2685 VN_INFO (use)->use_processed = true;
2686 return;
2689 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2691 tree def = DEF_FROM_PTR (defp);
2693 VN_INFO (def)->use_processed = true;
2697 /* Set all definitions in STMT to value number to themselves.
2698 Return true if a value number changed. */
2700 static bool
2701 defs_to_varying (gimple stmt)
2703 bool changed = false;
2704 ssa_op_iter iter;
2705 def_operand_p defp;
2707 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2709 tree def = DEF_FROM_PTR (defp);
2710 changed |= set_ssa_val_to (def, def);
2712 return changed;
2715 static bool expr_has_constants (tree expr);
2716 static tree valueize_expr (tree expr);
2718 /* Visit a copy between LHS and RHS, return true if the value number
2719 changed. */
2721 static bool
2722 visit_copy (tree lhs, tree rhs)
2724 /* The copy may have a more interesting constant filled expression
2725 (we don't, since we know our RHS is just an SSA name). */
2726 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2727 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2729 /* And finally valueize. */
2730 rhs = SSA_VAL (rhs);
2732 return set_ssa_val_to (lhs, rhs);
2735 /* Visit a nary operator RHS, value number it, and return true if the
2736 value number of LHS has changed as a result. */
2738 static bool
2739 visit_nary_op (tree lhs, gimple stmt)
2741 bool changed = false;
2742 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2744 if (result)
2745 changed = set_ssa_val_to (lhs, result);
2746 else
2748 changed = set_ssa_val_to (lhs, lhs);
2749 vn_nary_op_insert_stmt (stmt, lhs);
2752 return changed;
2755 /* Visit a call STMT storing into LHS. Return true if the value number
2756 of the LHS has changed as a result. */
2758 static bool
2759 visit_reference_op_call (tree lhs, gimple stmt)
2761 bool changed = false;
2762 struct vn_reference_s vr1;
2763 vn_reference_t vnresult = NULL;
2764 tree vuse = gimple_vuse (stmt);
2765 tree vdef = gimple_vdef (stmt);
2767 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2768 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2769 lhs = NULL_TREE;
2771 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2772 vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2773 vr1.type = gimple_expr_type (stmt);
2774 vr1.set = 0;
2775 vr1.hashcode = vn_reference_compute_hash (&vr1);
2776 vn_reference_lookup_1 (&vr1, &vnresult);
2778 if (vnresult)
2780 if (vnresult->result_vdef)
2781 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2783 if (!vnresult->result && lhs)
2784 vnresult->result = lhs;
2786 if (vnresult->result && lhs)
2788 changed |= set_ssa_val_to (lhs, vnresult->result);
2790 if (VN_INFO (vnresult->result)->has_constants)
2791 VN_INFO (lhs)->has_constants = true;
2794 else
2796 vn_reference_s **slot;
2797 vn_reference_t vr2;
2798 if (vdef)
2799 changed |= set_ssa_val_to (vdef, vdef);
2800 if (lhs)
2801 changed |= set_ssa_val_to (lhs, lhs);
2802 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2803 vr2->vuse = vr1.vuse;
2804 vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2805 vr2->type = vr1.type;
2806 vr2->set = vr1.set;
2807 vr2->hashcode = vr1.hashcode;
2808 vr2->result = lhs;
2809 vr2->result_vdef = vdef;
2810 slot = current_info->references.find_slot_with_hash (vr2, vr2->hashcode,
2811 INSERT);
2812 if (*slot)
2813 free_reference (*slot);
2814 *slot = vr2;
2817 return changed;
2820 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2821 and return true if the value number of the LHS has changed as a result. */
2823 static bool
2824 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2826 bool changed = false;
2827 tree last_vuse;
2828 tree result;
2830 last_vuse = gimple_vuse (stmt);
2831 last_vuse_ptr = &last_vuse;
2832 result = vn_reference_lookup (op, gimple_vuse (stmt),
2833 default_vn_walk_kind, NULL);
2834 last_vuse_ptr = NULL;
2836 /* If we have a VCE, try looking up its operand as it might be stored in
2837 a different type. */
2838 if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2839 result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2840 default_vn_walk_kind, NULL);
2842 /* We handle type-punning through unions by value-numbering based
2843 on offset and size of the access. Be prepared to handle a
2844 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2845 if (result
2846 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2848 /* We will be setting the value number of lhs to the value number
2849 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2850 So first simplify and lookup this expression to see if it
2851 is already available. */
2852 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2853 if ((CONVERT_EXPR_P (val)
2854 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2855 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2857 tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2858 if ((CONVERT_EXPR_P (tem)
2859 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2860 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2861 TREE_TYPE (val), tem)))
2862 val = tem;
2864 result = val;
2865 if (!is_gimple_min_invariant (val)
2866 && TREE_CODE (val) != SSA_NAME)
2867 result = vn_nary_op_lookup (val, NULL);
2868 /* If the expression is not yet available, value-number lhs to
2869 a new SSA_NAME we create. */
2870 if (!result)
2872 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
2873 "vntemp");
2874 /* Initialize value-number information properly. */
2875 VN_INFO_GET (result)->valnum = result;
2876 VN_INFO (result)->value_id = get_next_value_id ();
2877 VN_INFO (result)->expr = val;
2878 VN_INFO (result)->has_constants = expr_has_constants (val);
2879 VN_INFO (result)->needs_insertion = true;
2880 /* As all "inserted" statements are singleton SCCs, insert
2881 to the valid table. This is strictly needed to
2882 avoid re-generating new value SSA_NAMEs for the same
2883 expression during SCC iteration over and over (the
2884 optimistic table gets cleared after each iteration).
2885 We do not need to insert into the optimistic table, as
2886 lookups there will fall back to the valid table. */
2887 if (current_info == optimistic_info)
2889 current_info = valid_info;
2890 vn_nary_op_insert (val, result);
2891 current_info = optimistic_info;
2893 else
2894 vn_nary_op_insert (val, result);
2895 if (dump_file && (dump_flags & TDF_DETAILS))
2897 fprintf (dump_file, "Inserting name ");
2898 print_generic_expr (dump_file, result, 0);
2899 fprintf (dump_file, " for expression ");
2900 print_generic_expr (dump_file, val, 0);
2901 fprintf (dump_file, "\n");
2906 if (result)
2908 changed = set_ssa_val_to (lhs, result);
2909 if (TREE_CODE (result) == SSA_NAME
2910 && VN_INFO (result)->has_constants)
2912 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2913 VN_INFO (lhs)->has_constants = true;
2916 else
2918 changed = set_ssa_val_to (lhs, lhs);
2919 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
2922 return changed;
2926 /* Visit a store to a reference operator LHS, part of STMT, value number it,
2927 and return true if the value number of the LHS has changed as a result. */
2929 static bool
2930 visit_reference_op_store (tree lhs, tree op, gimple stmt)
2932 bool changed = false;
2933 vn_reference_t vnresult = NULL;
2934 tree result, assign;
2935 bool resultsame = false;
2936 tree vuse = gimple_vuse (stmt);
2937 tree vdef = gimple_vdef (stmt);
2939 /* First we want to lookup using the *vuses* from the store and see
2940 if there the last store to this location with the same address
2941 had the same value.
2943 The vuses represent the memory state before the store. If the
2944 memory state, address, and value of the store is the same as the
2945 last store to this location, then this store will produce the
2946 same memory state as that store.
2948 In this case the vdef versions for this store are value numbered to those
2949 vuse versions, since they represent the same memory state after
2950 this store.
2952 Otherwise, the vdefs for the store are used when inserting into
2953 the table, since the store generates a new memory state. */
2955 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
2957 if (result)
2959 if (TREE_CODE (result) == SSA_NAME)
2960 result = SSA_VAL (result);
2961 if (TREE_CODE (op) == SSA_NAME)
2962 op = SSA_VAL (op);
2963 resultsame = expressions_equal_p (result, op);
2966 if (!result || !resultsame)
2968 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2969 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
2970 if (vnresult)
2972 VN_INFO (vdef)->use_processed = true;
2973 return set_ssa_val_to (vdef, vnresult->result_vdef);
2977 if (!result || !resultsame)
2979 if (dump_file && (dump_flags & TDF_DETAILS))
2981 fprintf (dump_file, "No store match\n");
2982 fprintf (dump_file, "Value numbering store ");
2983 print_generic_expr (dump_file, lhs, 0);
2984 fprintf (dump_file, " to ");
2985 print_generic_expr (dump_file, op, 0);
2986 fprintf (dump_file, "\n");
2988 /* Have to set value numbers before insert, since insert is
2989 going to valueize the references in-place. */
2990 if (vdef)
2992 changed |= set_ssa_val_to (vdef, vdef);
2995 /* Do not insert structure copies into the tables. */
2996 if (is_gimple_min_invariant (op)
2997 || is_gimple_reg (op))
2998 vn_reference_insert (lhs, op, vdef, NULL);
3000 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3001 vn_reference_insert (assign, lhs, vuse, vdef);
3003 else
3005 /* We had a match, so value number the vdef to have the value
3006 number of the vuse it came from. */
3008 if (dump_file && (dump_flags & TDF_DETAILS))
3009 fprintf (dump_file, "Store matched earlier value,"
3010 "value numbering store vdefs to matching vuses.\n");
3012 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3015 return changed;
3018 /* Visit and value number PHI, return true if the value number
3019 changed. */
3021 static bool
3022 visit_phi (gimple phi)
3024 bool changed = false;
3025 tree result;
3026 tree sameval = VN_TOP;
3027 bool allsame = true;
3028 unsigned i;
3030 /* TODO: We could check for this in init_sccvn, and replace this
3031 with a gcc_assert. */
3032 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3033 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3035 /* See if all non-TOP arguments have the same value. TOP is
3036 equivalent to everything, so we can ignore it. */
3037 for (i = 0; i < gimple_phi_num_args (phi); i++)
3039 tree def = PHI_ARG_DEF (phi, i);
3041 if (TREE_CODE (def) == SSA_NAME)
3042 def = SSA_VAL (def);
3043 if (def == VN_TOP)
3044 continue;
3045 if (sameval == VN_TOP)
3047 sameval = def;
3049 else
3051 if (!expressions_equal_p (def, sameval))
3053 allsame = false;
3054 break;
3059 /* If all value numbered to the same value, the phi node has that
3060 value. */
3061 if (allsame)
3063 if (is_gimple_min_invariant (sameval))
3065 VN_INFO (PHI_RESULT (phi))->has_constants = true;
3066 VN_INFO (PHI_RESULT (phi))->expr = sameval;
3068 else
3070 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3071 VN_INFO (PHI_RESULT (phi))->expr = sameval;
3074 if (TREE_CODE (sameval) == SSA_NAME)
3075 return visit_copy (PHI_RESULT (phi), sameval);
3077 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3080 /* Otherwise, see if it is equivalent to a phi node in this block. */
3081 result = vn_phi_lookup (phi);
3082 if (result)
3084 if (TREE_CODE (result) == SSA_NAME)
3085 changed = visit_copy (PHI_RESULT (phi), result);
3086 else
3087 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3089 else
3091 vn_phi_insert (phi, PHI_RESULT (phi));
3092 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3093 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3094 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3097 return changed;
3100 /* Return true if EXPR contains constants. */
3102 static bool
3103 expr_has_constants (tree expr)
3105 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3107 case tcc_unary:
3108 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3110 case tcc_binary:
3111 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3112 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3113 /* Constants inside reference ops are rarely interesting, but
3114 it can take a lot of looking to find them. */
3115 case tcc_reference:
3116 case tcc_declaration:
3117 return false;
3118 default:
3119 return is_gimple_min_invariant (expr);
3121 return false;
3124 /* Return true if STMT contains constants. */
3126 static bool
3127 stmt_has_constants (gimple stmt)
3129 tree tem;
3131 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3132 return false;
3134 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3136 case GIMPLE_TERNARY_RHS:
3137 tem = gimple_assign_rhs3 (stmt);
3138 if (TREE_CODE (tem) == SSA_NAME)
3139 tem = SSA_VAL (tem);
3140 if (is_gimple_min_invariant (tem))
3141 return true;
3142 /* Fallthru. */
3144 case GIMPLE_BINARY_RHS:
3145 tem = gimple_assign_rhs2 (stmt);
3146 if (TREE_CODE (tem) == SSA_NAME)
3147 tem = SSA_VAL (tem);
3148 if (is_gimple_min_invariant (tem))
3149 return true;
3150 /* Fallthru. */
3152 case GIMPLE_SINGLE_RHS:
3153 /* Constants inside reference ops are rarely interesting, but
3154 it can take a lot of looking to find them. */
3155 case GIMPLE_UNARY_RHS:
3156 tem = gimple_assign_rhs1 (stmt);
3157 if (TREE_CODE (tem) == SSA_NAME)
3158 tem = SSA_VAL (tem);
3159 return is_gimple_min_invariant (tem);
3161 default:
3162 gcc_unreachable ();
3164 return false;
3167 /* Replace SSA_NAMES in expr with their value numbers, and return the
3168 result.
3169 This is performed in place. */
3171 static tree
3172 valueize_expr (tree expr)
3174 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3176 case tcc_binary:
3177 TREE_OPERAND (expr, 1) = vn_valueize (TREE_OPERAND (expr, 1));
3178 /* Fallthru. */
3179 case tcc_unary:
3180 TREE_OPERAND (expr, 0) = vn_valueize (TREE_OPERAND (expr, 0));
3181 break;
3182 default:;
3184 return expr;
3187 /* Simplify the binary expression RHS, and return the result if
3188 simplified. */
3190 static tree
3191 simplify_binary_expression (gimple stmt)
3193 tree result = NULL_TREE;
3194 tree op0 = gimple_assign_rhs1 (stmt);
3195 tree op1 = gimple_assign_rhs2 (stmt);
3196 enum tree_code code = gimple_assign_rhs_code (stmt);
3198 /* This will not catch every single case we could combine, but will
3199 catch those with constants. The goal here is to simultaneously
3200 combine constants between expressions, but avoid infinite
3201 expansion of expressions during simplification. */
3202 if (TREE_CODE (op0) == SSA_NAME)
3204 if (VN_INFO (op0)->has_constants
3205 || TREE_CODE_CLASS (code) == tcc_comparison
3206 || code == COMPLEX_EXPR)
3207 op0 = valueize_expr (vn_get_expr_for (op0));
3208 else
3209 op0 = vn_valueize (op0);
3212 if (TREE_CODE (op1) == SSA_NAME)
3214 if (VN_INFO (op1)->has_constants
3215 || code == COMPLEX_EXPR)
3216 op1 = valueize_expr (vn_get_expr_for (op1));
3217 else
3218 op1 = vn_valueize (op1);
3221 /* Pointer plus constant can be represented as invariant address.
3222 Do so to allow further propatation, see also tree forwprop. */
3223 if (code == POINTER_PLUS_EXPR
3224 && host_integerp (op1, 1)
3225 && TREE_CODE (op0) == ADDR_EXPR
3226 && is_gimple_min_invariant (op0))
3227 return build_invariant_address (TREE_TYPE (op0),
3228 TREE_OPERAND (op0, 0),
3229 TREE_INT_CST_LOW (op1));
3231 /* Avoid folding if nothing changed. */
3232 if (op0 == gimple_assign_rhs1 (stmt)
3233 && op1 == gimple_assign_rhs2 (stmt))
3234 return NULL_TREE;
3236 fold_defer_overflow_warnings ();
3238 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3239 if (result)
3240 STRIP_USELESS_TYPE_CONVERSION (result);
3242 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3243 stmt, 0);
3245 /* Make sure result is not a complex expression consisting
3246 of operators of operators (IE (a + b) + (a + c))
3247 Otherwise, we will end up with unbounded expressions if
3248 fold does anything at all. */
3249 if (result && valid_gimple_rhs_p (result))
3250 return result;
3252 return NULL_TREE;
3255 /* Simplify the unary expression RHS, and return the result if
3256 simplified. */
3258 static tree
3259 simplify_unary_expression (gimple stmt)
3261 tree result = NULL_TREE;
3262 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3263 enum tree_code code = gimple_assign_rhs_code (stmt);
3265 /* We handle some tcc_reference codes here that are all
3266 GIMPLE_ASSIGN_SINGLE codes. */
3267 if (code == REALPART_EXPR
3268 || code == IMAGPART_EXPR
3269 || code == VIEW_CONVERT_EXPR
3270 || code == BIT_FIELD_REF)
3271 op0 = TREE_OPERAND (op0, 0);
3273 if (TREE_CODE (op0) != SSA_NAME)
3274 return NULL_TREE;
3276 orig_op0 = op0;
3277 if (VN_INFO (op0)->has_constants)
3278 op0 = valueize_expr (vn_get_expr_for (op0));
3279 else if (CONVERT_EXPR_CODE_P (code)
3280 || code == REALPART_EXPR
3281 || code == IMAGPART_EXPR
3282 || code == VIEW_CONVERT_EXPR
3283 || code == BIT_FIELD_REF)
3285 /* We want to do tree-combining on conversion-like expressions.
3286 Make sure we feed only SSA_NAMEs or constants to fold though. */
3287 tree tem = valueize_expr (vn_get_expr_for (op0));
3288 if (UNARY_CLASS_P (tem)
3289 || BINARY_CLASS_P (tem)
3290 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3291 || TREE_CODE (tem) == SSA_NAME
3292 || TREE_CODE (tem) == CONSTRUCTOR
3293 || is_gimple_min_invariant (tem))
3294 op0 = tem;
3297 /* Avoid folding if nothing changed, but remember the expression. */
3298 if (op0 == orig_op0)
3299 return NULL_TREE;
3301 if (code == BIT_FIELD_REF)
3303 tree rhs = gimple_assign_rhs1 (stmt);
3304 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3305 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3307 else
3308 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3309 if (result)
3311 STRIP_USELESS_TYPE_CONVERSION (result);
3312 if (valid_gimple_rhs_p (result))
3313 return result;
3316 return NULL_TREE;
3319 /* Try to simplify RHS using equivalences and constant folding. */
3321 static tree
3322 try_to_simplify (gimple stmt)
3324 enum tree_code code = gimple_assign_rhs_code (stmt);
3325 tree tem;
3327 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3328 in this case, there is no point in doing extra work. */
3329 if (code == SSA_NAME)
3330 return NULL_TREE;
3332 /* First try constant folding based on our current lattice. */
3333 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize);
3334 if (tem
3335 && (TREE_CODE (tem) == SSA_NAME
3336 || is_gimple_min_invariant (tem)))
3337 return tem;
3339 /* If that didn't work try combining multiple statements. */
3340 switch (TREE_CODE_CLASS (code))
3342 case tcc_reference:
3343 /* Fallthrough for some unary codes that can operate on registers. */
3344 if (!(code == REALPART_EXPR
3345 || code == IMAGPART_EXPR
3346 || code == VIEW_CONVERT_EXPR
3347 || code == BIT_FIELD_REF))
3348 break;
3349 /* We could do a little more with unary ops, if they expand
3350 into binary ops, but it's debatable whether it is worth it. */
3351 case tcc_unary:
3352 return simplify_unary_expression (stmt);
3354 case tcc_comparison:
3355 case tcc_binary:
3356 return simplify_binary_expression (stmt);
3358 default:
3359 break;
3362 return NULL_TREE;
3365 /* Visit and value number USE, return true if the value number
3366 changed. */
3368 static bool
3369 visit_use (tree use)
3371 bool changed = false;
3372 gimple stmt = SSA_NAME_DEF_STMT (use);
3374 mark_use_processed (use);
3376 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3377 if (dump_file && (dump_flags & TDF_DETAILS)
3378 && !SSA_NAME_IS_DEFAULT_DEF (use))
3380 fprintf (dump_file, "Value numbering ");
3381 print_generic_expr (dump_file, use, 0);
3382 fprintf (dump_file, " stmt = ");
3383 print_gimple_stmt (dump_file, stmt, 0, 0);
3386 /* Handle uninitialized uses. */
3387 if (SSA_NAME_IS_DEFAULT_DEF (use))
3388 changed = set_ssa_val_to (use, use);
3389 else
3391 if (gimple_code (stmt) == GIMPLE_PHI)
3392 changed = visit_phi (stmt);
3393 else if (gimple_has_volatile_ops (stmt))
3394 changed = defs_to_varying (stmt);
3395 else if (is_gimple_assign (stmt))
3397 enum tree_code code = gimple_assign_rhs_code (stmt);
3398 tree lhs = gimple_assign_lhs (stmt);
3399 tree rhs1 = gimple_assign_rhs1 (stmt);
3400 tree simplified;
3402 /* Shortcut for copies. Simplifying copies is pointless,
3403 since we copy the expression and value they represent. */
3404 if (code == SSA_NAME
3405 && TREE_CODE (lhs) == SSA_NAME)
3407 changed = visit_copy (lhs, rhs1);
3408 goto done;
3410 simplified = try_to_simplify (stmt);
3411 if (simplified)
3413 if (dump_file && (dump_flags & TDF_DETAILS))
3415 fprintf (dump_file, "RHS ");
3416 print_gimple_expr (dump_file, stmt, 0, 0);
3417 fprintf (dump_file, " simplified to ");
3418 print_generic_expr (dump_file, simplified, 0);
3419 if (TREE_CODE (lhs) == SSA_NAME)
3420 fprintf (dump_file, " has constants %d\n",
3421 expr_has_constants (simplified));
3422 else
3423 fprintf (dump_file, "\n");
3426 /* Setting value numbers to constants will occasionally
3427 screw up phi congruence because constants are not
3428 uniquely associated with a single ssa name that can be
3429 looked up. */
3430 if (simplified
3431 && is_gimple_min_invariant (simplified)
3432 && TREE_CODE (lhs) == SSA_NAME)
3434 VN_INFO (lhs)->expr = simplified;
3435 VN_INFO (lhs)->has_constants = true;
3436 changed = set_ssa_val_to (lhs, simplified);
3437 goto done;
3439 else if (simplified
3440 && TREE_CODE (simplified) == SSA_NAME
3441 && TREE_CODE (lhs) == SSA_NAME)
3443 changed = visit_copy (lhs, simplified);
3444 goto done;
3446 else if (simplified)
3448 if (TREE_CODE (lhs) == SSA_NAME)
3450 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3451 /* We have to unshare the expression or else
3452 valuizing may change the IL stream. */
3453 VN_INFO (lhs)->expr = unshare_expr (simplified);
3456 else if (stmt_has_constants (stmt)
3457 && TREE_CODE (lhs) == SSA_NAME)
3458 VN_INFO (lhs)->has_constants = true;
3459 else if (TREE_CODE (lhs) == SSA_NAME)
3461 /* We reset expr and constantness here because we may
3462 have been value numbering optimistically, and
3463 iterating. They may become non-constant in this case,
3464 even if they were optimistically constant. */
3466 VN_INFO (lhs)->has_constants = false;
3467 VN_INFO (lhs)->expr = NULL_TREE;
3470 if ((TREE_CODE (lhs) == SSA_NAME
3471 /* We can substitute SSA_NAMEs that are live over
3472 abnormal edges with their constant value. */
3473 && !(gimple_assign_copy_p (stmt)
3474 && is_gimple_min_invariant (rhs1))
3475 && !(simplified
3476 && is_gimple_min_invariant (simplified))
3477 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3478 /* Stores or copies from SSA_NAMEs that are live over
3479 abnormal edges are a problem. */
3480 || (code == SSA_NAME
3481 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3482 changed = defs_to_varying (stmt);
3483 else if (REFERENCE_CLASS_P (lhs)
3484 || DECL_P (lhs))
3485 changed = visit_reference_op_store (lhs, rhs1, stmt);
3486 else if (TREE_CODE (lhs) == SSA_NAME)
3488 if ((gimple_assign_copy_p (stmt)
3489 && is_gimple_min_invariant (rhs1))
3490 || (simplified
3491 && is_gimple_min_invariant (simplified)))
3493 VN_INFO (lhs)->has_constants = true;
3494 if (simplified)
3495 changed = set_ssa_val_to (lhs, simplified);
3496 else
3497 changed = set_ssa_val_to (lhs, rhs1);
3499 else
3501 /* First try to lookup the simplified expression. */
3502 if (simplified)
3504 enum gimple_rhs_class rhs_class;
3507 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3508 if ((rhs_class == GIMPLE_UNARY_RHS
3509 || rhs_class == GIMPLE_BINARY_RHS
3510 || rhs_class == GIMPLE_TERNARY_RHS)
3511 && valid_gimple_rhs_p (simplified))
3513 tree result = vn_nary_op_lookup (simplified, NULL);
3514 if (result)
3516 changed = set_ssa_val_to (lhs, result);
3517 goto done;
3522 /* Otherwise visit the original statement. */
3523 switch (vn_get_stmt_kind (stmt))
3525 case VN_NARY:
3526 changed = visit_nary_op (lhs, stmt);
3527 break;
3528 case VN_REFERENCE:
3529 changed = visit_reference_op_load (lhs, rhs1, stmt);
3530 break;
3531 default:
3532 changed = defs_to_varying (stmt);
3533 break;
3537 else
3538 changed = defs_to_varying (stmt);
3540 else if (is_gimple_call (stmt))
3542 tree lhs = gimple_call_lhs (stmt);
3544 /* ??? We could try to simplify calls. */
3546 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3548 if (stmt_has_constants (stmt))
3549 VN_INFO (lhs)->has_constants = true;
3550 else
3552 /* We reset expr and constantness here because we may
3553 have been value numbering optimistically, and
3554 iterating. They may become non-constant in this case,
3555 even if they were optimistically constant. */
3556 VN_INFO (lhs)->has_constants = false;
3557 VN_INFO (lhs)->expr = NULL_TREE;
3560 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3562 changed = defs_to_varying (stmt);
3563 goto done;
3567 if (!gimple_call_internal_p (stmt)
3568 && (/* Calls to the same function with the same vuse
3569 and the same operands do not necessarily return the same
3570 value, unless they're pure or const. */
3571 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3572 /* If calls have a vdef, subsequent calls won't have
3573 the same incoming vuse. So, if 2 calls with vdef have the
3574 same vuse, we know they're not subsequent.
3575 We can value number 2 calls to the same function with the
3576 same vuse and the same operands which are not subsequent
3577 the same, because there is no code in the program that can
3578 compare the 2 values... */
3579 || (gimple_vdef (stmt)
3580 /* ... unless the call returns a pointer which does
3581 not alias with anything else. In which case the
3582 information that the values are distinct are encoded
3583 in the IL. */
3584 && !(gimple_call_return_flags (stmt) & ERF_NOALIAS))))
3585 changed = visit_reference_op_call (lhs, stmt);
3586 else
3587 changed = defs_to_varying (stmt);
3589 else
3590 changed = defs_to_varying (stmt);
3592 done:
3593 return changed;
3596 /* Compare two operands by reverse postorder index */
3598 static int
3599 compare_ops (const void *pa, const void *pb)
3601 const tree opa = *((const tree *)pa);
3602 const tree opb = *((const tree *)pb);
3603 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3604 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3605 basic_block bba;
3606 basic_block bbb;
3608 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3609 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3610 else if (gimple_nop_p (opstmta))
3611 return -1;
3612 else if (gimple_nop_p (opstmtb))
3613 return 1;
3615 bba = gimple_bb (opstmta);
3616 bbb = gimple_bb (opstmtb);
3618 if (!bba && !bbb)
3619 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3620 else if (!bba)
3621 return -1;
3622 else if (!bbb)
3623 return 1;
3625 if (bba == bbb)
3627 if (gimple_code (opstmta) == GIMPLE_PHI
3628 && gimple_code (opstmtb) == GIMPLE_PHI)
3629 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3630 else if (gimple_code (opstmta) == GIMPLE_PHI)
3631 return -1;
3632 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3633 return 1;
3634 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3635 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3636 else
3637 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3639 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3642 /* Sort an array containing members of a strongly connected component
3643 SCC so that the members are ordered by RPO number.
3644 This means that when the sort is complete, iterating through the
3645 array will give you the members in RPO order. */
3647 static void
3648 sort_scc (vec<tree> scc)
3650 scc.qsort (compare_ops);
3653 /* Insert the no longer used nary ONARY to the hash INFO. */
3655 static void
3656 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3658 size_t size = sizeof_vn_nary_op (onary->length);
3659 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3660 &info->nary_obstack);
3661 memcpy (nary, onary, size);
3662 vn_nary_op_insert_into (nary, info->nary, false);
3665 /* Insert the no longer used phi OPHI to the hash INFO. */
3667 static void
3668 copy_phi (vn_phi_t ophi, vn_tables_t info)
3670 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3671 vn_phi_s **slot;
3672 memcpy (phi, ophi, sizeof (*phi));
3673 ophi->phiargs.create (0);
3674 slot = info->phis.find_slot_with_hash (phi, phi->hashcode, INSERT);
3675 gcc_assert (!*slot);
3676 *slot = phi;
3679 /* Insert the no longer used reference OREF to the hash INFO. */
3681 static void
3682 copy_reference (vn_reference_t oref, vn_tables_t info)
3684 vn_reference_t ref;
3685 vn_reference_s **slot;
3686 ref = (vn_reference_t) pool_alloc (info->references_pool);
3687 memcpy (ref, oref, sizeof (*ref));
3688 oref->operands.create (0);
3689 slot = info->references.find_slot_with_hash (ref, ref->hashcode, INSERT);
3690 if (*slot)
3691 free_reference (*slot);
3692 *slot = ref;
3695 /* Process a strongly connected component in the SSA graph. */
3697 static void
3698 process_scc (vec<tree> scc)
3700 tree var;
3701 unsigned int i;
3702 unsigned int iterations = 0;
3703 bool changed = true;
3704 vn_nary_op_iterator_type hin;
3705 vn_phi_iterator_type hip;
3706 vn_reference_iterator_type hir;
3707 vn_nary_op_t nary;
3708 vn_phi_t phi;
3709 vn_reference_t ref;
3711 /* If the SCC has a single member, just visit it. */
3712 if (scc.length () == 1)
3714 tree use = scc[0];
3715 if (VN_INFO (use)->use_processed)
3716 return;
3717 /* We need to make sure it doesn't form a cycle itself, which can
3718 happen for self-referential PHI nodes. In that case we would
3719 end up inserting an expression with VN_TOP operands into the
3720 valid table which makes us derive bogus equivalences later.
3721 The cheapest way to check this is to assume it for all PHI nodes. */
3722 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3723 /* Fallthru to iteration. */ ;
3724 else
3726 visit_use (use);
3727 return;
3731 /* Iterate over the SCC with the optimistic table until it stops
3732 changing. */
3733 current_info = optimistic_info;
3734 while (changed)
3736 changed = false;
3737 iterations++;
3738 if (dump_file && (dump_flags & TDF_DETAILS))
3739 fprintf (dump_file, "Starting iteration %d\n", iterations);
3740 /* As we are value-numbering optimistically we have to
3741 clear the expression tables and the simplified expressions
3742 in each iteration until we converge. */
3743 optimistic_info->nary.empty ();
3744 optimistic_info->phis.empty ();
3745 optimistic_info->references.empty ();
3746 obstack_free (&optimistic_info->nary_obstack, NULL);
3747 gcc_obstack_init (&optimistic_info->nary_obstack);
3748 empty_alloc_pool (optimistic_info->phis_pool);
3749 empty_alloc_pool (optimistic_info->references_pool);
3750 FOR_EACH_VEC_ELT (scc, i, var)
3751 VN_INFO (var)->expr = NULL_TREE;
3752 FOR_EACH_VEC_ELT (scc, i, var)
3753 changed |= visit_use (var);
3756 statistics_histogram_event (cfun, "SCC iterations", iterations);
3758 /* Finally, copy the contents of the no longer used optimistic
3759 table to the valid table. */
3760 FOR_EACH_HASH_TABLE_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hin)
3761 copy_nary (nary, valid_info);
3762 FOR_EACH_HASH_TABLE_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hip)
3763 copy_phi (phi, valid_info);
3764 FOR_EACH_HASH_TABLE_ELEMENT (optimistic_info->references,
3765 ref, vn_reference_t, hir)
3766 copy_reference (ref, valid_info);
3768 current_info = valid_info;
3772 /* Pop the components of the found SCC for NAME off the SCC stack
3773 and process them. Returns true if all went well, false if
3774 we run into resource limits. */
3776 static bool
3777 extract_and_process_scc_for_name (tree name)
3779 vec<tree> scc = vNULL;
3780 tree x;
3782 /* Found an SCC, pop the components off the SCC stack and
3783 process them. */
3786 x = sccstack.pop ();
3788 VN_INFO (x)->on_sccstack = false;
3789 scc.safe_push (x);
3790 } while (x != name);
3792 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3793 if (scc.length ()
3794 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3796 if (dump_file)
3797 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3798 "SCC size %u exceeding %u\n", scc.length (),
3799 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3801 scc.release ();
3802 return false;
3805 if (scc.length () > 1)
3806 sort_scc (scc);
3808 if (dump_file && (dump_flags & TDF_DETAILS))
3809 print_scc (dump_file, scc);
3811 process_scc (scc);
3813 scc.release ();
3815 return true;
3818 /* Depth first search on NAME to discover and process SCC's in the SSA
3819 graph.
3820 Execution of this algorithm relies on the fact that the SCC's are
3821 popped off the stack in topological order.
3822 Returns true if successful, false if we stopped processing SCC's due
3823 to resource constraints. */
3825 static bool
3826 DFS (tree name)
3828 vec<ssa_op_iter> itervec = vNULL;
3829 vec<tree> namevec = vNULL;
3830 use_operand_p usep = NULL;
3831 gimple defstmt;
3832 tree use;
3833 ssa_op_iter iter;
3835 start_over:
3836 /* SCC info */
3837 VN_INFO (name)->dfsnum = next_dfs_num++;
3838 VN_INFO (name)->visited = true;
3839 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3841 sccstack.safe_push (name);
3842 VN_INFO (name)->on_sccstack = true;
3843 defstmt = SSA_NAME_DEF_STMT (name);
3845 /* Recursively DFS on our operands, looking for SCC's. */
3846 if (!gimple_nop_p (defstmt))
3848 /* Push a new iterator. */
3849 if (gimple_code (defstmt) == GIMPLE_PHI)
3850 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3851 else
3852 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3854 else
3855 clear_and_done_ssa_iter (&iter);
3857 while (1)
3859 /* If we are done processing uses of a name, go up the stack
3860 of iterators and process SCCs as we found them. */
3861 if (op_iter_done (&iter))
3863 /* See if we found an SCC. */
3864 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3865 if (!extract_and_process_scc_for_name (name))
3867 namevec.release ();
3868 itervec.release ();
3869 return false;
3872 /* Check if we are done. */
3873 if (namevec.is_empty ())
3875 namevec.release ();
3876 itervec.release ();
3877 return true;
3880 /* Restore the last use walker and continue walking there. */
3881 use = name;
3882 name = namevec.pop ();
3883 memcpy (&iter, &itervec.last (),
3884 sizeof (ssa_op_iter));
3885 itervec.pop ();
3886 goto continue_walking;
3889 use = USE_FROM_PTR (usep);
3891 /* Since we handle phi nodes, we will sometimes get
3892 invariants in the use expression. */
3893 if (TREE_CODE (use) == SSA_NAME)
3895 if (! (VN_INFO (use)->visited))
3897 /* Recurse by pushing the current use walking state on
3898 the stack and starting over. */
3899 itervec.safe_push (iter);
3900 namevec.safe_push (name);
3901 name = use;
3902 goto start_over;
3904 continue_walking:
3905 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3906 VN_INFO (use)->low);
3908 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3909 && VN_INFO (use)->on_sccstack)
3911 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3912 VN_INFO (name)->low);
3916 usep = op_iter_next_use (&iter);
3920 /* Allocate a value number table. */
3922 static void
3923 allocate_vn_table (vn_tables_t table)
3925 table->phis.create (23);
3926 table->nary.create (23);
3927 table->references.create (23);
3929 gcc_obstack_init (&table->nary_obstack);
3930 table->phis_pool = create_alloc_pool ("VN phis",
3931 sizeof (struct vn_phi_s),
3932 30);
3933 table->references_pool = create_alloc_pool ("VN references",
3934 sizeof (struct vn_reference_s),
3935 30);
3938 /* Free a value number table. */
3940 static void
3941 free_vn_table (vn_tables_t table)
3943 table->phis.dispose ();
3944 table->nary.dispose ();
3945 table->references.dispose ();
3946 obstack_free (&table->nary_obstack, NULL);
3947 free_alloc_pool (table->phis_pool);
3948 free_alloc_pool (table->references_pool);
3951 static void
3952 init_scc_vn (void)
3954 size_t i;
3955 int j;
3956 int *rpo_numbers_temp;
3958 calculate_dominance_info (CDI_DOMINATORS);
3959 sccstack.create (0);
3960 constant_to_value_id.create (23);
3962 constant_value_ids = BITMAP_ALLOC (NULL);
3964 next_dfs_num = 1;
3965 next_value_id = 1;
3967 vn_ssa_aux_table.create (num_ssa_names + 1);
3968 /* VEC_alloc doesn't actually grow it to the right size, it just
3969 preallocates the space to do so. */
3970 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
3971 gcc_obstack_init (&vn_ssa_aux_obstack);
3973 shared_lookup_phiargs.create (0);
3974 shared_lookup_references.create (0);
3975 rpo_numbers = XNEWVEC (int, last_basic_block);
3976 rpo_numbers_temp = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
3977 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3979 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3980 the i'th block in RPO order is bb. We want to map bb's to RPO
3981 numbers, so we need to rearrange this array. */
3982 for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3983 rpo_numbers[rpo_numbers_temp[j]] = j;
3985 XDELETE (rpo_numbers_temp);
3987 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3989 /* Create the VN_INFO structures, and initialize value numbers to
3990 TOP. */
3991 for (i = 0; i < num_ssa_names; i++)
3993 tree name = ssa_name (i);
3994 if (name)
3996 VN_INFO_GET (name)->valnum = VN_TOP;
3997 VN_INFO (name)->expr = NULL_TREE;
3998 VN_INFO (name)->value_id = 0;
4002 renumber_gimple_stmt_uids ();
4004 /* Create the valid and optimistic value numbering tables. */
4005 valid_info = XCNEW (struct vn_tables_s);
4006 allocate_vn_table (valid_info);
4007 optimistic_info = XCNEW (struct vn_tables_s);
4008 allocate_vn_table (optimistic_info);
4011 void
4012 free_scc_vn (void)
4014 size_t i;
4016 constant_to_value_id.dispose ();
4017 BITMAP_FREE (constant_value_ids);
4018 shared_lookup_phiargs.release ();
4019 shared_lookup_references.release ();
4020 XDELETEVEC (rpo_numbers);
4022 for (i = 0; i < num_ssa_names; i++)
4024 tree name = ssa_name (i);
4025 if (name
4026 && VN_INFO (name)->needs_insertion)
4027 release_ssa_name (name);
4029 obstack_free (&vn_ssa_aux_obstack, NULL);
4030 vn_ssa_aux_table.release ();
4032 sccstack.release ();
4033 free_vn_table (valid_info);
4034 XDELETE (valid_info);
4035 free_vn_table (optimistic_info);
4036 XDELETE (optimistic_info);
4039 /* Set *ID according to RESULT. */
4041 static void
4042 set_value_id_for_result (tree result, unsigned int *id)
4044 if (result && TREE_CODE (result) == SSA_NAME)
4045 *id = VN_INFO (result)->value_id;
4046 else if (result && is_gimple_min_invariant (result))
4047 *id = get_or_alloc_constant_value_id (result);
4048 else
4049 *id = get_next_value_id ();
4052 /* Set the value ids in the valid hash tables. */
4054 static void
4055 set_hashtable_value_ids (void)
4057 vn_nary_op_iterator_type hin;
4058 vn_phi_iterator_type hip;
4059 vn_reference_iterator_type hir;
4060 vn_nary_op_t vno;
4061 vn_reference_t vr;
4062 vn_phi_t vp;
4064 /* Now set the value ids of the things we had put in the hash
4065 table. */
4067 FOR_EACH_HASH_TABLE_ELEMENT (valid_info->nary, vno, vn_nary_op_t, hin)
4068 set_value_id_for_result (vno->result, &vno->value_id);
4070 FOR_EACH_HASH_TABLE_ELEMENT (valid_info->phis, vp, vn_phi_t, hip)
4071 set_value_id_for_result (vp->result, &vp->value_id);
4073 FOR_EACH_HASH_TABLE_ELEMENT (valid_info->references, vr, vn_reference_t, hir)
4074 set_value_id_for_result (vr->result, &vr->value_id);
4077 /* Do SCCVN. Returns true if it finished, false if we bailed out
4078 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4079 how we use the alias oracle walking during the VN process. */
4081 bool
4082 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4084 size_t i;
4085 tree param;
4087 default_vn_walk_kind = default_vn_walk_kind_;
4089 init_scc_vn ();
4090 current_info = valid_info;
4092 for (param = DECL_ARGUMENTS (current_function_decl);
4093 param;
4094 param = DECL_CHAIN (param))
4096 tree def = ssa_default_def (cfun, param);
4097 if (def)
4098 VN_INFO (def)->valnum = def;
4101 for (i = 1; i < num_ssa_names; ++i)
4103 tree name = ssa_name (i);
4104 if (name
4105 && VN_INFO (name)->visited == false
4106 && !has_zero_uses (name))
4107 if (!DFS (name))
4109 free_scc_vn ();
4110 return false;
4114 /* Initialize the value ids. */
4116 for (i = 1; i < num_ssa_names; ++i)
4118 tree name = ssa_name (i);
4119 vn_ssa_aux_t info;
4120 if (!name)
4121 continue;
4122 info = VN_INFO (name);
4123 if (info->valnum == name
4124 || info->valnum == VN_TOP)
4125 info->value_id = get_next_value_id ();
4126 else if (is_gimple_min_invariant (info->valnum))
4127 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4130 /* Propagate. */
4131 for (i = 1; i < num_ssa_names; ++i)
4133 tree name = ssa_name (i);
4134 vn_ssa_aux_t info;
4135 if (!name)
4136 continue;
4137 info = VN_INFO (name);
4138 if (TREE_CODE (info->valnum) == SSA_NAME
4139 && info->valnum != name
4140 && info->value_id != VN_INFO (info->valnum)->value_id)
4141 info->value_id = VN_INFO (info->valnum)->value_id;
4144 set_hashtable_value_ids ();
4146 if (dump_file && (dump_flags & TDF_DETAILS))
4148 fprintf (dump_file, "Value numbers:\n");
4149 for (i = 0; i < num_ssa_names; i++)
4151 tree name = ssa_name (i);
4152 if (name
4153 && VN_INFO (name)->visited
4154 && SSA_VAL (name) != name)
4156 print_generic_expr (dump_file, name, 0);
4157 fprintf (dump_file, " = ");
4158 print_generic_expr (dump_file, SSA_VAL (name), 0);
4159 fprintf (dump_file, "\n");
4164 return true;
4167 /* Return the maximum value id we have ever seen. */
4169 unsigned int
4170 get_max_value_id (void)
4172 return next_value_id;
4175 /* Return the next unique value id. */
4177 unsigned int
4178 get_next_value_id (void)
4180 return next_value_id++;
4184 /* Compare two expressions E1 and E2 and return true if they are equal. */
4186 bool
4187 expressions_equal_p (tree e1, tree e2)
4189 /* The obvious case. */
4190 if (e1 == e2)
4191 return true;
4193 /* If only one of them is null, they cannot be equal. */
4194 if (!e1 || !e2)
4195 return false;
4197 /* Now perform the actual comparison. */
4198 if (TREE_CODE (e1) == TREE_CODE (e2)
4199 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4200 return true;
4202 return false;
4206 /* Return true if the nary operation NARY may trap. This is a copy
4207 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4209 bool
4210 vn_nary_may_trap (vn_nary_op_t nary)
4212 tree type;
4213 tree rhs2 = NULL_TREE;
4214 bool honor_nans = false;
4215 bool honor_snans = false;
4216 bool fp_operation = false;
4217 bool honor_trapv = false;
4218 bool handled, ret;
4219 unsigned i;
4221 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4222 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4223 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4225 type = nary->type;
4226 fp_operation = FLOAT_TYPE_P (type);
4227 if (fp_operation)
4229 honor_nans = flag_trapping_math && !flag_finite_math_only;
4230 honor_snans = flag_signaling_nans != 0;
4232 else if (INTEGRAL_TYPE_P (type)
4233 && TYPE_OVERFLOW_TRAPS (type))
4234 honor_trapv = true;
4236 if (nary->length >= 2)
4237 rhs2 = nary->op[1];
4238 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4239 honor_trapv,
4240 honor_nans, honor_snans, rhs2,
4241 &handled);
4242 if (handled
4243 && ret)
4244 return true;
4246 for (i = 0; i < nary->length; ++i)
4247 if (tree_could_trap_p (nary->op[i]))
4248 return true;
4250 return false;