2012-10-23 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob2391632364d9951cbe2c93096b1b93a22f3e195f
1 /* SCC value numbering for trees
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dan@dberlin.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
30 #include "tree-flow.h"
31 #include "gimple.h"
32 #include "dumpfile.h"
33 #include "hashtab.h"
34 #include "alloc-pool.h"
35 #include "flags.h"
36 #include "bitmap.h"
37 #include "cfgloop.h"
38 #include "params.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-ssa-sccvn.h"
41 #include "gimple-fold.h"
43 /* This algorithm is based on the SCC algorithm presented by Keith
44 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
45 (http://citeseer.ist.psu.edu/41805.html). In
46 straight line code, it is equivalent to a regular hash based value
47 numbering that is performed in reverse postorder.
49 For code with cycles, there are two alternatives, both of which
50 require keeping the hashtables separate from the actual list of
51 value numbers for SSA names.
53 1. Iterate value numbering in an RPO walk of the blocks, removing
54 all the entries from the hashtable after each iteration (but
55 keeping the SSA name->value number mapping between iterations).
56 Iterate until it does not change.
58 2. Perform value numbering as part of an SCC walk on the SSA graph,
59 iterating only the cycles in the SSA graph until they do not change
60 (using a separate, optimistic hashtable for value numbering the SCC
61 operands).
63 The second is not just faster in practice (because most SSA graph
64 cycles do not involve all the variables in the graph), it also has
65 some nice properties.
67 One of these nice properties is that when we pop an SCC off the
68 stack, we are guaranteed to have processed all the operands coming from
69 *outside of that SCC*, so we do not need to do anything special to
70 ensure they have value numbers.
72 Another nice property is that the SCC walk is done as part of a DFS
73 of the SSA graph, which makes it easy to perform combining and
74 simplifying operations at the same time.
76 The code below is deliberately written in a way that makes it easy
77 to separate the SCC walk from the other work it does.
79 In order to propagate constants through the code, we track which
80 expressions contain constants, and use those while folding. In
81 theory, we could also track expressions whose value numbers are
82 replaced, in case we end up folding based on expression
83 identities.
85 In order to value number memory, we assign value numbers to vuses.
86 This enables us to note that, for example, stores to the same
87 address of the same value from the same starting memory states are
88 equivalent.
89 TODO:
91 1. We can iterate only the changing portions of the SCC's, but
92 I have not seen an SCC big enough for this to be a win.
93 2. If you differentiate between phi nodes for loops and phi nodes
94 for if-then-else, you can properly consider phi nodes in different
95 blocks for equivalence.
96 3. We could value number vuses in more cases, particularly, whole
97 structure copies.
100 /* The set of hashtables and alloc_pool's for their items. */
102 typedef struct vn_tables_s
104 htab_t nary;
105 htab_t phis;
106 htab_t references;
107 struct obstack nary_obstack;
108 alloc_pool phis_pool;
109 alloc_pool references_pool;
110 } *vn_tables_t;
112 static htab_t constant_to_value_id;
113 static bitmap constant_value_ids;
116 /* Valid hashtables storing information we have proven to be
117 correct. */
119 static vn_tables_t valid_info;
121 /* Optimistic hashtables storing information we are making assumptions about
122 during iterations. */
124 static vn_tables_t optimistic_info;
126 /* Pointer to the set of hashtables that is currently being used.
127 Should always point to either the optimistic_info, or the
128 valid_info. */
130 static vn_tables_t current_info;
133 /* Reverse post order index for each basic block. */
135 static int *rpo_numbers;
137 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
139 /* This represents the top of the VN lattice, which is the universal
140 value. */
142 tree VN_TOP;
144 /* Unique counter for our value ids. */
146 static unsigned int next_value_id;
148 /* Next DFS number and the stack for strongly connected component
149 detection. */
151 static unsigned int next_dfs_num;
152 static VEC (tree, heap) *sccstack;
155 DEF_VEC_P(vn_ssa_aux_t);
156 DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
158 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
159 are allocated on an obstack for locality reasons, and to free them
160 without looping over the VEC. */
162 static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
163 static struct obstack vn_ssa_aux_obstack;
165 /* Return the value numbering information for a given SSA name. */
167 vn_ssa_aux_t
168 VN_INFO (tree name)
170 vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
171 SSA_NAME_VERSION (name));
172 gcc_checking_assert (res);
173 return res;
176 /* Set the value numbering info for a given SSA name to a given
177 value. */
179 static inline void
180 VN_INFO_SET (tree name, vn_ssa_aux_t value)
182 VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
183 SSA_NAME_VERSION (name), value);
186 /* Initialize the value numbering info for a given SSA name.
187 This should be called just once for every SSA name. */
189 vn_ssa_aux_t
190 VN_INFO_GET (tree name)
192 vn_ssa_aux_t newinfo;
194 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
195 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
196 if (SSA_NAME_VERSION (name) >= VEC_length (vn_ssa_aux_t, vn_ssa_aux_table))
197 VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table,
198 SSA_NAME_VERSION (name) + 1);
199 VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
200 SSA_NAME_VERSION (name), newinfo);
201 return newinfo;
205 /* Get the representative expression for the SSA_NAME NAME. Returns
206 the representative SSA_NAME if there is no expression associated with it. */
208 tree
209 vn_get_expr_for (tree name)
211 vn_ssa_aux_t vn = VN_INFO (name);
212 gimple def_stmt;
213 tree expr = NULL_TREE;
214 enum tree_code code;
216 if (vn->valnum == VN_TOP)
217 return name;
219 /* If the value-number is a constant it is the representative
220 expression. */
221 if (TREE_CODE (vn->valnum) != SSA_NAME)
222 return vn->valnum;
224 /* Get to the information of the value of this SSA_NAME. */
225 vn = VN_INFO (vn->valnum);
227 /* If the value-number is a constant it is the representative
228 expression. */
229 if (TREE_CODE (vn->valnum) != SSA_NAME)
230 return vn->valnum;
232 /* Else if we have an expression, return it. */
233 if (vn->expr != NULL_TREE)
234 return vn->expr;
236 /* Otherwise use the defining statement to build the expression. */
237 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
239 /* If the value number is not an assignment use it directly. */
240 if (!is_gimple_assign (def_stmt))
241 return vn->valnum;
243 /* FIXME tuples. This is incomplete and likely will miss some
244 simplifications. */
245 code = gimple_assign_rhs_code (def_stmt);
246 switch (TREE_CODE_CLASS (code))
248 case tcc_reference:
249 if ((code == REALPART_EXPR
250 || code == IMAGPART_EXPR
251 || code == VIEW_CONVERT_EXPR)
252 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
253 0)) == SSA_NAME)
254 expr = fold_build1 (code,
255 gimple_expr_type (def_stmt),
256 TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
257 break;
259 case tcc_unary:
260 expr = fold_build1 (code,
261 gimple_expr_type (def_stmt),
262 gimple_assign_rhs1 (def_stmt));
263 break;
265 case tcc_binary:
266 expr = fold_build2 (code,
267 gimple_expr_type (def_stmt),
268 gimple_assign_rhs1 (def_stmt),
269 gimple_assign_rhs2 (def_stmt));
270 break;
272 case tcc_exceptional:
273 if (code == CONSTRUCTOR
274 && TREE_CODE
275 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
276 expr = gimple_assign_rhs1 (def_stmt);
277 break;
279 default:;
281 if (expr == NULL_TREE)
282 return vn->valnum;
284 /* Cache the expression. */
285 vn->expr = expr;
287 return expr;
290 /* Return the vn_kind the expression computed by the stmt should be
291 associated with. */
293 enum vn_kind
294 vn_get_stmt_kind (gimple stmt)
296 switch (gimple_code (stmt))
298 case GIMPLE_CALL:
299 return VN_REFERENCE;
300 case GIMPLE_PHI:
301 return VN_PHI;
302 case GIMPLE_ASSIGN:
304 enum tree_code code = gimple_assign_rhs_code (stmt);
305 tree rhs1 = gimple_assign_rhs1 (stmt);
306 switch (get_gimple_rhs_class (code))
308 case GIMPLE_UNARY_RHS:
309 case GIMPLE_BINARY_RHS:
310 case GIMPLE_TERNARY_RHS:
311 return VN_NARY;
312 case GIMPLE_SINGLE_RHS:
313 switch (TREE_CODE_CLASS (code))
315 case tcc_reference:
316 /* VOP-less references can go through unary case. */
317 if ((code == REALPART_EXPR
318 || code == IMAGPART_EXPR
319 || code == VIEW_CONVERT_EXPR
320 || code == BIT_FIELD_REF)
321 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
322 return VN_NARY;
324 /* Fallthrough. */
325 case tcc_declaration:
326 return VN_REFERENCE;
328 case tcc_constant:
329 return VN_CONSTANT;
331 default:
332 if (code == ADDR_EXPR)
333 return (is_gimple_min_invariant (rhs1)
334 ? VN_CONSTANT : VN_REFERENCE);
335 else if (code == CONSTRUCTOR)
336 return VN_NARY;
337 return VN_NONE;
339 default:
340 return VN_NONE;
343 default:
344 return VN_NONE;
348 /* Free a phi operation structure VP. */
350 static void
351 free_phi (void *vp)
353 vn_phi_t phi = (vn_phi_t) vp;
354 VEC_free (tree, heap, phi->phiargs);
357 /* Free a reference operation structure VP. */
359 static void
360 free_reference (void *vp)
362 vn_reference_t vr = (vn_reference_t) vp;
363 VEC_free (vn_reference_op_s, heap, vr->operands);
366 /* Hash table equality function for vn_constant_t. */
368 static int
369 vn_constant_eq (const void *p1, const void *p2)
371 const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
372 const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
374 if (vc1->hashcode != vc2->hashcode)
375 return false;
377 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
380 /* Hash table hash function for vn_constant_t. */
382 static hashval_t
383 vn_constant_hash (const void *p1)
385 const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
386 return vc1->hashcode;
389 /* Lookup a value id for CONSTANT and return it. If it does not
390 exist returns 0. */
392 unsigned int
393 get_constant_value_id (tree constant)
395 void **slot;
396 struct vn_constant_s vc;
398 vc.hashcode = vn_hash_constant_with_type (constant);
399 vc.constant = constant;
400 slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
401 vc.hashcode, NO_INSERT);
402 if (slot)
403 return ((vn_constant_t)*slot)->value_id;
404 return 0;
407 /* Lookup a value id for CONSTANT, and if it does not exist, create a
408 new one and return it. If it does exist, return it. */
410 unsigned int
411 get_or_alloc_constant_value_id (tree constant)
413 void **slot;
414 struct vn_constant_s vc;
415 vn_constant_t vcp;
417 vc.hashcode = vn_hash_constant_with_type (constant);
418 vc.constant = constant;
419 slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
420 vc.hashcode, INSERT);
421 if (*slot)
422 return ((vn_constant_t)*slot)->value_id;
424 vcp = XNEW (struct vn_constant_s);
425 vcp->hashcode = vc.hashcode;
426 vcp->constant = constant;
427 vcp->value_id = get_next_value_id ();
428 *slot = (void *) vcp;
429 bitmap_set_bit (constant_value_ids, vcp->value_id);
430 return vcp->value_id;
433 /* Return true if V is a value id for a constant. */
435 bool
436 value_id_constant_p (unsigned int v)
438 return bitmap_bit_p (constant_value_ids, v);
441 /* Compare two reference operands P1 and P2 for equality. Return true if
442 they are equal, and false otherwise. */
444 static int
445 vn_reference_op_eq (const void *p1, const void *p2)
447 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
448 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
450 return (vro1->opcode == vro2->opcode
451 /* We do not care for differences in type qualification. */
452 && (vro1->type == vro2->type
453 || (vro1->type && vro2->type
454 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
455 TYPE_MAIN_VARIANT (vro2->type))))
456 && expressions_equal_p (vro1->op0, vro2->op0)
457 && expressions_equal_p (vro1->op1, vro2->op1)
458 && expressions_equal_p (vro1->op2, vro2->op2));
461 /* Compute the hash for a reference operand VRO1. */
463 static hashval_t
464 vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
466 result = iterative_hash_hashval_t (vro1->opcode, result);
467 if (vro1->op0)
468 result = iterative_hash_expr (vro1->op0, result);
469 if (vro1->op1)
470 result = iterative_hash_expr (vro1->op1, result);
471 if (vro1->op2)
472 result = iterative_hash_expr (vro1->op2, result);
473 return result;
476 /* Return the hashcode for a given reference operation P1. */
478 static hashval_t
479 vn_reference_hash (const void *p1)
481 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
482 return vr1->hashcode;
485 /* Compute a hash for the reference operation VR1 and return it. */
487 hashval_t
488 vn_reference_compute_hash (const vn_reference_t vr1)
490 hashval_t result = 0;
491 int i;
492 vn_reference_op_t vro;
493 HOST_WIDE_INT off = -1;
494 bool deref = false;
496 FOR_EACH_VEC_ELT (vn_reference_op_s, vr1->operands, i, vro)
498 if (vro->opcode == MEM_REF)
499 deref = true;
500 else if (vro->opcode != ADDR_EXPR)
501 deref = false;
502 if (vro->off != -1)
504 if (off == -1)
505 off = 0;
506 off += vro->off;
508 else
510 if (off != -1
511 && off != 0)
512 result = iterative_hash_hashval_t (off, result);
513 off = -1;
514 if (deref
515 && vro->opcode == ADDR_EXPR)
517 if (vro->op0)
519 tree op = TREE_OPERAND (vro->op0, 0);
520 result = iterative_hash_hashval_t (TREE_CODE (op), result);
521 result = iterative_hash_expr (op, result);
524 else
525 result = vn_reference_op_compute_hash (vro, result);
528 if (vr1->vuse)
529 result += SSA_NAME_VERSION (vr1->vuse);
531 return result;
534 /* Return true if reference operations P1 and P2 are equivalent. This
535 means they have the same set of operands and vuses. */
538 vn_reference_eq (const void *p1, const void *p2)
540 unsigned i, j;
542 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
543 const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
544 if (vr1->hashcode != vr2->hashcode)
545 return false;
547 /* Early out if this is not a hash collision. */
548 if (vr1->hashcode != vr2->hashcode)
549 return false;
551 /* The VOP needs to be the same. */
552 if (vr1->vuse != vr2->vuse)
553 return false;
555 /* If the operands are the same we are done. */
556 if (vr1->operands == vr2->operands)
557 return true;
559 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
560 return false;
562 if (INTEGRAL_TYPE_P (vr1->type)
563 && INTEGRAL_TYPE_P (vr2->type))
565 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
566 return false;
568 else if (INTEGRAL_TYPE_P (vr1->type)
569 && (TYPE_PRECISION (vr1->type)
570 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
571 return false;
572 else if (INTEGRAL_TYPE_P (vr2->type)
573 && (TYPE_PRECISION (vr2->type)
574 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
575 return false;
577 i = 0;
578 j = 0;
581 HOST_WIDE_INT off1 = 0, off2 = 0;
582 vn_reference_op_t vro1, vro2;
583 vn_reference_op_s tem1, tem2;
584 bool deref1 = false, deref2 = false;
585 for (; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro1); i++)
587 if (vro1->opcode == MEM_REF)
588 deref1 = true;
589 if (vro1->off == -1)
590 break;
591 off1 += vro1->off;
593 for (; VEC_iterate (vn_reference_op_s, vr2->operands, j, vro2); j++)
595 if (vro2->opcode == MEM_REF)
596 deref2 = true;
597 if (vro2->off == -1)
598 break;
599 off2 += vro2->off;
601 if (off1 != off2)
602 return false;
603 if (deref1 && vro1->opcode == ADDR_EXPR)
605 memset (&tem1, 0, sizeof (tem1));
606 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
607 tem1.type = TREE_TYPE (tem1.op0);
608 tem1.opcode = TREE_CODE (tem1.op0);
609 vro1 = &tem1;
610 deref1 = false;
612 if (deref2 && vro2->opcode == ADDR_EXPR)
614 memset (&tem2, 0, sizeof (tem2));
615 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
616 tem2.type = TREE_TYPE (tem2.op0);
617 tem2.opcode = TREE_CODE (tem2.op0);
618 vro2 = &tem2;
619 deref2 = false;
621 if (deref1 != deref2)
622 return false;
623 if (!vn_reference_op_eq (vro1, vro2))
624 return false;
625 ++j;
626 ++i;
628 while (VEC_length (vn_reference_op_s, vr1->operands) != i
629 || VEC_length (vn_reference_op_s, vr2->operands) != j);
631 return true;
634 /* Copy the operations present in load/store REF into RESULT, a vector of
635 vn_reference_op_s's. */
637 void
638 copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
640 if (TREE_CODE (ref) == TARGET_MEM_REF)
642 vn_reference_op_s temp;
644 memset (&temp, 0, sizeof (temp));
645 temp.type = TREE_TYPE (ref);
646 temp.opcode = TREE_CODE (ref);
647 temp.op0 = TMR_INDEX (ref);
648 temp.op1 = TMR_STEP (ref);
649 temp.op2 = TMR_OFFSET (ref);
650 temp.off = -1;
651 VEC_safe_push (vn_reference_op_s, heap, *result, temp);
653 memset (&temp, 0, sizeof (temp));
654 temp.type = NULL_TREE;
655 temp.opcode = ERROR_MARK;
656 temp.op0 = TMR_INDEX2 (ref);
657 temp.off = -1;
658 VEC_safe_push (vn_reference_op_s, heap, *result, temp);
660 memset (&temp, 0, sizeof (temp));
661 temp.type = NULL_TREE;
662 temp.opcode = TREE_CODE (TMR_BASE (ref));
663 temp.op0 = TMR_BASE (ref);
664 temp.off = -1;
665 VEC_safe_push (vn_reference_op_s, heap, *result, temp);
666 return;
669 /* For non-calls, store the information that makes up the address. */
671 while (ref)
673 vn_reference_op_s temp;
675 memset (&temp, 0, sizeof (temp));
676 temp.type = TREE_TYPE (ref);
677 temp.opcode = TREE_CODE (ref);
678 temp.off = -1;
680 switch (temp.opcode)
682 case MODIFY_EXPR:
683 temp.op0 = TREE_OPERAND (ref, 1);
684 break;
685 case WITH_SIZE_EXPR:
686 temp.op0 = TREE_OPERAND (ref, 1);
687 temp.off = 0;
688 break;
689 case MEM_REF:
690 /* The base address gets its own vn_reference_op_s structure. */
691 temp.op0 = TREE_OPERAND (ref, 1);
692 if (host_integerp (TREE_OPERAND (ref, 1), 0))
693 temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
694 break;
695 case BIT_FIELD_REF:
696 /* Record bits and position. */
697 temp.op0 = TREE_OPERAND (ref, 1);
698 temp.op1 = TREE_OPERAND (ref, 2);
699 break;
700 case COMPONENT_REF:
701 /* The field decl is enough to unambiguously specify the field,
702 a matching type is not necessary and a mismatching type
703 is always a spurious difference. */
704 temp.type = NULL_TREE;
705 temp.op0 = TREE_OPERAND (ref, 1);
706 temp.op1 = TREE_OPERAND (ref, 2);
708 tree this_offset = component_ref_field_offset (ref);
709 if (this_offset
710 && TREE_CODE (this_offset) == INTEGER_CST)
712 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
713 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
715 double_int off
716 = tree_to_double_int (this_offset)
717 + tree_to_double_int (bit_offset)
718 .arshift (BITS_PER_UNIT == 8
719 ? 3 : exact_log2 (BITS_PER_UNIT),
720 HOST_BITS_PER_DOUBLE_INT);
721 if (off.fits_shwi ())
722 temp.off = off.low;
726 break;
727 case ARRAY_RANGE_REF:
728 case ARRAY_REF:
729 /* Record index as operand. */
730 temp.op0 = TREE_OPERAND (ref, 1);
731 /* Always record lower bounds and element size. */
732 temp.op1 = array_ref_low_bound (ref);
733 temp.op2 = array_ref_element_size (ref);
734 if (TREE_CODE (temp.op0) == INTEGER_CST
735 && TREE_CODE (temp.op1) == INTEGER_CST
736 && TREE_CODE (temp.op2) == INTEGER_CST)
738 double_int off = tree_to_double_int (temp.op0);
739 off += -tree_to_double_int (temp.op1);
740 off *= tree_to_double_int (temp.op2);
741 if (off.fits_shwi ())
742 temp.off = off.low;
744 break;
745 case VAR_DECL:
746 if (DECL_HARD_REGISTER (ref))
748 temp.op0 = ref;
749 break;
751 /* Fallthru. */
752 case PARM_DECL:
753 case CONST_DECL:
754 case RESULT_DECL:
755 /* Canonicalize decls to MEM[&decl] which is what we end up with
756 when valueizing MEM[ptr] with ptr = &decl. */
757 temp.opcode = MEM_REF;
758 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
759 temp.off = 0;
760 VEC_safe_push (vn_reference_op_s, heap, *result, temp);
761 temp.opcode = ADDR_EXPR;
762 temp.op0 = build_fold_addr_expr (ref);
763 temp.type = TREE_TYPE (temp.op0);
764 temp.off = -1;
765 break;
766 case STRING_CST:
767 case INTEGER_CST:
768 case COMPLEX_CST:
769 case VECTOR_CST:
770 case REAL_CST:
771 case FIXED_CST:
772 case CONSTRUCTOR:
773 case SSA_NAME:
774 temp.op0 = ref;
775 break;
776 case ADDR_EXPR:
777 if (is_gimple_min_invariant (ref))
779 temp.op0 = ref;
780 break;
782 /* Fallthrough. */
783 /* These are only interesting for their operands, their
784 existence, and their type. They will never be the last
785 ref in the chain of references (IE they require an
786 operand), so we don't have to put anything
787 for op* as it will be handled by the iteration */
788 case REALPART_EXPR:
789 case VIEW_CONVERT_EXPR:
790 temp.off = 0;
791 break;
792 case IMAGPART_EXPR:
793 /* This is only interesting for its constant offset. */
794 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
795 break;
796 default:
797 gcc_unreachable ();
799 VEC_safe_push (vn_reference_op_s, heap, *result, temp);
801 if (REFERENCE_CLASS_P (ref)
802 || TREE_CODE (ref) == MODIFY_EXPR
803 || TREE_CODE (ref) == WITH_SIZE_EXPR
804 || (TREE_CODE (ref) == ADDR_EXPR
805 && !is_gimple_min_invariant (ref)))
806 ref = TREE_OPERAND (ref, 0);
807 else
808 ref = NULL_TREE;
812 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
813 operands in *OPS, the reference alias set SET and the reference type TYPE.
814 Return true if something useful was produced. */
816 bool
817 ao_ref_init_from_vn_reference (ao_ref *ref,
818 alias_set_type set, tree type,
819 VEC (vn_reference_op_s, heap) *ops)
821 vn_reference_op_t op;
822 unsigned i;
823 tree base = NULL_TREE;
824 tree *op0_p = &base;
825 HOST_WIDE_INT offset = 0;
826 HOST_WIDE_INT max_size;
827 HOST_WIDE_INT size = -1;
828 tree size_tree = NULL_TREE;
829 alias_set_type base_alias_set = -1;
831 /* First get the final access size from just the outermost expression. */
832 op = &VEC_index (vn_reference_op_s, ops, 0);
833 if (op->opcode == COMPONENT_REF)
834 size_tree = DECL_SIZE (op->op0);
835 else if (op->opcode == BIT_FIELD_REF)
836 size_tree = op->op0;
837 else
839 enum machine_mode mode = TYPE_MODE (type);
840 if (mode == BLKmode)
841 size_tree = TYPE_SIZE (type);
842 else
843 size = GET_MODE_BITSIZE (mode);
845 if (size_tree != NULL_TREE)
847 if (!host_integerp (size_tree, 1))
848 size = -1;
849 else
850 size = TREE_INT_CST_LOW (size_tree);
853 /* Initially, maxsize is the same as the accessed element size.
854 In the following it will only grow (or become -1). */
855 max_size = size;
857 /* Compute cumulative bit-offset for nested component-refs and array-refs,
858 and find the ultimate containing object. */
859 FOR_EACH_VEC_ELT (vn_reference_op_s, ops, i, op)
861 switch (op->opcode)
863 /* These may be in the reference ops, but we cannot do anything
864 sensible with them here. */
865 case ADDR_EXPR:
866 /* Apart from ADDR_EXPR arguments to MEM_REF. */
867 if (base != NULL_TREE
868 && TREE_CODE (base) == MEM_REF
869 && op->op0
870 && DECL_P (TREE_OPERAND (op->op0, 0)))
872 vn_reference_op_t pop = &VEC_index (vn_reference_op_s, ops, i-1);
873 base = TREE_OPERAND (op->op0, 0);
874 if (pop->off == -1)
876 max_size = -1;
877 offset = 0;
879 else
880 offset += pop->off * BITS_PER_UNIT;
881 op0_p = NULL;
882 break;
884 /* Fallthru. */
885 case CALL_EXPR:
886 return false;
888 /* Record the base objects. */
889 case MEM_REF:
890 base_alias_set = get_deref_alias_set (op->op0);
891 *op0_p = build2 (MEM_REF, op->type,
892 NULL_TREE, op->op0);
893 op0_p = &TREE_OPERAND (*op0_p, 0);
894 break;
896 case VAR_DECL:
897 case PARM_DECL:
898 case RESULT_DECL:
899 case SSA_NAME:
900 *op0_p = op->op0;
901 op0_p = NULL;
902 break;
904 /* And now the usual component-reference style ops. */
905 case BIT_FIELD_REF:
906 offset += tree_low_cst (op->op1, 0);
907 break;
909 case COMPONENT_REF:
911 tree field = op->op0;
912 /* We do not have a complete COMPONENT_REF tree here so we
913 cannot use component_ref_field_offset. Do the interesting
914 parts manually. */
916 if (op->op1
917 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
918 max_size = -1;
919 else
921 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
922 * BITS_PER_UNIT);
923 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
925 break;
928 case ARRAY_RANGE_REF:
929 case ARRAY_REF:
930 /* We recorded the lower bound and the element size. */
931 if (!host_integerp (op->op0, 0)
932 || !host_integerp (op->op1, 0)
933 || !host_integerp (op->op2, 0))
934 max_size = -1;
935 else
937 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
938 hindex -= TREE_INT_CST_LOW (op->op1);
939 hindex *= TREE_INT_CST_LOW (op->op2);
940 hindex *= BITS_PER_UNIT;
941 offset += hindex;
943 break;
945 case REALPART_EXPR:
946 break;
948 case IMAGPART_EXPR:
949 offset += size;
950 break;
952 case VIEW_CONVERT_EXPR:
953 break;
955 case STRING_CST:
956 case INTEGER_CST:
957 case COMPLEX_CST:
958 case VECTOR_CST:
959 case REAL_CST:
960 case CONSTRUCTOR:
961 case CONST_DECL:
962 return false;
964 default:
965 return false;
969 if (base == NULL_TREE)
970 return false;
972 ref->ref = NULL_TREE;
973 ref->base = base;
974 ref->offset = offset;
975 ref->size = size;
976 ref->max_size = max_size;
977 ref->ref_alias_set = set;
978 if (base_alias_set != -1)
979 ref->base_alias_set = base_alias_set;
980 else
981 ref->base_alias_set = get_alias_set (base);
982 /* We discount volatiles from value-numbering elsewhere. */
983 ref->volatile_p = false;
985 return true;
988 /* Copy the operations present in load/store/call REF into RESULT, a vector of
989 vn_reference_op_s's. */
991 void
992 copy_reference_ops_from_call (gimple call,
993 VEC(vn_reference_op_s, heap) **result)
995 vn_reference_op_s temp;
996 unsigned i;
997 tree lhs = gimple_call_lhs (call);
999 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1000 different. By adding the lhs here in the vector, we ensure that the
1001 hashcode is different, guaranteeing a different value number. */
1002 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1004 memset (&temp, 0, sizeof (temp));
1005 temp.opcode = MODIFY_EXPR;
1006 temp.type = TREE_TYPE (lhs);
1007 temp.op0 = lhs;
1008 temp.off = -1;
1009 VEC_safe_push (vn_reference_op_s, heap, *result, temp);
1012 /* Copy the type, opcode, function being called and static chain. */
1013 memset (&temp, 0, sizeof (temp));
1014 temp.type = gimple_call_return_type (call);
1015 temp.opcode = CALL_EXPR;
1016 temp.op0 = gimple_call_fn (call);
1017 temp.op1 = gimple_call_chain (call);
1018 temp.off = -1;
1019 VEC_safe_push (vn_reference_op_s, heap, *result, temp);
1021 /* Copy the call arguments. As they can be references as well,
1022 just chain them together. */
1023 for (i = 0; i < gimple_call_num_args (call); ++i)
1025 tree callarg = gimple_call_arg (call, i);
1026 copy_reference_ops_from_ref (callarg, result);
1030 /* Create a vector of vn_reference_op_s structures from REF, a
1031 REFERENCE_CLASS_P tree. The vector is not shared. */
1033 static VEC(vn_reference_op_s, heap) *
1034 create_reference_ops_from_ref (tree ref)
1036 VEC (vn_reference_op_s, heap) *result = NULL;
1038 copy_reference_ops_from_ref (ref, &result);
1039 return result;
1042 /* Create a vector of vn_reference_op_s structures from CALL, a
1043 call statement. The vector is not shared. */
1045 static VEC(vn_reference_op_s, heap) *
1046 create_reference_ops_from_call (gimple call)
1048 VEC (vn_reference_op_s, heap) *result = NULL;
1050 copy_reference_ops_from_call (call, &result);
1051 return result;
1054 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1055 *I_P to point to the last element of the replacement. */
1056 void
1057 vn_reference_fold_indirect (VEC (vn_reference_op_s, heap) **ops,
1058 unsigned int *i_p)
1060 unsigned int i = *i_p;
1061 vn_reference_op_t op = &VEC_index (vn_reference_op_s, *ops, i);
1062 vn_reference_op_t mem_op = &VEC_index (vn_reference_op_s, *ops, i - 1);
1063 tree addr_base;
1064 HOST_WIDE_INT addr_offset = 0;
1066 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1067 from .foo.bar to the preceding MEM_REF offset and replace the
1068 address with &OBJ. */
1069 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1070 &addr_offset);
1071 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1072 if (addr_base != op->op0)
1074 double_int off = tree_to_double_int (mem_op->op0);
1075 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1076 off += double_int::from_shwi (addr_offset);
1077 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1078 op->op0 = build_fold_addr_expr (addr_base);
1079 if (host_integerp (mem_op->op0, 0))
1080 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1081 else
1082 mem_op->off = -1;
1086 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1087 *I_P to point to the last element of the replacement. */
1088 static void
1089 vn_reference_maybe_forwprop_address (VEC (vn_reference_op_s, heap) **ops,
1090 unsigned int *i_p)
1092 unsigned int i = *i_p;
1093 vn_reference_op_t op = &VEC_index (vn_reference_op_s, *ops, i);
1094 vn_reference_op_t mem_op = &VEC_index (vn_reference_op_s, *ops, i - 1);
1095 gimple def_stmt;
1096 enum tree_code code;
1097 double_int off;
1099 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1100 if (!is_gimple_assign (def_stmt))
1101 return;
1103 code = gimple_assign_rhs_code (def_stmt);
1104 if (code != ADDR_EXPR
1105 && code != POINTER_PLUS_EXPR)
1106 return;
1108 off = tree_to_double_int (mem_op->op0);
1109 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1111 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1112 from .foo.bar to the preceding MEM_REF offset and replace the
1113 address with &OBJ. */
1114 if (code == ADDR_EXPR)
1116 tree addr, addr_base;
1117 HOST_WIDE_INT addr_offset;
1119 addr = gimple_assign_rhs1 (def_stmt);
1120 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1121 &addr_offset);
1122 if (!addr_base
1123 || TREE_CODE (addr_base) != MEM_REF)
1124 return;
1126 off += double_int::from_shwi (addr_offset);
1127 off += mem_ref_offset (addr_base);
1128 op->op0 = TREE_OPERAND (addr_base, 0);
1130 else
1132 tree ptr, ptroff;
1133 ptr = gimple_assign_rhs1 (def_stmt);
1134 ptroff = gimple_assign_rhs2 (def_stmt);
1135 if (TREE_CODE (ptr) != SSA_NAME
1136 || TREE_CODE (ptroff) != INTEGER_CST)
1137 return;
1139 off += tree_to_double_int (ptroff);
1140 op->op0 = ptr;
1143 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1144 if (host_integerp (mem_op->op0, 0))
1145 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1146 else
1147 mem_op->off = -1;
1148 if (TREE_CODE (op->op0) == SSA_NAME)
1149 op->op0 = SSA_VAL (op->op0);
1150 if (TREE_CODE (op->op0) != SSA_NAME)
1151 op->opcode = TREE_CODE (op->op0);
1153 /* And recurse. */
1154 if (TREE_CODE (op->op0) == SSA_NAME)
1155 vn_reference_maybe_forwprop_address (ops, i_p);
1156 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1157 vn_reference_fold_indirect (ops, i_p);
1160 /* Optimize the reference REF to a constant if possible or return
1161 NULL_TREE if not. */
1163 tree
1164 fully_constant_vn_reference_p (vn_reference_t ref)
1166 VEC (vn_reference_op_s, heap) *operands = ref->operands;
1167 vn_reference_op_t op;
1169 /* Try to simplify the translated expression if it is
1170 a call to a builtin function with at most two arguments. */
1171 op = &VEC_index (vn_reference_op_s, operands, 0);
1172 if (op->opcode == CALL_EXPR
1173 && TREE_CODE (op->op0) == ADDR_EXPR
1174 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1175 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1176 && VEC_length (vn_reference_op_s, operands) >= 2
1177 && VEC_length (vn_reference_op_s, operands) <= 3)
1179 vn_reference_op_t arg0, arg1 = NULL;
1180 bool anyconst = false;
1181 arg0 = &VEC_index (vn_reference_op_s, operands, 1);
1182 if (VEC_length (vn_reference_op_s, operands) > 2)
1183 arg1 = &VEC_index (vn_reference_op_s, operands, 2);
1184 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1185 || (arg0->opcode == ADDR_EXPR
1186 && is_gimple_min_invariant (arg0->op0)))
1187 anyconst = true;
1188 if (arg1
1189 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1190 || (arg1->opcode == ADDR_EXPR
1191 && is_gimple_min_invariant (arg1->op0))))
1192 anyconst = true;
1193 if (anyconst)
1195 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1196 arg1 ? 2 : 1,
1197 arg0->op0,
1198 arg1 ? arg1->op0 : NULL);
1199 if (folded
1200 && TREE_CODE (folded) == NOP_EXPR)
1201 folded = TREE_OPERAND (folded, 0);
1202 if (folded
1203 && is_gimple_min_invariant (folded))
1204 return folded;
1208 /* Simplify reads from constant strings. */
1209 else if (op->opcode == ARRAY_REF
1210 && TREE_CODE (op->op0) == INTEGER_CST
1211 && integer_zerop (op->op1)
1212 && VEC_length (vn_reference_op_s, operands) == 2)
1214 vn_reference_op_t arg0;
1215 arg0 = &VEC_index (vn_reference_op_s, operands, 1);
1216 if (arg0->opcode == STRING_CST
1217 && (TYPE_MODE (op->type)
1218 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1219 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1220 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1221 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1222 return build_int_cst_type (op->type,
1223 (TREE_STRING_POINTER (arg0->op0)
1224 [TREE_INT_CST_LOW (op->op0)]));
1227 return NULL_TREE;
1230 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1231 structures into their value numbers. This is done in-place, and
1232 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1233 whether any operands were valueized. */
1235 static VEC (vn_reference_op_s, heap) *
1236 valueize_refs_1 (VEC (vn_reference_op_s, heap) *orig, bool *valueized_anything)
1238 vn_reference_op_t vro;
1239 unsigned int i;
1241 *valueized_anything = false;
1243 FOR_EACH_VEC_ELT (vn_reference_op_s, orig, i, vro)
1245 if (vro->opcode == SSA_NAME
1246 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1248 tree tem = SSA_VAL (vro->op0);
1249 if (tem != vro->op0)
1251 *valueized_anything = true;
1252 vro->op0 = tem;
1254 /* If it transforms from an SSA_NAME to a constant, update
1255 the opcode. */
1256 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1257 vro->opcode = TREE_CODE (vro->op0);
1259 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1261 tree tem = SSA_VAL (vro->op1);
1262 if (tem != vro->op1)
1264 *valueized_anything = true;
1265 vro->op1 = tem;
1268 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1270 tree tem = SSA_VAL (vro->op2);
1271 if (tem != vro->op2)
1273 *valueized_anything = true;
1274 vro->op2 = tem;
1277 /* If it transforms from an SSA_NAME to an address, fold with
1278 a preceding indirect reference. */
1279 if (i > 0
1280 && vro->op0
1281 && TREE_CODE (vro->op0) == ADDR_EXPR
1282 && VEC_index (vn_reference_op_s,
1283 orig, i - 1).opcode == MEM_REF)
1284 vn_reference_fold_indirect (&orig, &i);
1285 else if (i > 0
1286 && vro->opcode == SSA_NAME
1287 && VEC_index (vn_reference_op_s,
1288 orig, i - 1).opcode == MEM_REF)
1289 vn_reference_maybe_forwprop_address (&orig, &i);
1290 /* If it transforms a non-constant ARRAY_REF into a constant
1291 one, adjust the constant offset. */
1292 else if (vro->opcode == ARRAY_REF
1293 && vro->off == -1
1294 && TREE_CODE (vro->op0) == INTEGER_CST
1295 && TREE_CODE (vro->op1) == INTEGER_CST
1296 && TREE_CODE (vro->op2) == INTEGER_CST)
1298 double_int off = tree_to_double_int (vro->op0);
1299 off += -tree_to_double_int (vro->op1);
1300 off *= tree_to_double_int (vro->op2);
1301 if (off.fits_shwi ())
1302 vro->off = off.low;
1306 return orig;
1309 static VEC (vn_reference_op_s, heap) *
1310 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
1312 bool tem;
1313 return valueize_refs_1 (orig, &tem);
1316 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
1318 /* Create a vector of vn_reference_op_s structures from REF, a
1319 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1320 this function. *VALUEIZED_ANYTHING will specify whether any
1321 operands were valueized. */
1323 static VEC(vn_reference_op_s, heap) *
1324 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1326 if (!ref)
1327 return NULL;
1328 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1329 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1330 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1331 valueized_anything);
1332 return shared_lookup_references;
1335 /* Create a vector of vn_reference_op_s structures from CALL, a
1336 call statement. The vector is shared among all callers of
1337 this function. */
1339 static VEC(vn_reference_op_s, heap) *
1340 valueize_shared_reference_ops_from_call (gimple call)
1342 if (!call)
1343 return NULL;
1344 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1345 copy_reference_ops_from_call (call, &shared_lookup_references);
1346 shared_lookup_references = valueize_refs (shared_lookup_references);
1347 return shared_lookup_references;
1350 /* Lookup a SCCVN reference operation VR in the current hash table.
1351 Returns the resulting value number if it exists in the hash table,
1352 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1353 vn_reference_t stored in the hashtable if something is found. */
1355 static tree
1356 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1358 void **slot;
1359 hashval_t hash;
1361 hash = vr->hashcode;
1362 slot = htab_find_slot_with_hash (current_info->references, vr,
1363 hash, NO_INSERT);
1364 if (!slot && current_info == optimistic_info)
1365 slot = htab_find_slot_with_hash (valid_info->references, vr,
1366 hash, NO_INSERT);
1367 if (slot)
1369 if (vnresult)
1370 *vnresult = (vn_reference_t)*slot;
1371 return ((vn_reference_t)*slot)->result;
1374 return NULL_TREE;
1377 static tree *last_vuse_ptr;
1378 static vn_lookup_kind vn_walk_kind;
1379 static vn_lookup_kind default_vn_walk_kind;
1381 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1382 with the current VUSE and performs the expression lookup. */
1384 static void *
1385 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1386 unsigned int cnt, void *vr_)
1388 vn_reference_t vr = (vn_reference_t)vr_;
1389 void **slot;
1390 hashval_t hash;
1392 /* This bounds the stmt walks we perform on reference lookups
1393 to O(1) instead of O(N) where N is the number of dominating
1394 stores. */
1395 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1396 return (void *)-1;
1398 if (last_vuse_ptr)
1399 *last_vuse_ptr = vuse;
1401 /* Fixup vuse and hash. */
1402 if (vr->vuse)
1403 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1404 vr->vuse = SSA_VAL (vuse);
1405 if (vr->vuse)
1406 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1408 hash = vr->hashcode;
1409 slot = htab_find_slot_with_hash (current_info->references, vr,
1410 hash, NO_INSERT);
1411 if (!slot && current_info == optimistic_info)
1412 slot = htab_find_slot_with_hash (valid_info->references, vr,
1413 hash, NO_INSERT);
1414 if (slot)
1415 return *slot;
1417 return NULL;
1420 /* Lookup an existing or insert a new vn_reference entry into the
1421 value table for the VUSE, SET, TYPE, OPERANDS reference which
1422 has the value VALUE which is either a constant or an SSA name. */
1424 static vn_reference_t
1425 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1426 alias_set_type set,
1427 tree type,
1428 VEC (vn_reference_op_s,
1429 heap) *operands,
1430 tree value)
1432 struct vn_reference_s vr1;
1433 vn_reference_t result;
1434 unsigned value_id;
1435 vr1.vuse = vuse;
1436 vr1.operands = operands;
1437 vr1.type = type;
1438 vr1.set = set;
1439 vr1.hashcode = vn_reference_compute_hash (&vr1);
1440 if (vn_reference_lookup_1 (&vr1, &result))
1441 return result;
1442 if (TREE_CODE (value) == SSA_NAME)
1443 value_id = VN_INFO (value)->value_id;
1444 else
1445 value_id = get_or_alloc_constant_value_id (value);
1446 return vn_reference_insert_pieces (vuse, set, type,
1447 VEC_copy (vn_reference_op_s, heap,
1448 operands), value, value_id);
1451 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1452 from the statement defining VUSE and if not successful tries to
1453 translate *REFP and VR_ through an aggregate copy at the definition
1454 of VUSE. */
1456 static void *
1457 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1459 vn_reference_t vr = (vn_reference_t)vr_;
1460 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1461 tree base;
1462 HOST_WIDE_INT offset, maxsize;
1463 static VEC (vn_reference_op_s, heap) *lhs_ops = NULL;
1464 ao_ref lhs_ref;
1465 bool lhs_ref_ok = false;
1467 /* First try to disambiguate after value-replacing in the definitions LHS. */
1468 if (is_gimple_assign (def_stmt))
1470 VEC (vn_reference_op_s, heap) *tem;
1471 tree lhs = gimple_assign_lhs (def_stmt);
1472 bool valueized_anything = false;
1473 /* Avoid re-allocation overhead. */
1474 VEC_truncate (vn_reference_op_s, lhs_ops, 0);
1475 copy_reference_ops_from_ref (lhs, &lhs_ops);
1476 tem = lhs_ops;
1477 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1478 gcc_assert (lhs_ops == tem);
1479 if (valueized_anything)
1481 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1482 get_alias_set (lhs),
1483 TREE_TYPE (lhs), lhs_ops);
1484 if (lhs_ref_ok
1485 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1486 return NULL;
1488 else
1490 ao_ref_init (&lhs_ref, lhs);
1491 lhs_ref_ok = true;
1495 base = ao_ref_base (ref);
1496 offset = ref->offset;
1497 maxsize = ref->max_size;
1499 /* If we cannot constrain the size of the reference we cannot
1500 test if anything kills it. */
1501 if (maxsize == -1)
1502 return (void *)-1;
1504 /* We can't deduce anything useful from clobbers. */
1505 if (gimple_clobber_p (def_stmt))
1506 return (void *)-1;
1508 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1509 from that definition.
1510 1) Memset. */
1511 if (is_gimple_reg_type (vr->type)
1512 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1513 && integer_zerop (gimple_call_arg (def_stmt, 1))
1514 && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1515 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1517 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1518 tree base2;
1519 HOST_WIDE_INT offset2, size2, maxsize2;
1520 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1521 size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1522 if ((unsigned HOST_WIDE_INT)size2 / 8
1523 == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1524 && maxsize2 != -1
1525 && operand_equal_p (base, base2, 0)
1526 && offset2 <= offset
1527 && offset2 + size2 >= offset + maxsize)
1529 tree val = build_zero_cst (vr->type);
1530 return vn_reference_lookup_or_insert_for_pieces
1531 (vuse, vr->set, vr->type, vr->operands, val);
1535 /* 2) Assignment from an empty CONSTRUCTOR. */
1536 else if (is_gimple_reg_type (vr->type)
1537 && gimple_assign_single_p (def_stmt)
1538 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1539 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1541 tree base2;
1542 HOST_WIDE_INT offset2, size2, maxsize2;
1543 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1544 &offset2, &size2, &maxsize2);
1545 if (maxsize2 != -1
1546 && operand_equal_p (base, base2, 0)
1547 && offset2 <= offset
1548 && offset2 + size2 >= offset + maxsize)
1550 tree val = build_zero_cst (vr->type);
1551 return vn_reference_lookup_or_insert_for_pieces
1552 (vuse, vr->set, vr->type, vr->operands, val);
1556 /* 3) Assignment from a constant. We can use folds native encode/interpret
1557 routines to extract the assigned bits. */
1558 else if (vn_walk_kind == VN_WALKREWRITE
1559 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1560 && ref->size == maxsize
1561 && maxsize % BITS_PER_UNIT == 0
1562 && offset % BITS_PER_UNIT == 0
1563 && is_gimple_reg_type (vr->type)
1564 && gimple_assign_single_p (def_stmt)
1565 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1567 tree base2;
1568 HOST_WIDE_INT offset2, size2, maxsize2;
1569 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1570 &offset2, &size2, &maxsize2);
1571 if (maxsize2 != -1
1572 && maxsize2 == size2
1573 && size2 % BITS_PER_UNIT == 0
1574 && offset2 % BITS_PER_UNIT == 0
1575 && operand_equal_p (base, base2, 0)
1576 && offset2 <= offset
1577 && offset2 + size2 >= offset + maxsize)
1579 /* We support up to 512-bit values (for V8DFmode). */
1580 unsigned char buffer[64];
1581 int len;
1583 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1584 buffer, sizeof (buffer));
1585 if (len > 0)
1587 tree val = native_interpret_expr (vr->type,
1588 buffer
1589 + ((offset - offset2)
1590 / BITS_PER_UNIT),
1591 ref->size / BITS_PER_UNIT);
1592 if (val)
1593 return vn_reference_lookup_or_insert_for_pieces
1594 (vuse, vr->set, vr->type, vr->operands, val);
1599 /* 4) Assignment from an SSA name which definition we may be able
1600 to access pieces from. */
1601 else if (ref->size == maxsize
1602 && is_gimple_reg_type (vr->type)
1603 && gimple_assign_single_p (def_stmt)
1604 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1606 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1607 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1608 if (is_gimple_assign (def_stmt2)
1609 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1610 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1611 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1613 tree base2;
1614 HOST_WIDE_INT offset2, size2, maxsize2, off;
1615 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1616 &offset2, &size2, &maxsize2);
1617 off = offset - offset2;
1618 if (maxsize2 != -1
1619 && maxsize2 == size2
1620 && operand_equal_p (base, base2, 0)
1621 && offset2 <= offset
1622 && offset2 + size2 >= offset + maxsize)
1624 tree val = NULL_TREE;
1625 HOST_WIDE_INT elsz
1626 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1627 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1629 if (off == 0)
1630 val = gimple_assign_rhs1 (def_stmt2);
1631 else if (off == elsz)
1632 val = gimple_assign_rhs2 (def_stmt2);
1634 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1635 && off % elsz == 0)
1637 tree ctor = gimple_assign_rhs1 (def_stmt2);
1638 unsigned i = off / elsz;
1639 if (i < CONSTRUCTOR_NELTS (ctor))
1641 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1642 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1644 if (TREE_CODE (TREE_TYPE (elt->value))
1645 != VECTOR_TYPE)
1646 val = elt->value;
1650 if (val)
1651 return vn_reference_lookup_or_insert_for_pieces
1652 (vuse, vr->set, vr->type, vr->operands, val);
1657 /* 5) For aggregate copies translate the reference through them if
1658 the copy kills ref. */
1659 else if (vn_walk_kind == VN_WALKREWRITE
1660 && gimple_assign_single_p (def_stmt)
1661 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1662 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1663 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1665 tree base2;
1666 HOST_WIDE_INT offset2, size2, maxsize2;
1667 int i, j;
1668 VEC (vn_reference_op_s, heap) *rhs = NULL;
1669 vn_reference_op_t vro;
1670 ao_ref r;
1672 if (!lhs_ref_ok)
1673 return (void *)-1;
1675 /* See if the assignment kills REF. */
1676 base2 = ao_ref_base (&lhs_ref);
1677 offset2 = lhs_ref.offset;
1678 size2 = lhs_ref.size;
1679 maxsize2 = lhs_ref.max_size;
1680 if (maxsize2 == -1
1681 || (base != base2 && !operand_equal_p (base, base2, 0))
1682 || offset2 > offset
1683 || offset2 + size2 < offset + maxsize)
1684 return (void *)-1;
1686 /* Find the common base of ref and the lhs. lhs_ops already
1687 contains valueized operands for the lhs. */
1688 i = VEC_length (vn_reference_op_s, vr->operands) - 1;
1689 j = VEC_length (vn_reference_op_s, lhs_ops) - 1;
1690 while (j >= 0 && i >= 0
1691 && vn_reference_op_eq (&VEC_index (vn_reference_op_s,
1692 vr->operands, i),
1693 &VEC_index (vn_reference_op_s, lhs_ops, j)))
1695 i--;
1696 j--;
1699 /* ??? The innermost op should always be a MEM_REF and we already
1700 checked that the assignment to the lhs kills vr. Thus for
1701 aggregate copies using char[] types the vn_reference_op_eq
1702 may fail when comparing types for compatibility. But we really
1703 don't care here - further lookups with the rewritten operands
1704 will simply fail if we messed up types too badly. */
1705 if (j == 0 && i >= 0
1706 && VEC_index (vn_reference_op_s, lhs_ops, 0).opcode == MEM_REF
1707 && VEC_index (vn_reference_op_s, lhs_ops, 0).off != -1
1708 && (VEC_index (vn_reference_op_s, lhs_ops, 0).off
1709 == VEC_index (vn_reference_op_s, vr->operands, i).off))
1710 i--, j--;
1712 /* i now points to the first additional op.
1713 ??? LHS may not be completely contained in VR, one or more
1714 VIEW_CONVERT_EXPRs could be in its way. We could at least
1715 try handling outermost VIEW_CONVERT_EXPRs. */
1716 if (j != -1)
1717 return (void *)-1;
1719 /* Now re-write REF to be based on the rhs of the assignment. */
1720 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1721 /* We need to pre-pend vr->operands[0..i] to rhs. */
1722 if (i + 1 + VEC_length (vn_reference_op_s, rhs)
1723 > VEC_length (vn_reference_op_s, vr->operands))
1725 VEC (vn_reference_op_s, heap) *old = vr->operands;
1726 VEC_safe_grow (vn_reference_op_s, heap, vr->operands,
1727 i + 1 + VEC_length (vn_reference_op_s, rhs));
1728 if (old == shared_lookup_references
1729 && vr->operands != old)
1730 shared_lookup_references = NULL;
1732 else
1733 VEC_truncate (vn_reference_op_s, vr->operands,
1734 i + 1 + VEC_length (vn_reference_op_s, rhs));
1735 FOR_EACH_VEC_ELT (vn_reference_op_s, rhs, j, vro)
1736 VEC_replace (vn_reference_op_s, vr->operands, i + 1 + j, *vro);
1737 VEC_free (vn_reference_op_s, heap, rhs);
1738 vr->operands = valueize_refs (vr->operands);
1739 vr->hashcode = vn_reference_compute_hash (vr);
1741 /* Adjust *ref from the new operands. */
1742 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1743 return (void *)-1;
1744 /* This can happen with bitfields. */
1745 if (ref->size != r.size)
1746 return (void *)-1;
1747 *ref = r;
1749 /* Do not update last seen VUSE after translating. */
1750 last_vuse_ptr = NULL;
1752 /* Keep looking for the adjusted *REF / VR pair. */
1753 return NULL;
1756 /* 6) For memcpy copies translate the reference through them if
1757 the copy kills ref. */
1758 else if (vn_walk_kind == VN_WALKREWRITE
1759 && is_gimple_reg_type (vr->type)
1760 /* ??? Handle BCOPY as well. */
1761 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1762 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1763 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1764 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1765 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1766 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1767 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1768 && host_integerp (gimple_call_arg (def_stmt, 2), 1))
1770 tree lhs, rhs;
1771 ao_ref r;
1772 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1773 vn_reference_op_s op;
1774 HOST_WIDE_INT at;
1777 /* Only handle non-variable, addressable refs. */
1778 if (ref->size != maxsize
1779 || offset % BITS_PER_UNIT != 0
1780 || ref->size % BITS_PER_UNIT != 0)
1781 return (void *)-1;
1783 /* Extract a pointer base and an offset for the destination. */
1784 lhs = gimple_call_arg (def_stmt, 0);
1785 lhs_offset = 0;
1786 if (TREE_CODE (lhs) == SSA_NAME)
1787 lhs = SSA_VAL (lhs);
1788 if (TREE_CODE (lhs) == ADDR_EXPR)
1790 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1791 &lhs_offset);
1792 if (!tem)
1793 return (void *)-1;
1794 if (TREE_CODE (tem) == MEM_REF
1795 && host_integerp (TREE_OPERAND (tem, 1), 1))
1797 lhs = TREE_OPERAND (tem, 0);
1798 lhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1800 else if (DECL_P (tem))
1801 lhs = build_fold_addr_expr (tem);
1802 else
1803 return (void *)-1;
1805 if (TREE_CODE (lhs) != SSA_NAME
1806 && TREE_CODE (lhs) != ADDR_EXPR)
1807 return (void *)-1;
1809 /* Extract a pointer base and an offset for the source. */
1810 rhs = gimple_call_arg (def_stmt, 1);
1811 rhs_offset = 0;
1812 if (TREE_CODE (rhs) == SSA_NAME)
1813 rhs = SSA_VAL (rhs);
1814 if (TREE_CODE (rhs) == ADDR_EXPR)
1816 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1817 &rhs_offset);
1818 if (!tem)
1819 return (void *)-1;
1820 if (TREE_CODE (tem) == MEM_REF
1821 && host_integerp (TREE_OPERAND (tem, 1), 1))
1823 rhs = TREE_OPERAND (tem, 0);
1824 rhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1826 else if (DECL_P (tem))
1827 rhs = build_fold_addr_expr (tem);
1828 else
1829 return (void *)-1;
1831 if (TREE_CODE (rhs) != SSA_NAME
1832 && TREE_CODE (rhs) != ADDR_EXPR)
1833 return (void *)-1;
1835 copy_size = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2));
1837 /* The bases of the destination and the references have to agree. */
1838 if ((TREE_CODE (base) != MEM_REF
1839 && !DECL_P (base))
1840 || (TREE_CODE (base) == MEM_REF
1841 && (TREE_OPERAND (base, 0) != lhs
1842 || !host_integerp (TREE_OPERAND (base, 1), 1)))
1843 || (DECL_P (base)
1844 && (TREE_CODE (lhs) != ADDR_EXPR
1845 || TREE_OPERAND (lhs, 0) != base)))
1846 return (void *)-1;
1848 /* And the access has to be contained within the memcpy destination. */
1849 at = offset / BITS_PER_UNIT;
1850 if (TREE_CODE (base) == MEM_REF)
1851 at += TREE_INT_CST_LOW (TREE_OPERAND (base, 1));
1852 if (lhs_offset > at
1853 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1854 return (void *)-1;
1856 /* Make room for 2 operands in the new reference. */
1857 if (VEC_length (vn_reference_op_s, vr->operands) < 2)
1859 VEC (vn_reference_op_s, heap) *old = vr->operands;
1860 VEC_safe_grow (vn_reference_op_s, heap, vr->operands, 2);
1861 if (old == shared_lookup_references
1862 && vr->operands != old)
1863 shared_lookup_references = NULL;
1865 else
1866 VEC_truncate (vn_reference_op_s, vr->operands, 2);
1868 /* The looked-through reference is a simple MEM_REF. */
1869 memset (&op, 0, sizeof (op));
1870 op.type = vr->type;
1871 op.opcode = MEM_REF;
1872 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
1873 op.off = at - lhs_offset + rhs_offset;
1874 VEC_replace (vn_reference_op_s, vr->operands, 0, op);
1875 op.type = TREE_TYPE (rhs);
1876 op.opcode = TREE_CODE (rhs);
1877 op.op0 = rhs;
1878 op.off = -1;
1879 VEC_replace (vn_reference_op_s, vr->operands, 1, op);
1880 vr->hashcode = vn_reference_compute_hash (vr);
1882 /* Adjust *ref from the new operands. */
1883 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1884 return (void *)-1;
1885 /* This can happen with bitfields. */
1886 if (ref->size != r.size)
1887 return (void *)-1;
1888 *ref = r;
1890 /* Do not update last seen VUSE after translating. */
1891 last_vuse_ptr = NULL;
1893 /* Keep looking for the adjusted *REF / VR pair. */
1894 return NULL;
1897 /* Bail out and stop walking. */
1898 return (void *)-1;
1901 /* Lookup a reference operation by it's parts, in the current hash table.
1902 Returns the resulting value number if it exists in the hash table,
1903 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1904 vn_reference_t stored in the hashtable if something is found. */
1906 tree
1907 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1908 VEC (vn_reference_op_s, heap) *operands,
1909 vn_reference_t *vnresult, vn_lookup_kind kind)
1911 struct vn_reference_s vr1;
1912 vn_reference_t tmp;
1913 tree cst;
1915 if (!vnresult)
1916 vnresult = &tmp;
1917 *vnresult = NULL;
1919 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1920 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1921 VEC_safe_grow (vn_reference_op_s, heap, shared_lookup_references,
1922 VEC_length (vn_reference_op_s, operands));
1923 memcpy (VEC_address (vn_reference_op_s, shared_lookup_references),
1924 VEC_address (vn_reference_op_s, operands),
1925 sizeof (vn_reference_op_s)
1926 * VEC_length (vn_reference_op_s, operands));
1927 vr1.operands = operands = shared_lookup_references
1928 = valueize_refs (shared_lookup_references);
1929 vr1.type = type;
1930 vr1.set = set;
1931 vr1.hashcode = vn_reference_compute_hash (&vr1);
1932 if ((cst = fully_constant_vn_reference_p (&vr1)))
1933 return cst;
1935 vn_reference_lookup_1 (&vr1, vnresult);
1936 if (!*vnresult
1937 && kind != VN_NOWALK
1938 && vr1.vuse)
1940 ao_ref r;
1941 vn_walk_kind = kind;
1942 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
1943 *vnresult =
1944 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1945 vn_reference_lookup_2,
1946 vn_reference_lookup_3, &vr1);
1947 if (vr1.operands != operands)
1948 VEC_free (vn_reference_op_s, heap, vr1.operands);
1951 if (*vnresult)
1952 return (*vnresult)->result;
1954 return NULL_TREE;
1957 /* Lookup OP in the current hash table, and return the resulting value
1958 number if it exists in the hash table. Return NULL_TREE if it does
1959 not exist in the hash table or if the result field of the structure
1960 was NULL.. VNRESULT will be filled in with the vn_reference_t
1961 stored in the hashtable if one exists. */
1963 tree
1964 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
1965 vn_reference_t *vnresult)
1967 VEC (vn_reference_op_s, heap) *operands;
1968 struct vn_reference_s vr1;
1969 tree cst;
1970 bool valuezied_anything;
1972 if (vnresult)
1973 *vnresult = NULL;
1975 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1976 vr1.operands = operands
1977 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
1978 vr1.type = TREE_TYPE (op);
1979 vr1.set = get_alias_set (op);
1980 vr1.hashcode = vn_reference_compute_hash (&vr1);
1981 if ((cst = fully_constant_vn_reference_p (&vr1)))
1982 return cst;
1984 if (kind != VN_NOWALK
1985 && vr1.vuse)
1987 vn_reference_t wvnresult;
1988 ao_ref r;
1989 /* Make sure to use a valueized reference if we valueized anything.
1990 Otherwise preserve the full reference for advanced TBAA. */
1991 if (!valuezied_anything
1992 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
1993 vr1.operands))
1994 ao_ref_init (&r, op);
1995 vn_walk_kind = kind;
1996 wvnresult =
1997 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1998 vn_reference_lookup_2,
1999 vn_reference_lookup_3, &vr1);
2000 if (vr1.operands != operands)
2001 VEC_free (vn_reference_op_s, heap, vr1.operands);
2002 if (wvnresult)
2004 if (vnresult)
2005 *vnresult = wvnresult;
2006 return wvnresult->result;
2009 return NULL_TREE;
2012 return vn_reference_lookup_1 (&vr1, vnresult);
2016 /* Insert OP into the current hash table with a value number of
2017 RESULT, and return the resulting reference structure we created. */
2019 vn_reference_t
2020 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2022 void **slot;
2023 vn_reference_t vr1;
2025 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2026 if (TREE_CODE (result) == SSA_NAME)
2027 vr1->value_id = VN_INFO (result)->value_id;
2028 else
2029 vr1->value_id = get_or_alloc_constant_value_id (result);
2030 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2031 vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
2032 vr1->type = TREE_TYPE (op);
2033 vr1->set = get_alias_set (op);
2034 vr1->hashcode = vn_reference_compute_hash (vr1);
2035 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2036 vr1->result_vdef = vdef;
2038 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
2039 INSERT);
2041 /* Because we lookup stores using vuses, and value number failures
2042 using the vdefs (see visit_reference_op_store for how and why),
2043 it's possible that on failure we may try to insert an already
2044 inserted store. This is not wrong, there is no ssa name for a
2045 store that we could use as a differentiator anyway. Thus, unlike
2046 the other lookup functions, you cannot gcc_assert (!*slot)
2047 here. */
2049 /* But free the old slot in case of a collision. */
2050 if (*slot)
2051 free_reference (*slot);
2053 *slot = vr1;
2054 return vr1;
2057 /* Insert a reference by it's pieces into the current hash table with
2058 a value number of RESULT. Return the resulting reference
2059 structure we created. */
2061 vn_reference_t
2062 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2063 VEC (vn_reference_op_s, heap) *operands,
2064 tree result, unsigned int value_id)
2067 void **slot;
2068 vn_reference_t vr1;
2070 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2071 vr1->value_id = value_id;
2072 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2073 vr1->operands = valueize_refs (operands);
2074 vr1->type = type;
2075 vr1->set = set;
2076 vr1->hashcode = vn_reference_compute_hash (vr1);
2077 if (result && TREE_CODE (result) == SSA_NAME)
2078 result = SSA_VAL (result);
2079 vr1->result = result;
2081 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
2082 INSERT);
2084 /* At this point we should have all the things inserted that we have
2085 seen before, and we should never try inserting something that
2086 already exists. */
2087 gcc_assert (!*slot);
2088 if (*slot)
2089 free_reference (*slot);
2091 *slot = vr1;
2092 return vr1;
2095 /* Compute and return the hash value for nary operation VBO1. */
2097 hashval_t
2098 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2100 hashval_t hash;
2101 unsigned i;
2103 for (i = 0; i < vno1->length; ++i)
2104 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2105 vno1->op[i] = SSA_VAL (vno1->op[i]);
2107 if (vno1->length == 2
2108 && commutative_tree_code (vno1->opcode)
2109 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2111 tree temp = vno1->op[0];
2112 vno1->op[0] = vno1->op[1];
2113 vno1->op[1] = temp;
2116 hash = iterative_hash_hashval_t (vno1->opcode, 0);
2117 for (i = 0; i < vno1->length; ++i)
2118 hash = iterative_hash_expr (vno1->op[i], hash);
2120 return hash;
2123 /* Return the computed hashcode for nary operation P1. */
2125 static hashval_t
2126 vn_nary_op_hash (const void *p1)
2128 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
2129 return vno1->hashcode;
2132 /* Compare nary operations P1 and P2 and return true if they are
2133 equivalent. */
2136 vn_nary_op_eq (const void *p1, const void *p2)
2138 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
2139 const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
2140 unsigned i;
2142 if (vno1->hashcode != vno2->hashcode)
2143 return false;
2145 if (vno1->length != vno2->length)
2146 return false;
2148 if (vno1->opcode != vno2->opcode
2149 || !types_compatible_p (vno1->type, vno2->type))
2150 return false;
2152 for (i = 0; i < vno1->length; ++i)
2153 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2154 return false;
2156 return true;
2159 /* Initialize VNO from the pieces provided. */
2161 static void
2162 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2163 enum tree_code code, tree type, tree *ops)
2165 vno->opcode = code;
2166 vno->length = length;
2167 vno->type = type;
2168 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2171 /* Initialize VNO from OP. */
2173 static void
2174 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2176 unsigned i;
2178 vno->opcode = TREE_CODE (op);
2179 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2180 vno->type = TREE_TYPE (op);
2181 for (i = 0; i < vno->length; ++i)
2182 vno->op[i] = TREE_OPERAND (op, i);
2185 /* Return the number of operands for a vn_nary ops structure from STMT. */
2187 static unsigned int
2188 vn_nary_length_from_stmt (gimple stmt)
2190 switch (gimple_assign_rhs_code (stmt))
2192 case REALPART_EXPR:
2193 case IMAGPART_EXPR:
2194 case VIEW_CONVERT_EXPR:
2195 return 1;
2197 case BIT_FIELD_REF:
2198 return 3;
2200 case CONSTRUCTOR:
2201 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2203 default:
2204 return gimple_num_ops (stmt) - 1;
2208 /* Initialize VNO from STMT. */
2210 static void
2211 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2213 unsigned i;
2215 vno->opcode = gimple_assign_rhs_code (stmt);
2216 vno->type = gimple_expr_type (stmt);
2217 switch (vno->opcode)
2219 case REALPART_EXPR:
2220 case IMAGPART_EXPR:
2221 case VIEW_CONVERT_EXPR:
2222 vno->length = 1;
2223 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2224 break;
2226 case BIT_FIELD_REF:
2227 vno->length = 3;
2228 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2229 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2230 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2231 break;
2233 case CONSTRUCTOR:
2234 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2235 for (i = 0; i < vno->length; ++i)
2236 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2237 break;
2239 default:
2240 gcc_checking_assert (!gimple_assign_single_p (stmt));
2241 vno->length = gimple_num_ops (stmt) - 1;
2242 for (i = 0; i < vno->length; ++i)
2243 vno->op[i] = gimple_op (stmt, i + 1);
2247 /* Compute the hashcode for VNO and look for it in the hash table;
2248 return the resulting value number if it exists in the hash table.
2249 Return NULL_TREE if it does not exist in the hash table or if the
2250 result field of the operation is NULL. VNRESULT will contain the
2251 vn_nary_op_t from the hashtable if it exists. */
2253 static tree
2254 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2256 void **slot;
2258 if (vnresult)
2259 *vnresult = NULL;
2261 vno->hashcode = vn_nary_op_compute_hash (vno);
2262 slot = htab_find_slot_with_hash (current_info->nary, vno, vno->hashcode,
2263 NO_INSERT);
2264 if (!slot && current_info == optimistic_info)
2265 slot = htab_find_slot_with_hash (valid_info->nary, vno, vno->hashcode,
2266 NO_INSERT);
2267 if (!slot)
2268 return NULL_TREE;
2269 if (vnresult)
2270 *vnresult = (vn_nary_op_t)*slot;
2271 return ((vn_nary_op_t)*slot)->result;
2274 /* Lookup a n-ary operation by its pieces and return the resulting value
2275 number if it exists in the hash table. Return NULL_TREE if it does
2276 not exist in the hash table or if the result field of the operation
2277 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2278 if it exists. */
2280 tree
2281 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2282 tree type, tree *ops, vn_nary_op_t *vnresult)
2284 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2285 sizeof_vn_nary_op (length));
2286 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2287 return vn_nary_op_lookup_1 (vno1, vnresult);
2290 /* Lookup OP in the current hash table, and return the resulting value
2291 number if it exists in the hash table. Return NULL_TREE if it does
2292 not exist in the hash table or if the result field of the operation
2293 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2294 if it exists. */
2296 tree
2297 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2299 vn_nary_op_t vno1
2300 = XALLOCAVAR (struct vn_nary_op_s,
2301 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2302 init_vn_nary_op_from_op (vno1, op);
2303 return vn_nary_op_lookup_1 (vno1, vnresult);
2306 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2307 value number if it exists in the hash table. Return NULL_TREE if
2308 it does not exist in the hash table. VNRESULT will contain the
2309 vn_nary_op_t from the hashtable if it exists. */
2311 tree
2312 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2314 vn_nary_op_t vno1
2315 = XALLOCAVAR (struct vn_nary_op_s,
2316 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2317 init_vn_nary_op_from_stmt (vno1, stmt);
2318 return vn_nary_op_lookup_1 (vno1, vnresult);
2321 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2323 static vn_nary_op_t
2324 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2326 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2329 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2330 obstack. */
2332 static vn_nary_op_t
2333 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2335 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2336 &current_info->nary_obstack);
2338 vno1->value_id = value_id;
2339 vno1->length = length;
2340 vno1->result = result;
2342 return vno1;
2345 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2346 VNO->HASHCODE first. */
2348 static vn_nary_op_t
2349 vn_nary_op_insert_into (vn_nary_op_t vno, htab_t table, bool compute_hash)
2351 void **slot;
2353 if (compute_hash)
2354 vno->hashcode = vn_nary_op_compute_hash (vno);
2356 slot = htab_find_slot_with_hash (table, vno, vno->hashcode, INSERT);
2357 gcc_assert (!*slot);
2359 *slot = vno;
2360 return vno;
2363 /* Insert a n-ary operation into the current hash table using it's
2364 pieces. Return the vn_nary_op_t structure we created and put in
2365 the hashtable. */
2367 vn_nary_op_t
2368 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2369 tree type, tree *ops,
2370 tree result, unsigned int value_id)
2372 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2373 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2374 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2377 /* Insert OP into the current hash table with a value number of
2378 RESULT. Return the vn_nary_op_t structure we created and put in
2379 the hashtable. */
2381 vn_nary_op_t
2382 vn_nary_op_insert (tree op, tree result)
2384 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2385 vn_nary_op_t vno1;
2387 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2388 init_vn_nary_op_from_op (vno1, op);
2389 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2392 /* Insert the rhs of STMT into the current hash table with a value number of
2393 RESULT. */
2395 vn_nary_op_t
2396 vn_nary_op_insert_stmt (gimple stmt, tree result)
2398 vn_nary_op_t vno1
2399 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2400 result, VN_INFO (result)->value_id);
2401 init_vn_nary_op_from_stmt (vno1, stmt);
2402 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2405 /* Compute a hashcode for PHI operation VP1 and return it. */
2407 static inline hashval_t
2408 vn_phi_compute_hash (vn_phi_t vp1)
2410 hashval_t result;
2411 int i;
2412 tree phi1op;
2413 tree type;
2415 result = vp1->block->index;
2417 /* If all PHI arguments are constants we need to distinguish
2418 the PHI node via its type. */
2419 type = TREE_TYPE (VEC_index (tree, vp1->phiargs, 0));
2420 result += (INTEGRAL_TYPE_P (type)
2421 + (INTEGRAL_TYPE_P (type)
2422 ? TYPE_PRECISION (type) + TYPE_UNSIGNED (type) : 0));
2424 FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
2426 if (phi1op == VN_TOP)
2427 continue;
2428 result = iterative_hash_expr (phi1op, result);
2431 return result;
2434 /* Return the computed hashcode for phi operation P1. */
2436 static hashval_t
2437 vn_phi_hash (const void *p1)
2439 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
2440 return vp1->hashcode;
2443 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2445 static int
2446 vn_phi_eq (const void *p1, const void *p2)
2448 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
2449 const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
2451 if (vp1->hashcode != vp2->hashcode)
2452 return false;
2454 if (vp1->block == vp2->block)
2456 int i;
2457 tree phi1op;
2459 /* If the PHI nodes do not have compatible types
2460 they are not the same. */
2461 if (!types_compatible_p (TREE_TYPE (VEC_index (tree, vp1->phiargs, 0)),
2462 TREE_TYPE (VEC_index (tree, vp2->phiargs, 0))))
2463 return false;
2465 /* Any phi in the same block will have it's arguments in the
2466 same edge order, because of how we store phi nodes. */
2467 FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
2469 tree phi2op = VEC_index (tree, vp2->phiargs, i);
2470 if (phi1op == VN_TOP || phi2op == VN_TOP)
2471 continue;
2472 if (!expressions_equal_p (phi1op, phi2op))
2473 return false;
2475 return true;
2477 return false;
2480 static VEC(tree, heap) *shared_lookup_phiargs;
2482 /* Lookup PHI in the current hash table, and return the resulting
2483 value number if it exists in the hash table. Return NULL_TREE if
2484 it does not exist in the hash table. */
2486 static tree
2487 vn_phi_lookup (gimple phi)
2489 void **slot;
2490 struct vn_phi_s vp1;
2491 unsigned i;
2493 VEC_truncate (tree, shared_lookup_phiargs, 0);
2495 /* Canonicalize the SSA_NAME's to their value number. */
2496 for (i = 0; i < gimple_phi_num_args (phi); i++)
2498 tree def = PHI_ARG_DEF (phi, i);
2499 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2500 VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
2502 vp1.phiargs = shared_lookup_phiargs;
2503 vp1.block = gimple_bb (phi);
2504 vp1.hashcode = vn_phi_compute_hash (&vp1);
2505 slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
2506 NO_INSERT);
2507 if (!slot && current_info == optimistic_info)
2508 slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
2509 NO_INSERT);
2510 if (!slot)
2511 return NULL_TREE;
2512 return ((vn_phi_t)*slot)->result;
2515 /* Insert PHI into the current hash table with a value number of
2516 RESULT. */
2518 static vn_phi_t
2519 vn_phi_insert (gimple phi, tree result)
2521 void **slot;
2522 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2523 unsigned i;
2524 VEC (tree, heap) *args = NULL;
2526 /* Canonicalize the SSA_NAME's to their value number. */
2527 for (i = 0; i < gimple_phi_num_args (phi); i++)
2529 tree def = PHI_ARG_DEF (phi, i);
2530 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2531 VEC_safe_push (tree, heap, args, def);
2533 vp1->value_id = VN_INFO (result)->value_id;
2534 vp1->phiargs = args;
2535 vp1->block = gimple_bb (phi);
2536 vp1->result = result;
2537 vp1->hashcode = vn_phi_compute_hash (vp1);
2539 slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
2540 INSERT);
2542 /* Because we iterate over phi operations more than once, it's
2543 possible the slot might already exist here, hence no assert.*/
2544 *slot = vp1;
2545 return vp1;
2549 /* Print set of components in strongly connected component SCC to OUT. */
2551 static void
2552 print_scc (FILE *out, VEC (tree, heap) *scc)
2554 tree var;
2555 unsigned int i;
2557 fprintf (out, "SCC consists of:");
2558 FOR_EACH_VEC_ELT (tree, scc, i, var)
2560 fprintf (out, " ");
2561 print_generic_expr (out, var, 0);
2563 fprintf (out, "\n");
2566 /* Set the value number of FROM to TO, return true if it has changed
2567 as a result. */
2569 static inline bool
2570 set_ssa_val_to (tree from, tree to)
2572 tree currval = SSA_VAL (from);
2574 if (from != to)
2576 if (currval == from)
2578 if (dump_file && (dump_flags & TDF_DETAILS))
2580 fprintf (dump_file, "Not changing value number of ");
2581 print_generic_expr (dump_file, from, 0);
2582 fprintf (dump_file, " from VARYING to ");
2583 print_generic_expr (dump_file, to, 0);
2584 fprintf (dump_file, "\n");
2586 return false;
2588 else if (TREE_CODE (to) == SSA_NAME
2589 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2590 to = from;
2593 /* The only thing we allow as value numbers are VN_TOP, ssa_names
2594 and invariants. So assert that here. */
2595 gcc_assert (to != NULL_TREE
2596 && (to == VN_TOP
2597 || TREE_CODE (to) == SSA_NAME
2598 || is_gimple_min_invariant (to)));
2600 if (dump_file && (dump_flags & TDF_DETAILS))
2602 fprintf (dump_file, "Setting value number of ");
2603 print_generic_expr (dump_file, from, 0);
2604 fprintf (dump_file, " to ");
2605 print_generic_expr (dump_file, to, 0);
2608 if (currval != to && !operand_equal_p (currval, to, OEP_PURE_SAME))
2610 VN_INFO (from)->valnum = to;
2611 if (dump_file && (dump_flags & TDF_DETAILS))
2612 fprintf (dump_file, " (changed)\n");
2613 return true;
2615 if (dump_file && (dump_flags & TDF_DETAILS))
2616 fprintf (dump_file, "\n");
2617 return false;
2620 /* Mark as processed all the definitions in the defining stmt of USE, or
2621 the USE itself. */
2623 static void
2624 mark_use_processed (tree use)
2626 ssa_op_iter iter;
2627 def_operand_p defp;
2628 gimple stmt = SSA_NAME_DEF_STMT (use);
2630 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2632 VN_INFO (use)->use_processed = true;
2633 return;
2636 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2638 tree def = DEF_FROM_PTR (defp);
2640 VN_INFO (def)->use_processed = true;
2644 /* Set all definitions in STMT to value number to themselves.
2645 Return true if a value number changed. */
2647 static bool
2648 defs_to_varying (gimple stmt)
2650 bool changed = false;
2651 ssa_op_iter iter;
2652 def_operand_p defp;
2654 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2656 tree def = DEF_FROM_PTR (defp);
2657 changed |= set_ssa_val_to (def, def);
2659 return changed;
2662 static bool expr_has_constants (tree expr);
2663 static tree valueize_expr (tree expr);
2665 /* Visit a copy between LHS and RHS, return true if the value number
2666 changed. */
2668 static bool
2669 visit_copy (tree lhs, tree rhs)
2671 /* Follow chains of copies to their destination. */
2672 while (TREE_CODE (rhs) == SSA_NAME
2673 && SSA_VAL (rhs) != rhs)
2674 rhs = SSA_VAL (rhs);
2676 /* The copy may have a more interesting constant filled expression
2677 (we don't, since we know our RHS is just an SSA name). */
2678 if (TREE_CODE (rhs) == SSA_NAME)
2680 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2681 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2684 return set_ssa_val_to (lhs, rhs);
2687 /* Visit a nary operator RHS, value number it, and return true if the
2688 value number of LHS has changed as a result. */
2690 static bool
2691 visit_nary_op (tree lhs, gimple stmt)
2693 bool changed = false;
2694 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2696 if (result)
2697 changed = set_ssa_val_to (lhs, result);
2698 else
2700 changed = set_ssa_val_to (lhs, lhs);
2701 vn_nary_op_insert_stmt (stmt, lhs);
2704 return changed;
2707 /* Visit a call STMT storing into LHS. Return true if the value number
2708 of the LHS has changed as a result. */
2710 static bool
2711 visit_reference_op_call (tree lhs, gimple stmt)
2713 bool changed = false;
2714 struct vn_reference_s vr1;
2715 vn_reference_t vnresult = NULL;
2716 tree vuse = gimple_vuse (stmt);
2717 tree vdef = gimple_vdef (stmt);
2719 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2720 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2721 lhs = NULL_TREE;
2723 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2724 vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2725 vr1.type = gimple_expr_type (stmt);
2726 vr1.set = 0;
2727 vr1.hashcode = vn_reference_compute_hash (&vr1);
2728 vn_reference_lookup_1 (&vr1, &vnresult);
2730 if (vnresult)
2732 if (vnresult->result_vdef)
2733 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2735 if (!vnresult->result && lhs)
2736 vnresult->result = lhs;
2738 if (vnresult->result && lhs)
2740 changed |= set_ssa_val_to (lhs, vnresult->result);
2742 if (VN_INFO (vnresult->result)->has_constants)
2743 VN_INFO (lhs)->has_constants = true;
2746 else
2748 void **slot;
2749 vn_reference_t vr2;
2750 if (vdef)
2751 changed |= set_ssa_val_to (vdef, vdef);
2752 if (lhs)
2753 changed |= set_ssa_val_to (lhs, lhs);
2754 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2755 vr2->vuse = vr1.vuse;
2756 vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2757 vr2->type = vr1.type;
2758 vr2->set = vr1.set;
2759 vr2->hashcode = vr1.hashcode;
2760 vr2->result = lhs;
2761 vr2->result_vdef = vdef;
2762 slot = htab_find_slot_with_hash (current_info->references,
2763 vr2, vr2->hashcode, INSERT);
2764 if (*slot)
2765 free_reference (*slot);
2766 *slot = vr2;
2769 return changed;
2772 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2773 and return true if the value number of the LHS has changed as a result. */
2775 static bool
2776 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2778 bool changed = false;
2779 tree last_vuse;
2780 tree result;
2782 last_vuse = gimple_vuse (stmt);
2783 last_vuse_ptr = &last_vuse;
2784 result = vn_reference_lookup (op, gimple_vuse (stmt),
2785 default_vn_walk_kind, NULL);
2786 last_vuse_ptr = NULL;
2788 /* If we have a VCE, try looking up its operand as it might be stored in
2789 a different type. */
2790 if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2791 result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2792 default_vn_walk_kind, NULL);
2794 /* We handle type-punning through unions by value-numbering based
2795 on offset and size of the access. Be prepared to handle a
2796 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2797 if (result
2798 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2800 /* We will be setting the value number of lhs to the value number
2801 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2802 So first simplify and lookup this expression to see if it
2803 is already available. */
2804 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2805 if ((CONVERT_EXPR_P (val)
2806 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2807 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2809 tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2810 if ((CONVERT_EXPR_P (tem)
2811 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2812 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2813 TREE_TYPE (val), tem)))
2814 val = tem;
2816 result = val;
2817 if (!is_gimple_min_invariant (val)
2818 && TREE_CODE (val) != SSA_NAME)
2819 result = vn_nary_op_lookup (val, NULL);
2820 /* If the expression is not yet available, value-number lhs to
2821 a new SSA_NAME we create. */
2822 if (!result)
2824 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
2825 "vntemp");
2826 /* Initialize value-number information properly. */
2827 VN_INFO_GET (result)->valnum = result;
2828 VN_INFO (result)->value_id = get_next_value_id ();
2829 VN_INFO (result)->expr = val;
2830 VN_INFO (result)->has_constants = expr_has_constants (val);
2831 VN_INFO (result)->needs_insertion = true;
2832 /* As all "inserted" statements are singleton SCCs, insert
2833 to the valid table. This is strictly needed to
2834 avoid re-generating new value SSA_NAMEs for the same
2835 expression during SCC iteration over and over (the
2836 optimistic table gets cleared after each iteration).
2837 We do not need to insert into the optimistic table, as
2838 lookups there will fall back to the valid table. */
2839 if (current_info == optimistic_info)
2841 current_info = valid_info;
2842 vn_nary_op_insert (val, result);
2843 current_info = optimistic_info;
2845 else
2846 vn_nary_op_insert (val, result);
2847 if (dump_file && (dump_flags & TDF_DETAILS))
2849 fprintf (dump_file, "Inserting name ");
2850 print_generic_expr (dump_file, result, 0);
2851 fprintf (dump_file, " for expression ");
2852 print_generic_expr (dump_file, val, 0);
2853 fprintf (dump_file, "\n");
2858 if (result)
2860 changed = set_ssa_val_to (lhs, result);
2861 if (TREE_CODE (result) == SSA_NAME
2862 && VN_INFO (result)->has_constants)
2864 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2865 VN_INFO (lhs)->has_constants = true;
2868 else
2870 changed = set_ssa_val_to (lhs, lhs);
2871 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
2874 return changed;
2878 /* Visit a store to a reference operator LHS, part of STMT, value number it,
2879 and return true if the value number of the LHS has changed as a result. */
2881 static bool
2882 visit_reference_op_store (tree lhs, tree op, gimple stmt)
2884 bool changed = false;
2885 vn_reference_t vnresult = NULL;
2886 tree result, assign;
2887 bool resultsame = false;
2888 tree vuse = gimple_vuse (stmt);
2889 tree vdef = gimple_vdef (stmt);
2891 /* First we want to lookup using the *vuses* from the store and see
2892 if there the last store to this location with the same address
2893 had the same value.
2895 The vuses represent the memory state before the store. If the
2896 memory state, address, and value of the store is the same as the
2897 last store to this location, then this store will produce the
2898 same memory state as that store.
2900 In this case the vdef versions for this store are value numbered to those
2901 vuse versions, since they represent the same memory state after
2902 this store.
2904 Otherwise, the vdefs for the store are used when inserting into
2905 the table, since the store generates a new memory state. */
2907 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
2909 if (result)
2911 if (TREE_CODE (result) == SSA_NAME)
2912 result = SSA_VAL (result);
2913 if (TREE_CODE (op) == SSA_NAME)
2914 op = SSA_VAL (op);
2915 resultsame = expressions_equal_p (result, op);
2918 if (!result || !resultsame)
2920 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2921 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
2922 if (vnresult)
2924 VN_INFO (vdef)->use_processed = true;
2925 return set_ssa_val_to (vdef, vnresult->result_vdef);
2929 if (!result || !resultsame)
2931 if (dump_file && (dump_flags & TDF_DETAILS))
2933 fprintf (dump_file, "No store match\n");
2934 fprintf (dump_file, "Value numbering store ");
2935 print_generic_expr (dump_file, lhs, 0);
2936 fprintf (dump_file, " to ");
2937 print_generic_expr (dump_file, op, 0);
2938 fprintf (dump_file, "\n");
2940 /* Have to set value numbers before insert, since insert is
2941 going to valueize the references in-place. */
2942 if (vdef)
2944 changed |= set_ssa_val_to (vdef, vdef);
2947 /* Do not insert structure copies into the tables. */
2948 if (is_gimple_min_invariant (op)
2949 || is_gimple_reg (op))
2950 vn_reference_insert (lhs, op, vdef, NULL);
2952 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2953 vn_reference_insert (assign, lhs, vuse, vdef);
2955 else
2957 /* We had a match, so value number the vdef to have the value
2958 number of the vuse it came from. */
2960 if (dump_file && (dump_flags & TDF_DETAILS))
2961 fprintf (dump_file, "Store matched earlier value,"
2962 "value numbering store vdefs to matching vuses.\n");
2964 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
2967 return changed;
2970 /* Visit and value number PHI, return true if the value number
2971 changed. */
2973 static bool
2974 visit_phi (gimple phi)
2976 bool changed = false;
2977 tree result;
2978 tree sameval = VN_TOP;
2979 bool allsame = true;
2980 unsigned i;
2982 /* TODO: We could check for this in init_sccvn, and replace this
2983 with a gcc_assert. */
2984 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
2985 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2987 /* See if all non-TOP arguments have the same value. TOP is
2988 equivalent to everything, so we can ignore it. */
2989 for (i = 0; i < gimple_phi_num_args (phi); i++)
2991 tree def = PHI_ARG_DEF (phi, i);
2993 if (TREE_CODE (def) == SSA_NAME)
2994 def = SSA_VAL (def);
2995 if (def == VN_TOP)
2996 continue;
2997 if (sameval == VN_TOP)
2999 sameval = def;
3001 else
3003 if (!expressions_equal_p (def, sameval))
3005 allsame = false;
3006 break;
3011 /* If all value numbered to the same value, the phi node has that
3012 value. */
3013 if (allsame)
3015 if (is_gimple_min_invariant (sameval))
3017 VN_INFO (PHI_RESULT (phi))->has_constants = true;
3018 VN_INFO (PHI_RESULT (phi))->expr = sameval;
3020 else
3022 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3023 VN_INFO (PHI_RESULT (phi))->expr = sameval;
3026 if (TREE_CODE (sameval) == SSA_NAME)
3027 return visit_copy (PHI_RESULT (phi), sameval);
3029 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3032 /* Otherwise, see if it is equivalent to a phi node in this block. */
3033 result = vn_phi_lookup (phi);
3034 if (result)
3036 if (TREE_CODE (result) == SSA_NAME)
3037 changed = visit_copy (PHI_RESULT (phi), result);
3038 else
3039 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3041 else
3043 vn_phi_insert (phi, PHI_RESULT (phi));
3044 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3045 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3046 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3049 return changed;
3052 /* Return true if EXPR contains constants. */
3054 static bool
3055 expr_has_constants (tree expr)
3057 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3059 case tcc_unary:
3060 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3062 case tcc_binary:
3063 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3064 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3065 /* Constants inside reference ops are rarely interesting, but
3066 it can take a lot of looking to find them. */
3067 case tcc_reference:
3068 case tcc_declaration:
3069 return false;
3070 default:
3071 return is_gimple_min_invariant (expr);
3073 return false;
3076 /* Return true if STMT contains constants. */
3078 static bool
3079 stmt_has_constants (gimple stmt)
3081 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3082 return false;
3084 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3086 case GIMPLE_UNARY_RHS:
3087 return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
3089 case GIMPLE_BINARY_RHS:
3090 return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
3091 || is_gimple_min_invariant (gimple_assign_rhs2 (stmt)));
3092 case GIMPLE_TERNARY_RHS:
3093 return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
3094 || is_gimple_min_invariant (gimple_assign_rhs2 (stmt))
3095 || is_gimple_min_invariant (gimple_assign_rhs3 (stmt)));
3096 case GIMPLE_SINGLE_RHS:
3097 /* Constants inside reference ops are rarely interesting, but
3098 it can take a lot of looking to find them. */
3099 return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
3100 default:
3101 gcc_unreachable ();
3103 return false;
3106 /* Replace SSA_NAMES in expr with their value numbers, and return the
3107 result.
3108 This is performed in place. */
3110 static tree
3111 valueize_expr (tree expr)
3113 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3115 case tcc_binary:
3116 TREE_OPERAND (expr, 1) = vn_valueize (TREE_OPERAND (expr, 1));
3117 /* Fallthru. */
3118 case tcc_unary:
3119 TREE_OPERAND (expr, 0) = vn_valueize (TREE_OPERAND (expr, 0));
3120 break;
3121 default:;
3123 return expr;
3126 /* Simplify the binary expression RHS, and return the result if
3127 simplified. */
3129 static tree
3130 simplify_binary_expression (gimple stmt)
3132 tree result = NULL_TREE;
3133 tree op0 = gimple_assign_rhs1 (stmt);
3134 tree op1 = gimple_assign_rhs2 (stmt);
3135 enum tree_code code = gimple_assign_rhs_code (stmt);
3137 /* This will not catch every single case we could combine, but will
3138 catch those with constants. The goal here is to simultaneously
3139 combine constants between expressions, but avoid infinite
3140 expansion of expressions during simplification. */
3141 if (TREE_CODE (op0) == SSA_NAME)
3143 if (VN_INFO (op0)->has_constants
3144 || TREE_CODE_CLASS (code) == tcc_comparison
3145 || code == COMPLEX_EXPR)
3146 op0 = valueize_expr (vn_get_expr_for (op0));
3147 else
3148 op0 = vn_valueize (op0);
3151 if (TREE_CODE (op1) == SSA_NAME)
3153 if (VN_INFO (op1)->has_constants
3154 || code == COMPLEX_EXPR)
3155 op1 = valueize_expr (vn_get_expr_for (op1));
3156 else
3157 op1 = vn_valueize (op1);
3160 /* Pointer plus constant can be represented as invariant address.
3161 Do so to allow further propatation, see also tree forwprop. */
3162 if (code == POINTER_PLUS_EXPR
3163 && host_integerp (op1, 1)
3164 && TREE_CODE (op0) == ADDR_EXPR
3165 && is_gimple_min_invariant (op0))
3166 return build_invariant_address (TREE_TYPE (op0),
3167 TREE_OPERAND (op0, 0),
3168 TREE_INT_CST_LOW (op1));
3170 /* Avoid folding if nothing changed. */
3171 if (op0 == gimple_assign_rhs1 (stmt)
3172 && op1 == gimple_assign_rhs2 (stmt))
3173 return NULL_TREE;
3175 fold_defer_overflow_warnings ();
3177 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3178 if (result)
3179 STRIP_USELESS_TYPE_CONVERSION (result);
3181 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3182 stmt, 0);
3184 /* Make sure result is not a complex expression consisting
3185 of operators of operators (IE (a + b) + (a + c))
3186 Otherwise, we will end up with unbounded expressions if
3187 fold does anything at all. */
3188 if (result && valid_gimple_rhs_p (result))
3189 return result;
3191 return NULL_TREE;
3194 /* Simplify the unary expression RHS, and return the result if
3195 simplified. */
3197 static tree
3198 simplify_unary_expression (gimple stmt)
3200 tree result = NULL_TREE;
3201 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3202 enum tree_code code = gimple_assign_rhs_code (stmt);
3204 /* We handle some tcc_reference codes here that are all
3205 GIMPLE_ASSIGN_SINGLE codes. */
3206 if (code == REALPART_EXPR
3207 || code == IMAGPART_EXPR
3208 || code == VIEW_CONVERT_EXPR
3209 || code == BIT_FIELD_REF)
3210 op0 = TREE_OPERAND (op0, 0);
3212 if (TREE_CODE (op0) != SSA_NAME)
3213 return NULL_TREE;
3215 orig_op0 = op0;
3216 if (VN_INFO (op0)->has_constants)
3217 op0 = valueize_expr (vn_get_expr_for (op0));
3218 else if (CONVERT_EXPR_CODE_P (code)
3219 || code == REALPART_EXPR
3220 || code == IMAGPART_EXPR
3221 || code == VIEW_CONVERT_EXPR
3222 || code == BIT_FIELD_REF)
3224 /* We want to do tree-combining on conversion-like expressions.
3225 Make sure we feed only SSA_NAMEs or constants to fold though. */
3226 tree tem = valueize_expr (vn_get_expr_for (op0));
3227 if (UNARY_CLASS_P (tem)
3228 || BINARY_CLASS_P (tem)
3229 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3230 || TREE_CODE (tem) == SSA_NAME
3231 || TREE_CODE (tem) == CONSTRUCTOR
3232 || is_gimple_min_invariant (tem))
3233 op0 = tem;
3236 /* Avoid folding if nothing changed, but remember the expression. */
3237 if (op0 == orig_op0)
3238 return NULL_TREE;
3240 if (code == BIT_FIELD_REF)
3242 tree rhs = gimple_assign_rhs1 (stmt);
3243 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3244 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3246 else
3247 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3248 if (result)
3250 STRIP_USELESS_TYPE_CONVERSION (result);
3251 if (valid_gimple_rhs_p (result))
3252 return result;
3255 return NULL_TREE;
3258 /* Try to simplify RHS using equivalences and constant folding. */
3260 static tree
3261 try_to_simplify (gimple stmt)
3263 enum tree_code code = gimple_assign_rhs_code (stmt);
3264 tree tem;
3266 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3267 in this case, there is no point in doing extra work. */
3268 if (code == SSA_NAME)
3269 return NULL_TREE;
3271 /* First try constant folding based on our current lattice. */
3272 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize);
3273 if (tem
3274 && (TREE_CODE (tem) == SSA_NAME
3275 || is_gimple_min_invariant (tem)))
3276 return tem;
3278 /* If that didn't work try combining multiple statements. */
3279 switch (TREE_CODE_CLASS (code))
3281 case tcc_reference:
3282 /* Fallthrough for some unary codes that can operate on registers. */
3283 if (!(code == REALPART_EXPR
3284 || code == IMAGPART_EXPR
3285 || code == VIEW_CONVERT_EXPR
3286 || code == BIT_FIELD_REF))
3287 break;
3288 /* We could do a little more with unary ops, if they expand
3289 into binary ops, but it's debatable whether it is worth it. */
3290 case tcc_unary:
3291 return simplify_unary_expression (stmt);
3293 case tcc_comparison:
3294 case tcc_binary:
3295 return simplify_binary_expression (stmt);
3297 default:
3298 break;
3301 return NULL_TREE;
3304 /* Visit and value number USE, return true if the value number
3305 changed. */
3307 static bool
3308 visit_use (tree use)
3310 bool changed = false;
3311 gimple stmt = SSA_NAME_DEF_STMT (use);
3313 mark_use_processed (use);
3315 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3316 if (dump_file && (dump_flags & TDF_DETAILS)
3317 && !SSA_NAME_IS_DEFAULT_DEF (use))
3319 fprintf (dump_file, "Value numbering ");
3320 print_generic_expr (dump_file, use, 0);
3321 fprintf (dump_file, " stmt = ");
3322 print_gimple_stmt (dump_file, stmt, 0, 0);
3325 /* Handle uninitialized uses. */
3326 if (SSA_NAME_IS_DEFAULT_DEF (use))
3327 changed = set_ssa_val_to (use, use);
3328 else
3330 if (gimple_code (stmt) == GIMPLE_PHI)
3331 changed = visit_phi (stmt);
3332 else if (gimple_has_volatile_ops (stmt))
3333 changed = defs_to_varying (stmt);
3334 else if (is_gimple_assign (stmt))
3336 enum tree_code code = gimple_assign_rhs_code (stmt);
3337 tree lhs = gimple_assign_lhs (stmt);
3338 tree rhs1 = gimple_assign_rhs1 (stmt);
3339 tree simplified;
3341 /* Shortcut for copies. Simplifying copies is pointless,
3342 since we copy the expression and value they represent. */
3343 if (code == SSA_NAME
3344 && TREE_CODE (lhs) == SSA_NAME)
3346 changed = visit_copy (lhs, rhs1);
3347 goto done;
3349 simplified = try_to_simplify (stmt);
3350 if (simplified)
3352 if (dump_file && (dump_flags & TDF_DETAILS))
3354 fprintf (dump_file, "RHS ");
3355 print_gimple_expr (dump_file, stmt, 0, 0);
3356 fprintf (dump_file, " simplified to ");
3357 print_generic_expr (dump_file, simplified, 0);
3358 if (TREE_CODE (lhs) == SSA_NAME)
3359 fprintf (dump_file, " has constants %d\n",
3360 expr_has_constants (simplified));
3361 else
3362 fprintf (dump_file, "\n");
3365 /* Setting value numbers to constants will occasionally
3366 screw up phi congruence because constants are not
3367 uniquely associated with a single ssa name that can be
3368 looked up. */
3369 if (simplified
3370 && is_gimple_min_invariant (simplified)
3371 && TREE_CODE (lhs) == SSA_NAME)
3373 VN_INFO (lhs)->expr = simplified;
3374 VN_INFO (lhs)->has_constants = true;
3375 changed = set_ssa_val_to (lhs, simplified);
3376 goto done;
3378 else if (simplified
3379 && TREE_CODE (simplified) == SSA_NAME
3380 && TREE_CODE (lhs) == SSA_NAME)
3382 changed = visit_copy (lhs, simplified);
3383 goto done;
3385 else if (simplified)
3387 if (TREE_CODE (lhs) == SSA_NAME)
3389 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3390 /* We have to unshare the expression or else
3391 valuizing may change the IL stream. */
3392 VN_INFO (lhs)->expr = unshare_expr (simplified);
3395 else if (stmt_has_constants (stmt)
3396 && TREE_CODE (lhs) == SSA_NAME)
3397 VN_INFO (lhs)->has_constants = true;
3398 else if (TREE_CODE (lhs) == SSA_NAME)
3400 /* We reset expr and constantness here because we may
3401 have been value numbering optimistically, and
3402 iterating. They may become non-constant in this case,
3403 even if they were optimistically constant. */
3405 VN_INFO (lhs)->has_constants = false;
3406 VN_INFO (lhs)->expr = NULL_TREE;
3409 if ((TREE_CODE (lhs) == SSA_NAME
3410 /* We can substitute SSA_NAMEs that are live over
3411 abnormal edges with their constant value. */
3412 && !(gimple_assign_copy_p (stmt)
3413 && is_gimple_min_invariant (rhs1))
3414 && !(simplified
3415 && is_gimple_min_invariant (simplified))
3416 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3417 /* Stores or copies from SSA_NAMEs that are live over
3418 abnormal edges are a problem. */
3419 || (code == SSA_NAME
3420 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3421 changed = defs_to_varying (stmt);
3422 else if (REFERENCE_CLASS_P (lhs)
3423 || DECL_P (lhs))
3424 changed = visit_reference_op_store (lhs, rhs1, stmt);
3425 else if (TREE_CODE (lhs) == SSA_NAME)
3427 if ((gimple_assign_copy_p (stmt)
3428 && is_gimple_min_invariant (rhs1))
3429 || (simplified
3430 && is_gimple_min_invariant (simplified)))
3432 VN_INFO (lhs)->has_constants = true;
3433 if (simplified)
3434 changed = set_ssa_val_to (lhs, simplified);
3435 else
3436 changed = set_ssa_val_to (lhs, rhs1);
3438 else
3440 switch (vn_get_stmt_kind (stmt))
3442 case VN_NARY:
3443 changed = visit_nary_op (lhs, stmt);
3444 break;
3445 case VN_REFERENCE:
3446 changed = visit_reference_op_load (lhs, rhs1, stmt);
3447 break;
3448 default:
3449 changed = defs_to_varying (stmt);
3450 break;
3454 else
3455 changed = defs_to_varying (stmt);
3457 else if (is_gimple_call (stmt))
3459 tree lhs = gimple_call_lhs (stmt);
3461 /* ??? We could try to simplify calls. */
3463 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3465 if (stmt_has_constants (stmt))
3466 VN_INFO (lhs)->has_constants = true;
3467 else
3469 /* We reset expr and constantness here because we may
3470 have been value numbering optimistically, and
3471 iterating. They may become non-constant in this case,
3472 even if they were optimistically constant. */
3473 VN_INFO (lhs)->has_constants = false;
3474 VN_INFO (lhs)->expr = NULL_TREE;
3477 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3479 changed = defs_to_varying (stmt);
3480 goto done;
3484 if (!gimple_call_internal_p (stmt)
3485 && (/* Calls to the same function with the same vuse
3486 and the same operands do not necessarily return the same
3487 value, unless they're pure or const. */
3488 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3489 /* If calls have a vdef, subsequent calls won't have
3490 the same incoming vuse. So, if 2 calls with vdef have the
3491 same vuse, we know they're not subsequent.
3492 We can value number 2 calls to the same function with the
3493 same vuse and the same operands which are not subsequent
3494 the same, because there is no code in the program that can
3495 compare the 2 values. */
3496 || gimple_vdef (stmt)))
3497 changed = visit_reference_op_call (lhs, stmt);
3498 else
3499 changed = defs_to_varying (stmt);
3501 else
3502 changed = defs_to_varying (stmt);
3504 done:
3505 return changed;
3508 /* Compare two operands by reverse postorder index */
3510 static int
3511 compare_ops (const void *pa, const void *pb)
3513 const tree opa = *((const tree *)pa);
3514 const tree opb = *((const tree *)pb);
3515 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3516 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3517 basic_block bba;
3518 basic_block bbb;
3520 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3521 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3522 else if (gimple_nop_p (opstmta))
3523 return -1;
3524 else if (gimple_nop_p (opstmtb))
3525 return 1;
3527 bba = gimple_bb (opstmta);
3528 bbb = gimple_bb (opstmtb);
3530 if (!bba && !bbb)
3531 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3532 else if (!bba)
3533 return -1;
3534 else if (!bbb)
3535 return 1;
3537 if (bba == bbb)
3539 if (gimple_code (opstmta) == GIMPLE_PHI
3540 && gimple_code (opstmtb) == GIMPLE_PHI)
3541 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3542 else if (gimple_code (opstmta) == GIMPLE_PHI)
3543 return -1;
3544 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3545 return 1;
3546 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3547 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3548 else
3549 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3551 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3554 /* Sort an array containing members of a strongly connected component
3555 SCC so that the members are ordered by RPO number.
3556 This means that when the sort is complete, iterating through the
3557 array will give you the members in RPO order. */
3559 static void
3560 sort_scc (VEC (tree, heap) *scc)
3562 VEC_qsort (tree, scc, compare_ops);
3565 /* Insert the no longer used nary ONARY to the hash INFO. */
3567 static void
3568 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3570 size_t size = sizeof_vn_nary_op (onary->length);
3571 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3572 &info->nary_obstack);
3573 memcpy (nary, onary, size);
3574 vn_nary_op_insert_into (nary, info->nary, false);
3577 /* Insert the no longer used phi OPHI to the hash INFO. */
3579 static void
3580 copy_phi (vn_phi_t ophi, vn_tables_t info)
3582 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3583 void **slot;
3584 memcpy (phi, ophi, sizeof (*phi));
3585 ophi->phiargs = NULL;
3586 slot = htab_find_slot_with_hash (info->phis, phi, phi->hashcode, INSERT);
3587 gcc_assert (!*slot);
3588 *slot = phi;
3591 /* Insert the no longer used reference OREF to the hash INFO. */
3593 static void
3594 copy_reference (vn_reference_t oref, vn_tables_t info)
3596 vn_reference_t ref;
3597 void **slot;
3598 ref = (vn_reference_t) pool_alloc (info->references_pool);
3599 memcpy (ref, oref, sizeof (*ref));
3600 oref->operands = NULL;
3601 slot = htab_find_slot_with_hash (info->references, ref, ref->hashcode,
3602 INSERT);
3603 if (*slot)
3604 free_reference (*slot);
3605 *slot = ref;
3608 /* Process a strongly connected component in the SSA graph. */
3610 static void
3611 process_scc (VEC (tree, heap) *scc)
3613 tree var;
3614 unsigned int i;
3615 unsigned int iterations = 0;
3616 bool changed = true;
3617 htab_iterator hi;
3618 vn_nary_op_t nary;
3619 vn_phi_t phi;
3620 vn_reference_t ref;
3622 /* If the SCC has a single member, just visit it. */
3623 if (VEC_length (tree, scc) == 1)
3625 tree use = VEC_index (tree, scc, 0);
3626 if (VN_INFO (use)->use_processed)
3627 return;
3628 /* We need to make sure it doesn't form a cycle itself, which can
3629 happen for self-referential PHI nodes. In that case we would
3630 end up inserting an expression with VN_TOP operands into the
3631 valid table which makes us derive bogus equivalences later.
3632 The cheapest way to check this is to assume it for all PHI nodes. */
3633 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3634 /* Fallthru to iteration. */ ;
3635 else
3637 visit_use (use);
3638 return;
3642 /* Iterate over the SCC with the optimistic table until it stops
3643 changing. */
3644 current_info = optimistic_info;
3645 while (changed)
3647 changed = false;
3648 iterations++;
3649 if (dump_file && (dump_flags & TDF_DETAILS))
3650 fprintf (dump_file, "Starting iteration %d\n", iterations);
3651 /* As we are value-numbering optimistically we have to
3652 clear the expression tables and the simplified expressions
3653 in each iteration until we converge. */
3654 htab_empty (optimistic_info->nary);
3655 htab_empty (optimistic_info->phis);
3656 htab_empty (optimistic_info->references);
3657 obstack_free (&optimistic_info->nary_obstack, NULL);
3658 gcc_obstack_init (&optimistic_info->nary_obstack);
3659 empty_alloc_pool (optimistic_info->phis_pool);
3660 empty_alloc_pool (optimistic_info->references_pool);
3661 FOR_EACH_VEC_ELT (tree, scc, i, var)
3662 VN_INFO (var)->expr = NULL_TREE;
3663 FOR_EACH_VEC_ELT (tree, scc, i, var)
3664 changed |= visit_use (var);
3667 statistics_histogram_event (cfun, "SCC iterations", iterations);
3669 /* Finally, copy the contents of the no longer used optimistic
3670 table to the valid table. */
3671 FOR_EACH_HTAB_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hi)
3672 copy_nary (nary, valid_info);
3673 FOR_EACH_HTAB_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hi)
3674 copy_phi (phi, valid_info);
3675 FOR_EACH_HTAB_ELEMENT (optimistic_info->references, ref, vn_reference_t, hi)
3676 copy_reference (ref, valid_info);
3678 current_info = valid_info;
3681 DEF_VEC_O(ssa_op_iter);
3682 DEF_VEC_ALLOC_O(ssa_op_iter,heap);
3684 /* Pop the components of the found SCC for NAME off the SCC stack
3685 and process them. Returns true if all went well, false if
3686 we run into resource limits. */
3688 static bool
3689 extract_and_process_scc_for_name (tree name)
3691 VEC (tree, heap) *scc = NULL;
3692 tree x;
3694 /* Found an SCC, pop the components off the SCC stack and
3695 process them. */
3698 x = VEC_pop (tree, sccstack);
3700 VN_INFO (x)->on_sccstack = false;
3701 VEC_safe_push (tree, heap, scc, x);
3702 } while (x != name);
3704 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3705 if (VEC_length (tree, scc)
3706 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3708 if (dump_file)
3709 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3710 "SCC size %u exceeding %u\n", VEC_length (tree, scc),
3711 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3713 VEC_free (tree, heap, scc);
3714 return false;
3717 if (VEC_length (tree, scc) > 1)
3718 sort_scc (scc);
3720 if (dump_file && (dump_flags & TDF_DETAILS))
3721 print_scc (dump_file, scc);
3723 process_scc (scc);
3725 VEC_free (tree, heap, scc);
3727 return true;
3730 /* Depth first search on NAME to discover and process SCC's in the SSA
3731 graph.
3732 Execution of this algorithm relies on the fact that the SCC's are
3733 popped off the stack in topological order.
3734 Returns true if successful, false if we stopped processing SCC's due
3735 to resource constraints. */
3737 static bool
3738 DFS (tree name)
3740 VEC(ssa_op_iter, heap) *itervec = NULL;
3741 VEC(tree, heap) *namevec = NULL;
3742 use_operand_p usep = NULL;
3743 gimple defstmt;
3744 tree use;
3745 ssa_op_iter iter;
3747 start_over:
3748 /* SCC info */
3749 VN_INFO (name)->dfsnum = next_dfs_num++;
3750 VN_INFO (name)->visited = true;
3751 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3753 VEC_safe_push (tree, heap, sccstack, name);
3754 VN_INFO (name)->on_sccstack = true;
3755 defstmt = SSA_NAME_DEF_STMT (name);
3757 /* Recursively DFS on our operands, looking for SCC's. */
3758 if (!gimple_nop_p (defstmt))
3760 /* Push a new iterator. */
3761 if (gimple_code (defstmt) == GIMPLE_PHI)
3762 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3763 else
3764 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3766 else
3767 clear_and_done_ssa_iter (&iter);
3769 while (1)
3771 /* If we are done processing uses of a name, go up the stack
3772 of iterators and process SCCs as we found them. */
3773 if (op_iter_done (&iter))
3775 /* See if we found an SCC. */
3776 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3777 if (!extract_and_process_scc_for_name (name))
3779 VEC_free (tree, heap, namevec);
3780 VEC_free (ssa_op_iter, heap, itervec);
3781 return false;
3784 /* Check if we are done. */
3785 if (VEC_empty (tree, namevec))
3787 VEC_free (tree, heap, namevec);
3788 VEC_free (ssa_op_iter, heap, itervec);
3789 return true;
3792 /* Restore the last use walker and continue walking there. */
3793 use = name;
3794 name = VEC_pop (tree, namevec);
3795 memcpy (&iter, &VEC_last (ssa_op_iter, itervec),
3796 sizeof (ssa_op_iter));
3797 VEC_pop (ssa_op_iter, itervec);
3798 goto continue_walking;
3801 use = USE_FROM_PTR (usep);
3803 /* Since we handle phi nodes, we will sometimes get
3804 invariants in the use expression. */
3805 if (TREE_CODE (use) == SSA_NAME)
3807 if (! (VN_INFO (use)->visited))
3809 /* Recurse by pushing the current use walking state on
3810 the stack and starting over. */
3811 VEC_safe_push(ssa_op_iter, heap, itervec, iter);
3812 VEC_safe_push(tree, heap, namevec, name);
3813 name = use;
3814 goto start_over;
3816 continue_walking:
3817 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3818 VN_INFO (use)->low);
3820 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3821 && VN_INFO (use)->on_sccstack)
3823 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3824 VN_INFO (name)->low);
3828 usep = op_iter_next_use (&iter);
3832 /* Allocate a value number table. */
3834 static void
3835 allocate_vn_table (vn_tables_t table)
3837 table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
3838 table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
3839 table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
3840 free_reference);
3842 gcc_obstack_init (&table->nary_obstack);
3843 table->phis_pool = create_alloc_pool ("VN phis",
3844 sizeof (struct vn_phi_s),
3845 30);
3846 table->references_pool = create_alloc_pool ("VN references",
3847 sizeof (struct vn_reference_s),
3848 30);
3851 /* Free a value number table. */
3853 static void
3854 free_vn_table (vn_tables_t table)
3856 htab_delete (table->phis);
3857 htab_delete (table->nary);
3858 htab_delete (table->references);
3859 obstack_free (&table->nary_obstack, NULL);
3860 free_alloc_pool (table->phis_pool);
3861 free_alloc_pool (table->references_pool);
3864 static void
3865 init_scc_vn (void)
3867 size_t i;
3868 int j;
3869 int *rpo_numbers_temp;
3871 calculate_dominance_info (CDI_DOMINATORS);
3872 sccstack = NULL;
3873 constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
3874 free);
3876 constant_value_ids = BITMAP_ALLOC (NULL);
3878 next_dfs_num = 1;
3879 next_value_id = 1;
3881 vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
3882 /* VEC_alloc doesn't actually grow it to the right size, it just
3883 preallocates the space to do so. */
3884 VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table,
3885 num_ssa_names + 1);
3886 gcc_obstack_init (&vn_ssa_aux_obstack);
3888 shared_lookup_phiargs = NULL;
3889 shared_lookup_references = NULL;
3890 rpo_numbers = XNEWVEC (int, last_basic_block);
3891 rpo_numbers_temp = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
3892 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3894 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3895 the i'th block in RPO order is bb. We want to map bb's to RPO
3896 numbers, so we need to rearrange this array. */
3897 for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3898 rpo_numbers[rpo_numbers_temp[j]] = j;
3900 XDELETE (rpo_numbers_temp);
3902 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3904 /* Create the VN_INFO structures, and initialize value numbers to
3905 TOP. */
3906 for (i = 0; i < num_ssa_names; i++)
3908 tree name = ssa_name (i);
3909 if (name)
3911 VN_INFO_GET (name)->valnum = VN_TOP;
3912 VN_INFO (name)->expr = NULL_TREE;
3913 VN_INFO (name)->value_id = 0;
3917 renumber_gimple_stmt_uids ();
3919 /* Create the valid and optimistic value numbering tables. */
3920 valid_info = XCNEW (struct vn_tables_s);
3921 allocate_vn_table (valid_info);
3922 optimistic_info = XCNEW (struct vn_tables_s);
3923 allocate_vn_table (optimistic_info);
3926 void
3927 free_scc_vn (void)
3929 size_t i;
3931 htab_delete (constant_to_value_id);
3932 BITMAP_FREE (constant_value_ids);
3933 VEC_free (tree, heap, shared_lookup_phiargs);
3934 VEC_free (vn_reference_op_s, heap, shared_lookup_references);
3935 XDELETEVEC (rpo_numbers);
3937 for (i = 0; i < num_ssa_names; i++)
3939 tree name = ssa_name (i);
3940 if (name
3941 && VN_INFO (name)->needs_insertion)
3942 release_ssa_name (name);
3944 obstack_free (&vn_ssa_aux_obstack, NULL);
3945 VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
3947 VEC_free (tree, heap, sccstack);
3948 free_vn_table (valid_info);
3949 XDELETE (valid_info);
3950 free_vn_table (optimistic_info);
3951 XDELETE (optimistic_info);
3954 /* Set *ID if we computed something useful in RESULT. */
3956 static void
3957 set_value_id_for_result (tree result, unsigned int *id)
3959 if (result)
3961 if (TREE_CODE (result) == SSA_NAME)
3962 *id = VN_INFO (result)->value_id;
3963 else if (is_gimple_min_invariant (result))
3964 *id = get_or_alloc_constant_value_id (result);
3968 /* Set the value ids in the valid hash tables. */
3970 static void
3971 set_hashtable_value_ids (void)
3973 htab_iterator hi;
3974 vn_nary_op_t vno;
3975 vn_reference_t vr;
3976 vn_phi_t vp;
3978 /* Now set the value ids of the things we had put in the hash
3979 table. */
3981 FOR_EACH_HTAB_ELEMENT (valid_info->nary,
3982 vno, vn_nary_op_t, hi)
3983 set_value_id_for_result (vno->result, &vno->value_id);
3985 FOR_EACH_HTAB_ELEMENT (valid_info->phis,
3986 vp, vn_phi_t, hi)
3987 set_value_id_for_result (vp->result, &vp->value_id);
3989 FOR_EACH_HTAB_ELEMENT (valid_info->references,
3990 vr, vn_reference_t, hi)
3991 set_value_id_for_result (vr->result, &vr->value_id);
3994 /* Do SCCVN. Returns true if it finished, false if we bailed out
3995 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
3996 how we use the alias oracle walking during the VN process. */
3998 bool
3999 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4001 size_t i;
4002 tree param;
4003 bool changed = true;
4005 default_vn_walk_kind = default_vn_walk_kind_;
4007 init_scc_vn ();
4008 current_info = valid_info;
4010 for (param = DECL_ARGUMENTS (current_function_decl);
4011 param;
4012 param = DECL_CHAIN (param))
4014 tree def = ssa_default_def (cfun, param);
4015 if (def)
4016 VN_INFO (def)->valnum = def;
4019 for (i = 1; i < num_ssa_names; ++i)
4021 tree name = ssa_name (i);
4022 if (name
4023 && VN_INFO (name)->visited == false
4024 && !has_zero_uses (name))
4025 if (!DFS (name))
4027 free_scc_vn ();
4028 return false;
4032 /* Initialize the value ids. */
4034 for (i = 1; i < num_ssa_names; ++i)
4036 tree name = ssa_name (i);
4037 vn_ssa_aux_t info;
4038 if (!name)
4039 continue;
4040 info = VN_INFO (name);
4041 if (info->valnum == name
4042 || info->valnum == VN_TOP)
4043 info->value_id = get_next_value_id ();
4044 else if (is_gimple_min_invariant (info->valnum))
4045 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4048 /* Propagate until they stop changing. */
4049 while (changed)
4051 changed = false;
4052 for (i = 1; i < num_ssa_names; ++i)
4054 tree name = ssa_name (i);
4055 vn_ssa_aux_t info;
4056 if (!name)
4057 continue;
4058 info = VN_INFO (name);
4059 if (TREE_CODE (info->valnum) == SSA_NAME
4060 && info->valnum != name
4061 && info->value_id != VN_INFO (info->valnum)->value_id)
4063 changed = true;
4064 info->value_id = VN_INFO (info->valnum)->value_id;
4069 set_hashtable_value_ids ();
4071 if (dump_file && (dump_flags & TDF_DETAILS))
4073 fprintf (dump_file, "Value numbers:\n");
4074 for (i = 0; i < num_ssa_names; i++)
4076 tree name = ssa_name (i);
4077 if (name
4078 && VN_INFO (name)->visited
4079 && SSA_VAL (name) != name)
4081 print_generic_expr (dump_file, name, 0);
4082 fprintf (dump_file, " = ");
4083 print_generic_expr (dump_file, SSA_VAL (name), 0);
4084 fprintf (dump_file, "\n");
4089 return true;
4092 /* Return the maximum value id we have ever seen. */
4094 unsigned int
4095 get_max_value_id (void)
4097 return next_value_id;
4100 /* Return the next unique value id. */
4102 unsigned int
4103 get_next_value_id (void)
4105 return next_value_id++;
4109 /* Compare two expressions E1 and E2 and return true if they are equal. */
4111 bool
4112 expressions_equal_p (tree e1, tree e2)
4114 /* The obvious case. */
4115 if (e1 == e2)
4116 return true;
4118 /* If only one of them is null, they cannot be equal. */
4119 if (!e1 || !e2)
4120 return false;
4122 /* Now perform the actual comparison. */
4123 if (TREE_CODE (e1) == TREE_CODE (e2)
4124 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4125 return true;
4127 return false;
4131 /* Return true if the nary operation NARY may trap. This is a copy
4132 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4134 bool
4135 vn_nary_may_trap (vn_nary_op_t nary)
4137 tree type;
4138 tree rhs2 = NULL_TREE;
4139 bool honor_nans = false;
4140 bool honor_snans = false;
4141 bool fp_operation = false;
4142 bool honor_trapv = false;
4143 bool handled, ret;
4144 unsigned i;
4146 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4147 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4148 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4150 type = nary->type;
4151 fp_operation = FLOAT_TYPE_P (type);
4152 if (fp_operation)
4154 honor_nans = flag_trapping_math && !flag_finite_math_only;
4155 honor_snans = flag_signaling_nans != 0;
4157 else if (INTEGRAL_TYPE_P (type)
4158 && TYPE_OVERFLOW_TRAPS (type))
4159 honor_trapv = true;
4161 if (nary->length >= 2)
4162 rhs2 = nary->op[1];
4163 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4164 honor_trapv,
4165 honor_nans, honor_snans, rhs2,
4166 &handled);
4167 if (handled
4168 && ret)
4169 return true;
4171 for (i = 0; i < nary->length; ++i)
4172 if (tree_could_trap_p (nary->op[i]))
4173 return true;
4175 return false;