2013-04-03 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blobe7aefbf0551dae86f658e1a2d4cfcf74757993b2
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "basic-block.h"
27 #include "gimple-pretty-print.h"
28 #include "tree-inline.h"
29 #include "tree-flow.h"
30 #include "gimple.h"
31 #include "dumpfile.h"
32 #include "hashtab.h"
33 #include "alloc-pool.h"
34 #include "flags.h"
35 #include "bitmap.h"
36 #include "cfgloop.h"
37 #include "params.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-ssa-sccvn.h"
40 #include "gimple-fold.h"
42 /* This algorithm is based on the SCC algorithm presented by Keith
43 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
44 (http://citeseer.ist.psu.edu/41805.html). In
45 straight line code, it is equivalent to a regular hash based value
46 numbering that is performed in reverse postorder.
48 For code with cycles, there are two alternatives, both of which
49 require keeping the hashtables separate from the actual list of
50 value numbers for SSA names.
52 1. Iterate value numbering in an RPO walk of the blocks, removing
53 all the entries from the hashtable after each iteration (but
54 keeping the SSA name->value number mapping between iterations).
55 Iterate until it does not change.
57 2. Perform value numbering as part of an SCC walk on the SSA graph,
58 iterating only the cycles in the SSA graph until they do not change
59 (using a separate, optimistic hashtable for value numbering the SCC
60 operands).
62 The second is not just faster in practice (because most SSA graph
63 cycles do not involve all the variables in the graph), it also has
64 some nice properties.
66 One of these nice properties is that when we pop an SCC off the
67 stack, we are guaranteed to have processed all the operands coming from
68 *outside of that SCC*, so we do not need to do anything special to
69 ensure they have value numbers.
71 Another nice property is that the SCC walk is done as part of a DFS
72 of the SSA graph, which makes it easy to perform combining and
73 simplifying operations at the same time.
75 The code below is deliberately written in a way that makes it easy
76 to separate the SCC walk from the other work it does.
78 In order to propagate constants through the code, we track which
79 expressions contain constants, and use those while folding. In
80 theory, we could also track expressions whose value numbers are
81 replaced, in case we end up folding based on expression
82 identities.
84 In order to value number memory, we assign value numbers to vuses.
85 This enables us to note that, for example, stores to the same
86 address of the same value from the same starting memory states are
87 equivalent.
88 TODO:
90 1. We can iterate only the changing portions of the SCC's, but
91 I have not seen an SCC big enough for this to be a win.
92 2. If you differentiate between phi nodes for loops and phi nodes
93 for if-then-else, you can properly consider phi nodes in different
94 blocks for equivalence.
95 3. We could value number vuses in more cases, particularly, whole
96 structure copies.
99 /* The set of hashtables and alloc_pool's for their items. */
101 typedef struct vn_tables_s
103 htab_t nary;
104 htab_t phis;
105 htab_t references;
106 struct obstack nary_obstack;
107 alloc_pool phis_pool;
108 alloc_pool references_pool;
109 } *vn_tables_t;
111 static htab_t constant_to_value_id;
112 static bitmap constant_value_ids;
115 /* Valid hashtables storing information we have proven to be
116 correct. */
118 static vn_tables_t valid_info;
120 /* Optimistic hashtables storing information we are making assumptions about
121 during iterations. */
123 static vn_tables_t optimistic_info;
125 /* Pointer to the set of hashtables that is currently being used.
126 Should always point to either the optimistic_info, or the
127 valid_info. */
129 static vn_tables_t current_info;
132 /* Reverse post order index for each basic block. */
134 static int *rpo_numbers;
136 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
138 /* This represents the top of the VN lattice, which is the universal
139 value. */
141 tree VN_TOP;
143 /* Unique counter for our value ids. */
145 static unsigned int next_value_id;
147 /* Next DFS number and the stack for strongly connected component
148 detection. */
150 static unsigned int next_dfs_num;
151 static vec<tree> sccstack;
155 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
156 are allocated on an obstack for locality reasons, and to free them
157 without looping over the vec. */
159 static vec<vn_ssa_aux_t> vn_ssa_aux_table;
160 static struct obstack vn_ssa_aux_obstack;
162 /* Return the value numbering information for a given SSA name. */
164 vn_ssa_aux_t
165 VN_INFO (tree name)
167 vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)];
168 gcc_checking_assert (res);
169 return res;
172 /* Set the value numbering info for a given SSA name to a given
173 value. */
175 static inline void
176 VN_INFO_SET (tree name, vn_ssa_aux_t value)
178 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value;
181 /* Initialize the value numbering info for a given SSA name.
182 This should be called just once for every SSA name. */
184 vn_ssa_aux_t
185 VN_INFO_GET (tree name)
187 vn_ssa_aux_t newinfo;
189 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
190 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
191 if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ())
192 vn_ssa_aux_table.safe_grow (SSA_NAME_VERSION (name) + 1);
193 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo;
194 return newinfo;
198 /* Get the representative expression for the SSA_NAME NAME. Returns
199 the representative SSA_NAME if there is no expression associated with it. */
201 tree
202 vn_get_expr_for (tree name)
204 vn_ssa_aux_t vn = VN_INFO (name);
205 gimple def_stmt;
206 tree expr = NULL_TREE;
207 enum tree_code code;
209 if (vn->valnum == VN_TOP)
210 return name;
212 /* If the value-number is a constant it is the representative
213 expression. */
214 if (TREE_CODE (vn->valnum) != SSA_NAME)
215 return vn->valnum;
217 /* Get to the information of the value of this SSA_NAME. */
218 vn = VN_INFO (vn->valnum);
220 /* If the value-number is a constant it is the representative
221 expression. */
222 if (TREE_CODE (vn->valnum) != SSA_NAME)
223 return vn->valnum;
225 /* Else if we have an expression, return it. */
226 if (vn->expr != NULL_TREE)
227 return vn->expr;
229 /* Otherwise use the defining statement to build the expression. */
230 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
232 /* If the value number is not an assignment use it directly. */
233 if (!is_gimple_assign (def_stmt))
234 return vn->valnum;
236 /* FIXME tuples. This is incomplete and likely will miss some
237 simplifications. */
238 code = gimple_assign_rhs_code (def_stmt);
239 switch (TREE_CODE_CLASS (code))
241 case tcc_reference:
242 if ((code == REALPART_EXPR
243 || code == IMAGPART_EXPR
244 || code == VIEW_CONVERT_EXPR)
245 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
246 0)) == SSA_NAME)
247 expr = fold_build1 (code,
248 gimple_expr_type (def_stmt),
249 TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
250 break;
252 case tcc_unary:
253 expr = fold_build1 (code,
254 gimple_expr_type (def_stmt),
255 gimple_assign_rhs1 (def_stmt));
256 break;
258 case tcc_binary:
259 expr = fold_build2 (code,
260 gimple_expr_type (def_stmt),
261 gimple_assign_rhs1 (def_stmt),
262 gimple_assign_rhs2 (def_stmt));
263 break;
265 case tcc_exceptional:
266 if (code == CONSTRUCTOR
267 && TREE_CODE
268 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
269 expr = gimple_assign_rhs1 (def_stmt);
270 break;
272 default:;
274 if (expr == NULL_TREE)
275 return vn->valnum;
277 /* Cache the expression. */
278 vn->expr = expr;
280 return expr;
283 /* Return the vn_kind the expression computed by the stmt should be
284 associated with. */
286 enum vn_kind
287 vn_get_stmt_kind (gimple stmt)
289 switch (gimple_code (stmt))
291 case GIMPLE_CALL:
292 return VN_REFERENCE;
293 case GIMPLE_PHI:
294 return VN_PHI;
295 case GIMPLE_ASSIGN:
297 enum tree_code code = gimple_assign_rhs_code (stmt);
298 tree rhs1 = gimple_assign_rhs1 (stmt);
299 switch (get_gimple_rhs_class (code))
301 case GIMPLE_UNARY_RHS:
302 case GIMPLE_BINARY_RHS:
303 case GIMPLE_TERNARY_RHS:
304 return VN_NARY;
305 case GIMPLE_SINGLE_RHS:
306 switch (TREE_CODE_CLASS (code))
308 case tcc_reference:
309 /* VOP-less references can go through unary case. */
310 if ((code == REALPART_EXPR
311 || code == IMAGPART_EXPR
312 || code == VIEW_CONVERT_EXPR
313 || code == BIT_FIELD_REF)
314 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
315 return VN_NARY;
317 /* Fallthrough. */
318 case tcc_declaration:
319 return VN_REFERENCE;
321 case tcc_constant:
322 return VN_CONSTANT;
324 default:
325 if (code == ADDR_EXPR)
326 return (is_gimple_min_invariant (rhs1)
327 ? VN_CONSTANT : VN_REFERENCE);
328 else if (code == CONSTRUCTOR)
329 return VN_NARY;
330 return VN_NONE;
332 default:
333 return VN_NONE;
336 default:
337 return VN_NONE;
341 /* Free a phi operation structure VP. */
343 static void
344 free_phi (void *vp)
346 vn_phi_t phi = (vn_phi_t) vp;
347 phi->phiargs.release ();
350 /* Free a reference operation structure VP. */
352 static void
353 free_reference (void *vp)
355 vn_reference_t vr = (vn_reference_t) vp;
356 vr->operands.release ();
359 /* Hash table equality function for vn_constant_t. */
361 static int
362 vn_constant_eq (const void *p1, const void *p2)
364 const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
365 const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
367 if (vc1->hashcode != vc2->hashcode)
368 return false;
370 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
373 /* Hash table hash function for vn_constant_t. */
375 static hashval_t
376 vn_constant_hash (const void *p1)
378 const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
379 return vc1->hashcode;
382 /* Lookup a value id for CONSTANT and return it. If it does not
383 exist returns 0. */
385 unsigned int
386 get_constant_value_id (tree constant)
388 void **slot;
389 struct vn_constant_s vc;
391 vc.hashcode = vn_hash_constant_with_type (constant);
392 vc.constant = constant;
393 slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
394 vc.hashcode, NO_INSERT);
395 if (slot)
396 return ((vn_constant_t)*slot)->value_id;
397 return 0;
400 /* Lookup a value id for CONSTANT, and if it does not exist, create a
401 new one and return it. If it does exist, return it. */
403 unsigned int
404 get_or_alloc_constant_value_id (tree constant)
406 void **slot;
407 struct vn_constant_s vc;
408 vn_constant_t vcp;
410 vc.hashcode = vn_hash_constant_with_type (constant);
411 vc.constant = constant;
412 slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
413 vc.hashcode, INSERT);
414 if (*slot)
415 return ((vn_constant_t)*slot)->value_id;
417 vcp = XNEW (struct vn_constant_s);
418 vcp->hashcode = vc.hashcode;
419 vcp->constant = constant;
420 vcp->value_id = get_next_value_id ();
421 *slot = (void *) vcp;
422 bitmap_set_bit (constant_value_ids, vcp->value_id);
423 return vcp->value_id;
426 /* Return true if V is a value id for a constant. */
428 bool
429 value_id_constant_p (unsigned int v)
431 return bitmap_bit_p (constant_value_ids, v);
434 /* Compare two reference operands P1 and P2 for equality. Return true if
435 they are equal, and false otherwise. */
437 static int
438 vn_reference_op_eq (const void *p1, const void *p2)
440 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
441 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
443 return (vro1->opcode == vro2->opcode
444 /* We do not care for differences in type qualification. */
445 && (vro1->type == vro2->type
446 || (vro1->type && vro2->type
447 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
448 TYPE_MAIN_VARIANT (vro2->type))))
449 && expressions_equal_p (vro1->op0, vro2->op0)
450 && expressions_equal_p (vro1->op1, vro2->op1)
451 && expressions_equal_p (vro1->op2, vro2->op2));
454 /* Compute the hash for a reference operand VRO1. */
456 static hashval_t
457 vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
459 result = iterative_hash_hashval_t (vro1->opcode, result);
460 if (vro1->op0)
461 result = iterative_hash_expr (vro1->op0, result);
462 if (vro1->op1)
463 result = iterative_hash_expr (vro1->op1, result);
464 if (vro1->op2)
465 result = iterative_hash_expr (vro1->op2, result);
466 return result;
469 /* Return the hashcode for a given reference operation P1. */
471 static hashval_t
472 vn_reference_hash (const void *p1)
474 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
475 return vr1->hashcode;
478 /* Compute a hash for the reference operation VR1 and return it. */
480 hashval_t
481 vn_reference_compute_hash (const vn_reference_t vr1)
483 hashval_t result = 0;
484 int i;
485 vn_reference_op_t vro;
486 HOST_WIDE_INT off = -1;
487 bool deref = false;
489 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
491 if (vro->opcode == MEM_REF)
492 deref = true;
493 else if (vro->opcode != ADDR_EXPR)
494 deref = false;
495 if (vro->off != -1)
497 if (off == -1)
498 off = 0;
499 off += vro->off;
501 else
503 if (off != -1
504 && off != 0)
505 result = iterative_hash_hashval_t (off, result);
506 off = -1;
507 if (deref
508 && vro->opcode == ADDR_EXPR)
510 if (vro->op0)
512 tree op = TREE_OPERAND (vro->op0, 0);
513 result = iterative_hash_hashval_t (TREE_CODE (op), result);
514 result = iterative_hash_expr (op, result);
517 else
518 result = vn_reference_op_compute_hash (vro, result);
521 if (vr1->vuse)
522 result += SSA_NAME_VERSION (vr1->vuse);
524 return result;
527 /* Return true if reference operations P1 and P2 are equivalent. This
528 means they have the same set of operands and vuses. */
531 vn_reference_eq (const void *p1, const void *p2)
533 unsigned i, j;
535 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
536 const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
537 if (vr1->hashcode != vr2->hashcode)
538 return false;
540 /* Early out if this is not a hash collision. */
541 if (vr1->hashcode != vr2->hashcode)
542 return false;
544 /* The VOP needs to be the same. */
545 if (vr1->vuse != vr2->vuse)
546 return false;
548 /* If the operands are the same we are done. */
549 if (vr1->operands == vr2->operands)
550 return true;
552 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
553 return false;
555 if (INTEGRAL_TYPE_P (vr1->type)
556 && INTEGRAL_TYPE_P (vr2->type))
558 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
559 return false;
561 else if (INTEGRAL_TYPE_P (vr1->type)
562 && (TYPE_PRECISION (vr1->type)
563 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
564 return false;
565 else if (INTEGRAL_TYPE_P (vr2->type)
566 && (TYPE_PRECISION (vr2->type)
567 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
568 return false;
570 i = 0;
571 j = 0;
574 HOST_WIDE_INT off1 = 0, off2 = 0;
575 vn_reference_op_t vro1, vro2;
576 vn_reference_op_s tem1, tem2;
577 bool deref1 = false, deref2 = false;
578 for (; vr1->operands.iterate (i, &vro1); i++)
580 if (vro1->opcode == MEM_REF)
581 deref1 = true;
582 if (vro1->off == -1)
583 break;
584 off1 += vro1->off;
586 for (; vr2->operands.iterate (j, &vro2); j++)
588 if (vro2->opcode == MEM_REF)
589 deref2 = true;
590 if (vro2->off == -1)
591 break;
592 off2 += vro2->off;
594 if (off1 != off2)
595 return false;
596 if (deref1 && vro1->opcode == ADDR_EXPR)
598 memset (&tem1, 0, sizeof (tem1));
599 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
600 tem1.type = TREE_TYPE (tem1.op0);
601 tem1.opcode = TREE_CODE (tem1.op0);
602 vro1 = &tem1;
603 deref1 = false;
605 if (deref2 && vro2->opcode == ADDR_EXPR)
607 memset (&tem2, 0, sizeof (tem2));
608 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
609 tem2.type = TREE_TYPE (tem2.op0);
610 tem2.opcode = TREE_CODE (tem2.op0);
611 vro2 = &tem2;
612 deref2 = false;
614 if (deref1 != deref2)
615 return false;
616 if (!vn_reference_op_eq (vro1, vro2))
617 return false;
618 ++j;
619 ++i;
621 while (vr1->operands.length () != i
622 || vr2->operands.length () != j);
624 return true;
627 /* Copy the operations present in load/store REF into RESULT, a vector of
628 vn_reference_op_s's. */
630 void
631 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
633 if (TREE_CODE (ref) == TARGET_MEM_REF)
635 vn_reference_op_s temp;
637 memset (&temp, 0, sizeof (temp));
638 temp.type = TREE_TYPE (ref);
639 temp.opcode = TREE_CODE (ref);
640 temp.op0 = TMR_INDEX (ref);
641 temp.op1 = TMR_STEP (ref);
642 temp.op2 = TMR_OFFSET (ref);
643 temp.off = -1;
644 result->safe_push (temp);
646 memset (&temp, 0, sizeof (temp));
647 temp.type = NULL_TREE;
648 temp.opcode = ERROR_MARK;
649 temp.op0 = TMR_INDEX2 (ref);
650 temp.off = -1;
651 result->safe_push (temp);
653 memset (&temp, 0, sizeof (temp));
654 temp.type = NULL_TREE;
655 temp.opcode = TREE_CODE (TMR_BASE (ref));
656 temp.op0 = TMR_BASE (ref);
657 temp.off = -1;
658 result->safe_push (temp);
659 return;
662 /* For non-calls, store the information that makes up the address. */
664 while (ref)
666 vn_reference_op_s temp;
668 memset (&temp, 0, sizeof (temp));
669 temp.type = TREE_TYPE (ref);
670 temp.opcode = TREE_CODE (ref);
671 temp.off = -1;
673 switch (temp.opcode)
675 case MODIFY_EXPR:
676 temp.op0 = TREE_OPERAND (ref, 1);
677 break;
678 case WITH_SIZE_EXPR:
679 temp.op0 = TREE_OPERAND (ref, 1);
680 temp.off = 0;
681 break;
682 case MEM_REF:
683 /* The base address gets its own vn_reference_op_s structure. */
684 temp.op0 = TREE_OPERAND (ref, 1);
685 if (host_integerp (TREE_OPERAND (ref, 1), 0))
686 temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
687 break;
688 case BIT_FIELD_REF:
689 /* Record bits and position. */
690 temp.op0 = TREE_OPERAND (ref, 1);
691 temp.op1 = TREE_OPERAND (ref, 2);
692 break;
693 case COMPONENT_REF:
694 /* The field decl is enough to unambiguously specify the field,
695 a matching type is not necessary and a mismatching type
696 is always a spurious difference. */
697 temp.type = NULL_TREE;
698 temp.op0 = TREE_OPERAND (ref, 1);
699 temp.op1 = TREE_OPERAND (ref, 2);
701 tree this_offset = component_ref_field_offset (ref);
702 if (this_offset
703 && TREE_CODE (this_offset) == INTEGER_CST)
705 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
706 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
708 double_int off
709 = tree_to_double_int (this_offset)
710 + tree_to_double_int (bit_offset)
711 .arshift (BITS_PER_UNIT == 8
712 ? 3 : exact_log2 (BITS_PER_UNIT),
713 HOST_BITS_PER_DOUBLE_INT);
714 if (off.fits_shwi ())
715 temp.off = off.low;
719 break;
720 case ARRAY_RANGE_REF:
721 case ARRAY_REF:
722 /* Record index as operand. */
723 temp.op0 = TREE_OPERAND (ref, 1);
724 /* Always record lower bounds and element size. */
725 temp.op1 = array_ref_low_bound (ref);
726 temp.op2 = array_ref_element_size (ref);
727 if (TREE_CODE (temp.op0) == INTEGER_CST
728 && TREE_CODE (temp.op1) == INTEGER_CST
729 && TREE_CODE (temp.op2) == INTEGER_CST)
731 double_int off = tree_to_double_int (temp.op0);
732 off += -tree_to_double_int (temp.op1);
733 off *= tree_to_double_int (temp.op2);
734 if (off.fits_shwi ())
735 temp.off = off.low;
737 break;
738 case VAR_DECL:
739 if (DECL_HARD_REGISTER (ref))
741 temp.op0 = ref;
742 break;
744 /* Fallthru. */
745 case PARM_DECL:
746 case CONST_DECL:
747 case RESULT_DECL:
748 /* Canonicalize decls to MEM[&decl] which is what we end up with
749 when valueizing MEM[ptr] with ptr = &decl. */
750 temp.opcode = MEM_REF;
751 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
752 temp.off = 0;
753 result->safe_push (temp);
754 temp.opcode = ADDR_EXPR;
755 temp.op0 = build_fold_addr_expr (ref);
756 temp.type = TREE_TYPE (temp.op0);
757 temp.off = -1;
758 break;
759 case STRING_CST:
760 case INTEGER_CST:
761 case COMPLEX_CST:
762 case VECTOR_CST:
763 case REAL_CST:
764 case FIXED_CST:
765 case CONSTRUCTOR:
766 case SSA_NAME:
767 temp.op0 = ref;
768 break;
769 case ADDR_EXPR:
770 if (is_gimple_min_invariant (ref))
772 temp.op0 = ref;
773 break;
775 /* Fallthrough. */
776 /* These are only interesting for their operands, their
777 existence, and their type. They will never be the last
778 ref in the chain of references (IE they require an
779 operand), so we don't have to put anything
780 for op* as it will be handled by the iteration */
781 case REALPART_EXPR:
782 case VIEW_CONVERT_EXPR:
783 temp.off = 0;
784 break;
785 case IMAGPART_EXPR:
786 /* This is only interesting for its constant offset. */
787 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
788 break;
789 default:
790 gcc_unreachable ();
792 result->safe_push (temp);
794 if (REFERENCE_CLASS_P (ref)
795 || TREE_CODE (ref) == MODIFY_EXPR
796 || TREE_CODE (ref) == WITH_SIZE_EXPR
797 || (TREE_CODE (ref) == ADDR_EXPR
798 && !is_gimple_min_invariant (ref)))
799 ref = TREE_OPERAND (ref, 0);
800 else
801 ref = NULL_TREE;
805 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
806 operands in *OPS, the reference alias set SET and the reference type TYPE.
807 Return true if something useful was produced. */
809 bool
810 ao_ref_init_from_vn_reference (ao_ref *ref,
811 alias_set_type set, tree type,
812 vec<vn_reference_op_s> ops)
814 vn_reference_op_t op;
815 unsigned i;
816 tree base = NULL_TREE;
817 tree *op0_p = &base;
818 HOST_WIDE_INT offset = 0;
819 HOST_WIDE_INT max_size;
820 HOST_WIDE_INT size = -1;
821 tree size_tree = NULL_TREE;
822 alias_set_type base_alias_set = -1;
824 /* First get the final access size from just the outermost expression. */
825 op = &ops[0];
826 if (op->opcode == COMPONENT_REF)
827 size_tree = DECL_SIZE (op->op0);
828 else if (op->opcode == BIT_FIELD_REF)
829 size_tree = op->op0;
830 else
832 enum machine_mode mode = TYPE_MODE (type);
833 if (mode == BLKmode)
834 size_tree = TYPE_SIZE (type);
835 else
836 size = GET_MODE_BITSIZE (mode);
838 if (size_tree != NULL_TREE)
840 if (!host_integerp (size_tree, 1))
841 size = -1;
842 else
843 size = TREE_INT_CST_LOW (size_tree);
846 /* Initially, maxsize is the same as the accessed element size.
847 In the following it will only grow (or become -1). */
848 max_size = size;
850 /* Compute cumulative bit-offset for nested component-refs and array-refs,
851 and find the ultimate containing object. */
852 FOR_EACH_VEC_ELT (ops, i, op)
854 switch (op->opcode)
856 /* These may be in the reference ops, but we cannot do anything
857 sensible with them here. */
858 case ADDR_EXPR:
859 /* Apart from ADDR_EXPR arguments to MEM_REF. */
860 if (base != NULL_TREE
861 && TREE_CODE (base) == MEM_REF
862 && op->op0
863 && DECL_P (TREE_OPERAND (op->op0, 0)))
865 vn_reference_op_t pop = &ops[i-1];
866 base = TREE_OPERAND (op->op0, 0);
867 if (pop->off == -1)
869 max_size = -1;
870 offset = 0;
872 else
873 offset += pop->off * BITS_PER_UNIT;
874 op0_p = NULL;
875 break;
877 /* Fallthru. */
878 case CALL_EXPR:
879 return false;
881 /* Record the base objects. */
882 case MEM_REF:
883 base_alias_set = get_deref_alias_set (op->op0);
884 *op0_p = build2 (MEM_REF, op->type,
885 NULL_TREE, op->op0);
886 op0_p = &TREE_OPERAND (*op0_p, 0);
887 break;
889 case VAR_DECL:
890 case PARM_DECL:
891 case RESULT_DECL:
892 case SSA_NAME:
893 *op0_p = op->op0;
894 op0_p = NULL;
895 break;
897 /* And now the usual component-reference style ops. */
898 case BIT_FIELD_REF:
899 offset += tree_low_cst (op->op1, 0);
900 break;
902 case COMPONENT_REF:
904 tree field = op->op0;
905 /* We do not have a complete COMPONENT_REF tree here so we
906 cannot use component_ref_field_offset. Do the interesting
907 parts manually. */
909 if (op->op1
910 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
911 max_size = -1;
912 else
914 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
915 * BITS_PER_UNIT);
916 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
918 break;
921 case ARRAY_RANGE_REF:
922 case ARRAY_REF:
923 /* We recorded the lower bound and the element size. */
924 if (!host_integerp (op->op0, 0)
925 || !host_integerp (op->op1, 0)
926 || !host_integerp (op->op2, 0))
927 max_size = -1;
928 else
930 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
931 hindex -= TREE_INT_CST_LOW (op->op1);
932 hindex *= TREE_INT_CST_LOW (op->op2);
933 hindex *= BITS_PER_UNIT;
934 offset += hindex;
936 break;
938 case REALPART_EXPR:
939 break;
941 case IMAGPART_EXPR:
942 offset += size;
943 break;
945 case VIEW_CONVERT_EXPR:
946 break;
948 case STRING_CST:
949 case INTEGER_CST:
950 case COMPLEX_CST:
951 case VECTOR_CST:
952 case REAL_CST:
953 case CONSTRUCTOR:
954 case CONST_DECL:
955 return false;
957 default:
958 return false;
962 if (base == NULL_TREE)
963 return false;
965 ref->ref = NULL_TREE;
966 ref->base = base;
967 ref->offset = offset;
968 ref->size = size;
969 ref->max_size = max_size;
970 ref->ref_alias_set = set;
971 if (base_alias_set != -1)
972 ref->base_alias_set = base_alias_set;
973 else
974 ref->base_alias_set = get_alias_set (base);
975 /* We discount volatiles from value-numbering elsewhere. */
976 ref->volatile_p = false;
978 return true;
981 /* Copy the operations present in load/store/call REF into RESULT, a vector of
982 vn_reference_op_s's. */
984 void
985 copy_reference_ops_from_call (gimple call,
986 vec<vn_reference_op_s> *result)
988 vn_reference_op_s temp;
989 unsigned i;
990 tree lhs = gimple_call_lhs (call);
992 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
993 different. By adding the lhs here in the vector, we ensure that the
994 hashcode is different, guaranteeing a different value number. */
995 if (lhs && TREE_CODE (lhs) != SSA_NAME)
997 memset (&temp, 0, sizeof (temp));
998 temp.opcode = MODIFY_EXPR;
999 temp.type = TREE_TYPE (lhs);
1000 temp.op0 = lhs;
1001 temp.off = -1;
1002 result->safe_push (temp);
1005 /* Copy the type, opcode, function being called and static chain. */
1006 memset (&temp, 0, sizeof (temp));
1007 temp.type = gimple_call_return_type (call);
1008 temp.opcode = CALL_EXPR;
1009 temp.op0 = gimple_call_fn (call);
1010 temp.op1 = gimple_call_chain (call);
1011 temp.off = -1;
1012 result->safe_push (temp);
1014 /* Copy the call arguments. As they can be references as well,
1015 just chain them together. */
1016 for (i = 0; i < gimple_call_num_args (call); ++i)
1018 tree callarg = gimple_call_arg (call, i);
1019 copy_reference_ops_from_ref (callarg, result);
1023 /* Create a vector of vn_reference_op_s structures from REF, a
1024 REFERENCE_CLASS_P tree. The vector is not shared. */
1026 static vec<vn_reference_op_s>
1027 create_reference_ops_from_ref (tree ref)
1029 vec<vn_reference_op_s> result = vNULL;
1031 copy_reference_ops_from_ref (ref, &result);
1032 return result;
1035 /* Create a vector of vn_reference_op_s structures from CALL, a
1036 call statement. The vector is not shared. */
1038 static vec<vn_reference_op_s>
1039 create_reference_ops_from_call (gimple call)
1041 vec<vn_reference_op_s> result = vNULL;
1043 copy_reference_ops_from_call (call, &result);
1044 return result;
1047 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1048 *I_P to point to the last element of the replacement. */
1049 void
1050 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1051 unsigned int *i_p)
1053 unsigned int i = *i_p;
1054 vn_reference_op_t op = &(*ops)[i];
1055 vn_reference_op_t mem_op = &(*ops)[i - 1];
1056 tree addr_base;
1057 HOST_WIDE_INT addr_offset = 0;
1059 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1060 from .foo.bar to the preceding MEM_REF offset and replace the
1061 address with &OBJ. */
1062 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1063 &addr_offset);
1064 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1065 if (addr_base != op->op0)
1067 double_int off = tree_to_double_int (mem_op->op0);
1068 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1069 off += double_int::from_shwi (addr_offset);
1070 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1071 op->op0 = build_fold_addr_expr (addr_base);
1072 if (host_integerp (mem_op->op0, 0))
1073 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1074 else
1075 mem_op->off = -1;
1079 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1080 *I_P to point to the last element of the replacement. */
1081 static void
1082 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1083 unsigned int *i_p)
1085 unsigned int i = *i_p;
1086 vn_reference_op_t op = &(*ops)[i];
1087 vn_reference_op_t mem_op = &(*ops)[i - 1];
1088 gimple def_stmt;
1089 enum tree_code code;
1090 double_int off;
1092 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1093 if (!is_gimple_assign (def_stmt))
1094 return;
1096 code = gimple_assign_rhs_code (def_stmt);
1097 if (code != ADDR_EXPR
1098 && code != POINTER_PLUS_EXPR)
1099 return;
1101 off = tree_to_double_int (mem_op->op0);
1102 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1104 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1105 from .foo.bar to the preceding MEM_REF offset and replace the
1106 address with &OBJ. */
1107 if (code == ADDR_EXPR)
1109 tree addr, addr_base;
1110 HOST_WIDE_INT addr_offset;
1112 addr = gimple_assign_rhs1 (def_stmt);
1113 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1114 &addr_offset);
1115 if (!addr_base
1116 || TREE_CODE (addr_base) != MEM_REF)
1117 return;
1119 off += double_int::from_shwi (addr_offset);
1120 off += mem_ref_offset (addr_base);
1121 op->op0 = TREE_OPERAND (addr_base, 0);
1123 else
1125 tree ptr, ptroff;
1126 ptr = gimple_assign_rhs1 (def_stmt);
1127 ptroff = gimple_assign_rhs2 (def_stmt);
1128 if (TREE_CODE (ptr) != SSA_NAME
1129 || TREE_CODE (ptroff) != INTEGER_CST)
1130 return;
1132 off += tree_to_double_int (ptroff);
1133 op->op0 = ptr;
1136 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1137 if (host_integerp (mem_op->op0, 0))
1138 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1139 else
1140 mem_op->off = -1;
1141 if (TREE_CODE (op->op0) == SSA_NAME)
1142 op->op0 = SSA_VAL (op->op0);
1143 if (TREE_CODE (op->op0) != SSA_NAME)
1144 op->opcode = TREE_CODE (op->op0);
1146 /* And recurse. */
1147 if (TREE_CODE (op->op0) == SSA_NAME)
1148 vn_reference_maybe_forwprop_address (ops, i_p);
1149 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1150 vn_reference_fold_indirect (ops, i_p);
1153 /* Optimize the reference REF to a constant if possible or return
1154 NULL_TREE if not. */
1156 tree
1157 fully_constant_vn_reference_p (vn_reference_t ref)
1159 vec<vn_reference_op_s> operands = ref->operands;
1160 vn_reference_op_t op;
1162 /* Try to simplify the translated expression if it is
1163 a call to a builtin function with at most two arguments. */
1164 op = &operands[0];
1165 if (op->opcode == CALL_EXPR
1166 && TREE_CODE (op->op0) == ADDR_EXPR
1167 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1168 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1169 && operands.length () >= 2
1170 && operands.length () <= 3)
1172 vn_reference_op_t arg0, arg1 = NULL;
1173 bool anyconst = false;
1174 arg0 = &operands[1];
1175 if (operands.length () > 2)
1176 arg1 = &operands[2];
1177 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1178 || (arg0->opcode == ADDR_EXPR
1179 && is_gimple_min_invariant (arg0->op0)))
1180 anyconst = true;
1181 if (arg1
1182 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1183 || (arg1->opcode == ADDR_EXPR
1184 && is_gimple_min_invariant (arg1->op0))))
1185 anyconst = true;
1186 if (anyconst)
1188 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1189 arg1 ? 2 : 1,
1190 arg0->op0,
1191 arg1 ? arg1->op0 : NULL);
1192 if (folded
1193 && TREE_CODE (folded) == NOP_EXPR)
1194 folded = TREE_OPERAND (folded, 0);
1195 if (folded
1196 && is_gimple_min_invariant (folded))
1197 return folded;
1201 /* Simplify reads from constant strings. */
1202 else if (op->opcode == ARRAY_REF
1203 && TREE_CODE (op->op0) == INTEGER_CST
1204 && integer_zerop (op->op1)
1205 && operands.length () == 2)
1207 vn_reference_op_t arg0;
1208 arg0 = &operands[1];
1209 if (arg0->opcode == STRING_CST
1210 && (TYPE_MODE (op->type)
1211 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1212 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1213 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1214 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1215 return build_int_cst_type (op->type,
1216 (TREE_STRING_POINTER (arg0->op0)
1217 [TREE_INT_CST_LOW (op->op0)]));
1220 return NULL_TREE;
1223 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1224 structures into their value numbers. This is done in-place, and
1225 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1226 whether any operands were valueized. */
1228 static vec<vn_reference_op_s>
1229 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1231 vn_reference_op_t vro;
1232 unsigned int i;
1234 *valueized_anything = false;
1236 FOR_EACH_VEC_ELT (orig, i, vro)
1238 if (vro->opcode == SSA_NAME
1239 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1241 tree tem = SSA_VAL (vro->op0);
1242 if (tem != vro->op0)
1244 *valueized_anything = true;
1245 vro->op0 = tem;
1247 /* If it transforms from an SSA_NAME to a constant, update
1248 the opcode. */
1249 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1250 vro->opcode = TREE_CODE (vro->op0);
1252 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1254 tree tem = SSA_VAL (vro->op1);
1255 if (tem != vro->op1)
1257 *valueized_anything = true;
1258 vro->op1 = tem;
1261 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1263 tree tem = SSA_VAL (vro->op2);
1264 if (tem != vro->op2)
1266 *valueized_anything = true;
1267 vro->op2 = tem;
1270 /* If it transforms from an SSA_NAME to an address, fold with
1271 a preceding indirect reference. */
1272 if (i > 0
1273 && vro->op0
1274 && TREE_CODE (vro->op0) == ADDR_EXPR
1275 && orig[i - 1].opcode == MEM_REF)
1276 vn_reference_fold_indirect (&orig, &i);
1277 else if (i > 0
1278 && vro->opcode == SSA_NAME
1279 && orig[i - 1].opcode == MEM_REF)
1280 vn_reference_maybe_forwprop_address (&orig, &i);
1281 /* If it transforms a non-constant ARRAY_REF into a constant
1282 one, adjust the constant offset. */
1283 else if (vro->opcode == ARRAY_REF
1284 && vro->off == -1
1285 && TREE_CODE (vro->op0) == INTEGER_CST
1286 && TREE_CODE (vro->op1) == INTEGER_CST
1287 && TREE_CODE (vro->op2) == INTEGER_CST)
1289 double_int off = tree_to_double_int (vro->op0);
1290 off += -tree_to_double_int (vro->op1);
1291 off *= tree_to_double_int (vro->op2);
1292 if (off.fits_shwi ())
1293 vro->off = off.low;
1297 return orig;
1300 static vec<vn_reference_op_s>
1301 valueize_refs (vec<vn_reference_op_s> orig)
1303 bool tem;
1304 return valueize_refs_1 (orig, &tem);
1307 static vec<vn_reference_op_s> shared_lookup_references;
1309 /* Create a vector of vn_reference_op_s structures from REF, a
1310 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1311 this function. *VALUEIZED_ANYTHING will specify whether any
1312 operands were valueized. */
1314 static vec<vn_reference_op_s>
1315 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1317 if (!ref)
1318 return vNULL;
1319 shared_lookup_references.truncate (0);
1320 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1321 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1322 valueized_anything);
1323 return shared_lookup_references;
1326 /* Create a vector of vn_reference_op_s structures from CALL, a
1327 call statement. The vector is shared among all callers of
1328 this function. */
1330 static vec<vn_reference_op_s>
1331 valueize_shared_reference_ops_from_call (gimple call)
1333 if (!call)
1334 return vNULL;
1335 shared_lookup_references.truncate (0);
1336 copy_reference_ops_from_call (call, &shared_lookup_references);
1337 shared_lookup_references = valueize_refs (shared_lookup_references);
1338 return shared_lookup_references;
1341 /* Lookup a SCCVN reference operation VR in the current hash table.
1342 Returns the resulting value number if it exists in the hash table,
1343 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1344 vn_reference_t stored in the hashtable if something is found. */
1346 static tree
1347 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1349 void **slot;
1350 hashval_t hash;
1352 hash = vr->hashcode;
1353 slot = htab_find_slot_with_hash (current_info->references, vr,
1354 hash, NO_INSERT);
1355 if (!slot && current_info == optimistic_info)
1356 slot = htab_find_slot_with_hash (valid_info->references, vr,
1357 hash, NO_INSERT);
1358 if (slot)
1360 if (vnresult)
1361 *vnresult = (vn_reference_t)*slot;
1362 return ((vn_reference_t)*slot)->result;
1365 return NULL_TREE;
1368 static tree *last_vuse_ptr;
1369 static vn_lookup_kind vn_walk_kind;
1370 static vn_lookup_kind default_vn_walk_kind;
1372 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1373 with the current VUSE and performs the expression lookup. */
1375 static void *
1376 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1377 unsigned int cnt, void *vr_)
1379 vn_reference_t vr = (vn_reference_t)vr_;
1380 void **slot;
1381 hashval_t hash;
1383 /* This bounds the stmt walks we perform on reference lookups
1384 to O(1) instead of O(N) where N is the number of dominating
1385 stores. */
1386 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1387 return (void *)-1;
1389 if (last_vuse_ptr)
1390 *last_vuse_ptr = vuse;
1392 /* Fixup vuse and hash. */
1393 if (vr->vuse)
1394 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1395 vr->vuse = SSA_VAL (vuse);
1396 if (vr->vuse)
1397 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1399 hash = vr->hashcode;
1400 slot = htab_find_slot_with_hash (current_info->references, vr,
1401 hash, NO_INSERT);
1402 if (!slot && current_info == optimistic_info)
1403 slot = htab_find_slot_with_hash (valid_info->references, vr,
1404 hash, NO_INSERT);
1405 if (slot)
1406 return *slot;
1408 return NULL;
1411 /* Lookup an existing or insert a new vn_reference entry into the
1412 value table for the VUSE, SET, TYPE, OPERANDS reference which
1413 has the value VALUE which is either a constant or an SSA name. */
1415 static vn_reference_t
1416 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1417 alias_set_type set,
1418 tree type,
1419 vec<vn_reference_op_s,
1420 va_heap> operands,
1421 tree value)
1423 struct vn_reference_s vr1;
1424 vn_reference_t result;
1425 unsigned value_id;
1426 vr1.vuse = vuse;
1427 vr1.operands = operands;
1428 vr1.type = type;
1429 vr1.set = set;
1430 vr1.hashcode = vn_reference_compute_hash (&vr1);
1431 if (vn_reference_lookup_1 (&vr1, &result))
1432 return result;
1433 if (TREE_CODE (value) == SSA_NAME)
1434 value_id = VN_INFO (value)->value_id;
1435 else
1436 value_id = get_or_alloc_constant_value_id (value);
1437 return vn_reference_insert_pieces (vuse, set, type,
1438 operands.copy (), value, value_id);
1441 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1442 from the statement defining VUSE and if not successful tries to
1443 translate *REFP and VR_ through an aggregate copy at the definition
1444 of VUSE. */
1446 static void *
1447 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1449 vn_reference_t vr = (vn_reference_t)vr_;
1450 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1451 tree base;
1452 HOST_WIDE_INT offset, maxsize;
1453 static vec<vn_reference_op_s>
1454 lhs_ops = vNULL;
1455 ao_ref lhs_ref;
1456 bool lhs_ref_ok = false;
1458 /* First try to disambiguate after value-replacing in the definitions LHS. */
1459 if (is_gimple_assign (def_stmt))
1461 vec<vn_reference_op_s> tem;
1462 tree lhs = gimple_assign_lhs (def_stmt);
1463 bool valueized_anything = false;
1464 /* Avoid re-allocation overhead. */
1465 lhs_ops.truncate (0);
1466 copy_reference_ops_from_ref (lhs, &lhs_ops);
1467 tem = lhs_ops;
1468 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1469 gcc_assert (lhs_ops == tem);
1470 if (valueized_anything)
1472 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1473 get_alias_set (lhs),
1474 TREE_TYPE (lhs), lhs_ops);
1475 if (lhs_ref_ok
1476 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1477 return NULL;
1479 else
1481 ao_ref_init (&lhs_ref, lhs);
1482 lhs_ref_ok = true;
1486 base = ao_ref_base (ref);
1487 offset = ref->offset;
1488 maxsize = ref->max_size;
1490 /* If we cannot constrain the size of the reference we cannot
1491 test if anything kills it. */
1492 if (maxsize == -1)
1493 return (void *)-1;
1495 /* We can't deduce anything useful from clobbers. */
1496 if (gimple_clobber_p (def_stmt))
1497 return (void *)-1;
1499 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1500 from that definition.
1501 1) Memset. */
1502 if (is_gimple_reg_type (vr->type)
1503 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1504 && integer_zerop (gimple_call_arg (def_stmt, 1))
1505 && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1506 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1508 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1509 tree base2;
1510 HOST_WIDE_INT offset2, size2, maxsize2;
1511 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1512 size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1513 if ((unsigned HOST_WIDE_INT)size2 / 8
1514 == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1515 && maxsize2 != -1
1516 && operand_equal_p (base, base2, 0)
1517 && offset2 <= offset
1518 && offset2 + size2 >= offset + maxsize)
1520 tree val = build_zero_cst (vr->type);
1521 return vn_reference_lookup_or_insert_for_pieces
1522 (vuse, vr->set, vr->type, vr->operands, val);
1526 /* 2) Assignment from an empty CONSTRUCTOR. */
1527 else if (is_gimple_reg_type (vr->type)
1528 && gimple_assign_single_p (def_stmt)
1529 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1530 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1532 tree base2;
1533 HOST_WIDE_INT offset2, size2, maxsize2;
1534 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1535 &offset2, &size2, &maxsize2);
1536 if (maxsize2 != -1
1537 && operand_equal_p (base, base2, 0)
1538 && offset2 <= offset
1539 && offset2 + size2 >= offset + maxsize)
1541 tree val = build_zero_cst (vr->type);
1542 return vn_reference_lookup_or_insert_for_pieces
1543 (vuse, vr->set, vr->type, vr->operands, val);
1547 /* 3) Assignment from a constant. We can use folds native encode/interpret
1548 routines to extract the assigned bits. */
1549 else if (vn_walk_kind == VN_WALKREWRITE
1550 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1551 && ref->size == maxsize
1552 && maxsize % BITS_PER_UNIT == 0
1553 && offset % BITS_PER_UNIT == 0
1554 && is_gimple_reg_type (vr->type)
1555 && gimple_assign_single_p (def_stmt)
1556 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1558 tree base2;
1559 HOST_WIDE_INT offset2, size2, maxsize2;
1560 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1561 &offset2, &size2, &maxsize2);
1562 if (maxsize2 != -1
1563 && maxsize2 == size2
1564 && size2 % BITS_PER_UNIT == 0
1565 && offset2 % BITS_PER_UNIT == 0
1566 && operand_equal_p (base, base2, 0)
1567 && offset2 <= offset
1568 && offset2 + size2 >= offset + maxsize)
1570 /* We support up to 512-bit values (for V8DFmode). */
1571 unsigned char buffer[64];
1572 int len;
1574 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1575 buffer, sizeof (buffer));
1576 if (len > 0)
1578 tree val = native_interpret_expr (vr->type,
1579 buffer
1580 + ((offset - offset2)
1581 / BITS_PER_UNIT),
1582 ref->size / BITS_PER_UNIT);
1583 if (val)
1584 return vn_reference_lookup_or_insert_for_pieces
1585 (vuse, vr->set, vr->type, vr->operands, val);
1590 /* 4) Assignment from an SSA name which definition we may be able
1591 to access pieces from. */
1592 else if (ref->size == maxsize
1593 && is_gimple_reg_type (vr->type)
1594 && gimple_assign_single_p (def_stmt)
1595 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1597 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1598 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1599 if (is_gimple_assign (def_stmt2)
1600 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1601 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1602 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1604 tree base2;
1605 HOST_WIDE_INT offset2, size2, maxsize2, off;
1606 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1607 &offset2, &size2, &maxsize2);
1608 off = offset - offset2;
1609 if (maxsize2 != -1
1610 && maxsize2 == size2
1611 && operand_equal_p (base, base2, 0)
1612 && offset2 <= offset
1613 && offset2 + size2 >= offset + maxsize)
1615 tree val = NULL_TREE;
1616 HOST_WIDE_INT elsz
1617 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1618 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1620 if (off == 0)
1621 val = gimple_assign_rhs1 (def_stmt2);
1622 else if (off == elsz)
1623 val = gimple_assign_rhs2 (def_stmt2);
1625 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1626 && off % elsz == 0)
1628 tree ctor = gimple_assign_rhs1 (def_stmt2);
1629 unsigned i = off / elsz;
1630 if (i < CONSTRUCTOR_NELTS (ctor))
1632 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1633 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1635 if (TREE_CODE (TREE_TYPE (elt->value))
1636 != VECTOR_TYPE)
1637 val = elt->value;
1641 if (val)
1642 return vn_reference_lookup_or_insert_for_pieces
1643 (vuse, vr->set, vr->type, vr->operands, val);
1648 /* 5) For aggregate copies translate the reference through them if
1649 the copy kills ref. */
1650 else if (vn_walk_kind == VN_WALKREWRITE
1651 && gimple_assign_single_p (def_stmt)
1652 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1653 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1654 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1656 tree base2;
1657 HOST_WIDE_INT offset2, size2, maxsize2;
1658 int i, j;
1659 vec<vn_reference_op_s>
1660 rhs = vNULL;
1661 vn_reference_op_t vro;
1662 ao_ref r;
1664 if (!lhs_ref_ok)
1665 return (void *)-1;
1667 /* See if the assignment kills REF. */
1668 base2 = ao_ref_base (&lhs_ref);
1669 offset2 = lhs_ref.offset;
1670 size2 = lhs_ref.size;
1671 maxsize2 = lhs_ref.max_size;
1672 if (maxsize2 == -1
1673 || (base != base2 && !operand_equal_p (base, base2, 0))
1674 || offset2 > offset
1675 || offset2 + size2 < offset + maxsize)
1676 return (void *)-1;
1678 /* Find the common base of ref and the lhs. lhs_ops already
1679 contains valueized operands for the lhs. */
1680 i = vr->operands.length () - 1;
1681 j = lhs_ops.length () - 1;
1682 while (j >= 0 && i >= 0
1683 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1685 i--;
1686 j--;
1689 /* ??? The innermost op should always be a MEM_REF and we already
1690 checked that the assignment to the lhs kills vr. Thus for
1691 aggregate copies using char[] types the vn_reference_op_eq
1692 may fail when comparing types for compatibility. But we really
1693 don't care here - further lookups with the rewritten operands
1694 will simply fail if we messed up types too badly. */
1695 if (j == 0 && i >= 0
1696 && lhs_ops[0].opcode == MEM_REF
1697 && lhs_ops[0].off != -1
1698 && (lhs_ops[0].off == vr->operands[i].off))
1699 i--, j--;
1701 /* i now points to the first additional op.
1702 ??? LHS may not be completely contained in VR, one or more
1703 VIEW_CONVERT_EXPRs could be in its way. We could at least
1704 try handling outermost VIEW_CONVERT_EXPRs. */
1705 if (j != -1)
1706 return (void *)-1;
1708 /* Now re-write REF to be based on the rhs of the assignment. */
1709 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1710 /* We need to pre-pend vr->operands[0..i] to rhs. */
1711 if (i + 1 + rhs.length () > vr->operands.length ())
1713 vec<vn_reference_op_s> old = vr->operands;
1714 vr->operands.safe_grow (i + 1 + rhs.length ());
1715 if (old == shared_lookup_references
1716 && vr->operands != old)
1717 shared_lookup_references = vNULL;
1719 else
1720 vr->operands.truncate (i + 1 + rhs.length ());
1721 FOR_EACH_VEC_ELT (rhs, j, vro)
1722 vr->operands[i + 1 + j] = *vro;
1723 rhs.release ();
1724 vr->operands = valueize_refs (vr->operands);
1725 vr->hashcode = vn_reference_compute_hash (vr);
1727 /* Adjust *ref from the new operands. */
1728 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1729 return (void *)-1;
1730 /* This can happen with bitfields. */
1731 if (ref->size != r.size)
1732 return (void *)-1;
1733 *ref = r;
1735 /* Do not update last seen VUSE after translating. */
1736 last_vuse_ptr = NULL;
1738 /* Keep looking for the adjusted *REF / VR pair. */
1739 return NULL;
1742 /* 6) For memcpy copies translate the reference through them if
1743 the copy kills ref. */
1744 else if (vn_walk_kind == VN_WALKREWRITE
1745 && is_gimple_reg_type (vr->type)
1746 /* ??? Handle BCOPY as well. */
1747 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1748 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1749 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1750 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1751 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1752 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1753 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1754 && host_integerp (gimple_call_arg (def_stmt, 2), 1))
1756 tree lhs, rhs;
1757 ao_ref r;
1758 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1759 vn_reference_op_s op;
1760 HOST_WIDE_INT at;
1763 /* Only handle non-variable, addressable refs. */
1764 if (ref->size != maxsize
1765 || offset % BITS_PER_UNIT != 0
1766 || ref->size % BITS_PER_UNIT != 0)
1767 return (void *)-1;
1769 /* Extract a pointer base and an offset for the destination. */
1770 lhs = gimple_call_arg (def_stmt, 0);
1771 lhs_offset = 0;
1772 if (TREE_CODE (lhs) == SSA_NAME)
1773 lhs = SSA_VAL (lhs);
1774 if (TREE_CODE (lhs) == ADDR_EXPR)
1776 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1777 &lhs_offset);
1778 if (!tem)
1779 return (void *)-1;
1780 if (TREE_CODE (tem) == MEM_REF
1781 && host_integerp (TREE_OPERAND (tem, 1), 1))
1783 lhs = TREE_OPERAND (tem, 0);
1784 lhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1786 else if (DECL_P (tem))
1787 lhs = build_fold_addr_expr (tem);
1788 else
1789 return (void *)-1;
1791 if (TREE_CODE (lhs) != SSA_NAME
1792 && TREE_CODE (lhs) != ADDR_EXPR)
1793 return (void *)-1;
1795 /* Extract a pointer base and an offset for the source. */
1796 rhs = gimple_call_arg (def_stmt, 1);
1797 rhs_offset = 0;
1798 if (TREE_CODE (rhs) == SSA_NAME)
1799 rhs = SSA_VAL (rhs);
1800 if (TREE_CODE (rhs) == ADDR_EXPR)
1802 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1803 &rhs_offset);
1804 if (!tem)
1805 return (void *)-1;
1806 if (TREE_CODE (tem) == MEM_REF
1807 && host_integerp (TREE_OPERAND (tem, 1), 1))
1809 rhs = TREE_OPERAND (tem, 0);
1810 rhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1812 else if (DECL_P (tem))
1813 rhs = build_fold_addr_expr (tem);
1814 else
1815 return (void *)-1;
1817 if (TREE_CODE (rhs) != SSA_NAME
1818 && TREE_CODE (rhs) != ADDR_EXPR)
1819 return (void *)-1;
1821 copy_size = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2));
1823 /* The bases of the destination and the references have to agree. */
1824 if ((TREE_CODE (base) != MEM_REF
1825 && !DECL_P (base))
1826 || (TREE_CODE (base) == MEM_REF
1827 && (TREE_OPERAND (base, 0) != lhs
1828 || !host_integerp (TREE_OPERAND (base, 1), 1)))
1829 || (DECL_P (base)
1830 && (TREE_CODE (lhs) != ADDR_EXPR
1831 || TREE_OPERAND (lhs, 0) != base)))
1832 return (void *)-1;
1834 /* And the access has to be contained within the memcpy destination. */
1835 at = offset / BITS_PER_UNIT;
1836 if (TREE_CODE (base) == MEM_REF)
1837 at += TREE_INT_CST_LOW (TREE_OPERAND (base, 1));
1838 if (lhs_offset > at
1839 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1840 return (void *)-1;
1842 /* Make room for 2 operands in the new reference. */
1843 if (vr->operands.length () < 2)
1845 vec<vn_reference_op_s> old = vr->operands;
1846 vr->operands.safe_grow_cleared (2);
1847 if (old == shared_lookup_references
1848 && vr->operands != old)
1849 shared_lookup_references.create (0);
1851 else
1852 vr->operands.truncate (2);
1854 /* The looked-through reference is a simple MEM_REF. */
1855 memset (&op, 0, sizeof (op));
1856 op.type = vr->type;
1857 op.opcode = MEM_REF;
1858 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
1859 op.off = at - lhs_offset + rhs_offset;
1860 vr->operands[0] = op;
1861 op.type = TREE_TYPE (rhs);
1862 op.opcode = TREE_CODE (rhs);
1863 op.op0 = rhs;
1864 op.off = -1;
1865 vr->operands[1] = op;
1866 vr->hashcode = vn_reference_compute_hash (vr);
1868 /* Adjust *ref from the new operands. */
1869 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1870 return (void *)-1;
1871 /* This can happen with bitfields. */
1872 if (ref->size != r.size)
1873 return (void *)-1;
1874 *ref = r;
1876 /* Do not update last seen VUSE after translating. */
1877 last_vuse_ptr = NULL;
1879 /* Keep looking for the adjusted *REF / VR pair. */
1880 return NULL;
1883 /* Bail out and stop walking. */
1884 return (void *)-1;
1887 /* Lookup a reference operation by it's parts, in the current hash table.
1888 Returns the resulting value number if it exists in the hash table,
1889 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1890 vn_reference_t stored in the hashtable if something is found. */
1892 tree
1893 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1894 vec<vn_reference_op_s> operands,
1895 vn_reference_t *vnresult, vn_lookup_kind kind)
1897 struct vn_reference_s vr1;
1898 vn_reference_t tmp;
1899 tree cst;
1901 if (!vnresult)
1902 vnresult = &tmp;
1903 *vnresult = NULL;
1905 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1906 shared_lookup_references.truncate (0);
1907 shared_lookup_references.safe_grow (operands.length ());
1908 memcpy (shared_lookup_references.address (),
1909 operands.address (),
1910 sizeof (vn_reference_op_s)
1911 * operands.length ());
1912 vr1.operands = operands = shared_lookup_references
1913 = valueize_refs (shared_lookup_references);
1914 vr1.type = type;
1915 vr1.set = set;
1916 vr1.hashcode = vn_reference_compute_hash (&vr1);
1917 if ((cst = fully_constant_vn_reference_p (&vr1)))
1918 return cst;
1920 vn_reference_lookup_1 (&vr1, vnresult);
1921 if (!*vnresult
1922 && kind != VN_NOWALK
1923 && vr1.vuse)
1925 ao_ref r;
1926 vn_walk_kind = kind;
1927 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
1928 *vnresult =
1929 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1930 vn_reference_lookup_2,
1931 vn_reference_lookup_3, &vr1);
1932 if (vr1.operands != operands)
1933 vr1.operands.release ();
1936 if (*vnresult)
1937 return (*vnresult)->result;
1939 return NULL_TREE;
1942 /* Lookup OP in the current hash table, and return the resulting value
1943 number if it exists in the hash table. Return NULL_TREE if it does
1944 not exist in the hash table or if the result field of the structure
1945 was NULL.. VNRESULT will be filled in with the vn_reference_t
1946 stored in the hashtable if one exists. */
1948 tree
1949 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
1950 vn_reference_t *vnresult)
1952 vec<vn_reference_op_s> operands;
1953 struct vn_reference_s vr1;
1954 tree cst;
1955 bool valuezied_anything;
1957 if (vnresult)
1958 *vnresult = NULL;
1960 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1961 vr1.operands = operands
1962 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
1963 vr1.type = TREE_TYPE (op);
1964 vr1.set = get_alias_set (op);
1965 vr1.hashcode = vn_reference_compute_hash (&vr1);
1966 if ((cst = fully_constant_vn_reference_p (&vr1)))
1967 return cst;
1969 if (kind != VN_NOWALK
1970 && vr1.vuse)
1972 vn_reference_t wvnresult;
1973 ao_ref r;
1974 /* Make sure to use a valueized reference if we valueized anything.
1975 Otherwise preserve the full reference for advanced TBAA. */
1976 if (!valuezied_anything
1977 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
1978 vr1.operands))
1979 ao_ref_init (&r, op);
1980 vn_walk_kind = kind;
1981 wvnresult =
1982 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1983 vn_reference_lookup_2,
1984 vn_reference_lookup_3, &vr1);
1985 if (vr1.operands != operands)
1986 vr1.operands.release ();
1987 if (wvnresult)
1989 if (vnresult)
1990 *vnresult = wvnresult;
1991 return wvnresult->result;
1994 return NULL_TREE;
1997 return vn_reference_lookup_1 (&vr1, vnresult);
2001 /* Insert OP into the current hash table with a value number of
2002 RESULT, and return the resulting reference structure we created. */
2004 vn_reference_t
2005 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2007 void **slot;
2008 vn_reference_t vr1;
2010 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2011 if (TREE_CODE (result) == SSA_NAME)
2012 vr1->value_id = VN_INFO (result)->value_id;
2013 else
2014 vr1->value_id = get_or_alloc_constant_value_id (result);
2015 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2016 vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
2017 vr1->type = TREE_TYPE (op);
2018 vr1->set = get_alias_set (op);
2019 vr1->hashcode = vn_reference_compute_hash (vr1);
2020 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2021 vr1->result_vdef = vdef;
2023 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
2024 INSERT);
2026 /* Because we lookup stores using vuses, and value number failures
2027 using the vdefs (see visit_reference_op_store for how and why),
2028 it's possible that on failure we may try to insert an already
2029 inserted store. This is not wrong, there is no ssa name for a
2030 store that we could use as a differentiator anyway. Thus, unlike
2031 the other lookup functions, you cannot gcc_assert (!*slot)
2032 here. */
2034 /* But free the old slot in case of a collision. */
2035 if (*slot)
2036 free_reference (*slot);
2038 *slot = vr1;
2039 return vr1;
2042 /* Insert a reference by it's pieces into the current hash table with
2043 a value number of RESULT. Return the resulting reference
2044 structure we created. */
2046 vn_reference_t
2047 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2048 vec<vn_reference_op_s> operands,
2049 tree result, unsigned int value_id)
2052 void **slot;
2053 vn_reference_t vr1;
2055 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2056 vr1->value_id = value_id;
2057 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2058 vr1->operands = valueize_refs (operands);
2059 vr1->type = type;
2060 vr1->set = set;
2061 vr1->hashcode = vn_reference_compute_hash (vr1);
2062 if (result && TREE_CODE (result) == SSA_NAME)
2063 result = SSA_VAL (result);
2064 vr1->result = result;
2066 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
2067 INSERT);
2069 /* At this point we should have all the things inserted that we have
2070 seen before, and we should never try inserting something that
2071 already exists. */
2072 gcc_assert (!*slot);
2073 if (*slot)
2074 free_reference (*slot);
2076 *slot = vr1;
2077 return vr1;
2080 /* Compute and return the hash value for nary operation VBO1. */
2082 hashval_t
2083 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2085 hashval_t hash;
2086 unsigned i;
2088 for (i = 0; i < vno1->length; ++i)
2089 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2090 vno1->op[i] = SSA_VAL (vno1->op[i]);
2092 if (vno1->length == 2
2093 && commutative_tree_code (vno1->opcode)
2094 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2096 tree temp = vno1->op[0];
2097 vno1->op[0] = vno1->op[1];
2098 vno1->op[1] = temp;
2101 hash = iterative_hash_hashval_t (vno1->opcode, 0);
2102 for (i = 0; i < vno1->length; ++i)
2103 hash = iterative_hash_expr (vno1->op[i], hash);
2105 return hash;
2108 /* Return the computed hashcode for nary operation P1. */
2110 static hashval_t
2111 vn_nary_op_hash (const void *p1)
2113 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
2114 return vno1->hashcode;
2117 /* Compare nary operations P1 and P2 and return true if they are
2118 equivalent. */
2121 vn_nary_op_eq (const void *p1, const void *p2)
2123 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
2124 const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
2125 unsigned i;
2127 if (vno1->hashcode != vno2->hashcode)
2128 return false;
2130 if (vno1->length != vno2->length)
2131 return false;
2133 if (vno1->opcode != vno2->opcode
2134 || !types_compatible_p (vno1->type, vno2->type))
2135 return false;
2137 for (i = 0; i < vno1->length; ++i)
2138 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2139 return false;
2141 return true;
2144 /* Initialize VNO from the pieces provided. */
2146 static void
2147 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2148 enum tree_code code, tree type, tree *ops)
2150 vno->opcode = code;
2151 vno->length = length;
2152 vno->type = type;
2153 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2156 /* Initialize VNO from OP. */
2158 static void
2159 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2161 unsigned i;
2163 vno->opcode = TREE_CODE (op);
2164 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2165 vno->type = TREE_TYPE (op);
2166 for (i = 0; i < vno->length; ++i)
2167 vno->op[i] = TREE_OPERAND (op, i);
2170 /* Return the number of operands for a vn_nary ops structure from STMT. */
2172 static unsigned int
2173 vn_nary_length_from_stmt (gimple stmt)
2175 switch (gimple_assign_rhs_code (stmt))
2177 case REALPART_EXPR:
2178 case IMAGPART_EXPR:
2179 case VIEW_CONVERT_EXPR:
2180 return 1;
2182 case BIT_FIELD_REF:
2183 return 3;
2185 case CONSTRUCTOR:
2186 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2188 default:
2189 return gimple_num_ops (stmt) - 1;
2193 /* Initialize VNO from STMT. */
2195 static void
2196 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2198 unsigned i;
2200 vno->opcode = gimple_assign_rhs_code (stmt);
2201 vno->type = gimple_expr_type (stmt);
2202 switch (vno->opcode)
2204 case REALPART_EXPR:
2205 case IMAGPART_EXPR:
2206 case VIEW_CONVERT_EXPR:
2207 vno->length = 1;
2208 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2209 break;
2211 case BIT_FIELD_REF:
2212 vno->length = 3;
2213 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2214 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2215 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2216 break;
2218 case CONSTRUCTOR:
2219 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2220 for (i = 0; i < vno->length; ++i)
2221 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2222 break;
2224 default:
2225 gcc_checking_assert (!gimple_assign_single_p (stmt));
2226 vno->length = gimple_num_ops (stmt) - 1;
2227 for (i = 0; i < vno->length; ++i)
2228 vno->op[i] = gimple_op (stmt, i + 1);
2232 /* Compute the hashcode for VNO and look for it in the hash table;
2233 return the resulting value number if it exists in the hash table.
2234 Return NULL_TREE if it does not exist in the hash table or if the
2235 result field of the operation is NULL. VNRESULT will contain the
2236 vn_nary_op_t from the hashtable if it exists. */
2238 static tree
2239 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2241 void **slot;
2243 if (vnresult)
2244 *vnresult = NULL;
2246 vno->hashcode = vn_nary_op_compute_hash (vno);
2247 slot = htab_find_slot_with_hash (current_info->nary, vno, vno->hashcode,
2248 NO_INSERT);
2249 if (!slot && current_info == optimistic_info)
2250 slot = htab_find_slot_with_hash (valid_info->nary, vno, vno->hashcode,
2251 NO_INSERT);
2252 if (!slot)
2253 return NULL_TREE;
2254 if (vnresult)
2255 *vnresult = (vn_nary_op_t)*slot;
2256 return ((vn_nary_op_t)*slot)->result;
2259 /* Lookup a n-ary operation by its pieces and return the resulting value
2260 number if it exists in the hash table. Return NULL_TREE if it does
2261 not exist in the hash table or if the result field of the operation
2262 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2263 if it exists. */
2265 tree
2266 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2267 tree type, tree *ops, vn_nary_op_t *vnresult)
2269 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2270 sizeof_vn_nary_op (length));
2271 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2272 return vn_nary_op_lookup_1 (vno1, vnresult);
2275 /* Lookup OP in the current hash table, and return the resulting value
2276 number if it exists in the hash table. Return NULL_TREE if it does
2277 not exist in the hash table or if the result field of the operation
2278 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2279 if it exists. */
2281 tree
2282 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2284 vn_nary_op_t vno1
2285 = XALLOCAVAR (struct vn_nary_op_s,
2286 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2287 init_vn_nary_op_from_op (vno1, op);
2288 return vn_nary_op_lookup_1 (vno1, vnresult);
2291 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2292 value number if it exists in the hash table. Return NULL_TREE if
2293 it does not exist in the hash table. VNRESULT will contain the
2294 vn_nary_op_t from the hashtable if it exists. */
2296 tree
2297 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2299 vn_nary_op_t vno1
2300 = XALLOCAVAR (struct vn_nary_op_s,
2301 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2302 init_vn_nary_op_from_stmt (vno1, stmt);
2303 return vn_nary_op_lookup_1 (vno1, vnresult);
2306 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2308 static vn_nary_op_t
2309 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2311 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2314 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2315 obstack. */
2317 static vn_nary_op_t
2318 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2320 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2321 &current_info->nary_obstack);
2323 vno1->value_id = value_id;
2324 vno1->length = length;
2325 vno1->result = result;
2327 return vno1;
2330 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2331 VNO->HASHCODE first. */
2333 static vn_nary_op_t
2334 vn_nary_op_insert_into (vn_nary_op_t vno, htab_t table, bool compute_hash)
2336 void **slot;
2338 if (compute_hash)
2339 vno->hashcode = vn_nary_op_compute_hash (vno);
2341 slot = htab_find_slot_with_hash (table, vno, vno->hashcode, INSERT);
2342 gcc_assert (!*slot);
2344 *slot = vno;
2345 return vno;
2348 /* Insert a n-ary operation into the current hash table using it's
2349 pieces. Return the vn_nary_op_t structure we created and put in
2350 the hashtable. */
2352 vn_nary_op_t
2353 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2354 tree type, tree *ops,
2355 tree result, unsigned int value_id)
2357 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2358 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2359 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2362 /* Insert OP into the current hash table with a value number of
2363 RESULT. Return the vn_nary_op_t structure we created and put in
2364 the hashtable. */
2366 vn_nary_op_t
2367 vn_nary_op_insert (tree op, tree result)
2369 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2370 vn_nary_op_t vno1;
2372 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2373 init_vn_nary_op_from_op (vno1, op);
2374 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2377 /* Insert the rhs of STMT into the current hash table with a value number of
2378 RESULT. */
2380 vn_nary_op_t
2381 vn_nary_op_insert_stmt (gimple stmt, tree result)
2383 vn_nary_op_t vno1
2384 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2385 result, VN_INFO (result)->value_id);
2386 init_vn_nary_op_from_stmt (vno1, stmt);
2387 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2390 /* Compute a hashcode for PHI operation VP1 and return it. */
2392 static inline hashval_t
2393 vn_phi_compute_hash (vn_phi_t vp1)
2395 hashval_t result;
2396 int i;
2397 tree phi1op;
2398 tree type;
2400 result = vp1->block->index;
2402 /* If all PHI arguments are constants we need to distinguish
2403 the PHI node via its type. */
2404 type = vp1->type;
2405 result += vn_hash_type (type);
2407 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2409 if (phi1op == VN_TOP)
2410 continue;
2411 result = iterative_hash_expr (phi1op, result);
2414 return result;
2417 /* Return the computed hashcode for phi operation P1. */
2419 static hashval_t
2420 vn_phi_hash (const void *p1)
2422 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
2423 return vp1->hashcode;
2426 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2428 static int
2429 vn_phi_eq (const void *p1, const void *p2)
2431 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
2432 const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
2434 if (vp1->hashcode != vp2->hashcode)
2435 return false;
2437 if (vp1->block == vp2->block)
2439 int i;
2440 tree phi1op;
2442 /* If the PHI nodes do not have compatible types
2443 they are not the same. */
2444 if (!types_compatible_p (vp1->type, vp2->type))
2445 return false;
2447 /* Any phi in the same block will have it's arguments in the
2448 same edge order, because of how we store phi nodes. */
2449 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2451 tree phi2op = vp2->phiargs[i];
2452 if (phi1op == VN_TOP || phi2op == VN_TOP)
2453 continue;
2454 if (!expressions_equal_p (phi1op, phi2op))
2455 return false;
2457 return true;
2459 return false;
2462 static vec<tree> shared_lookup_phiargs;
2464 /* Lookup PHI in the current hash table, and return the resulting
2465 value number if it exists in the hash table. Return NULL_TREE if
2466 it does not exist in the hash table. */
2468 static tree
2469 vn_phi_lookup (gimple phi)
2471 void **slot;
2472 struct vn_phi_s vp1;
2473 unsigned i;
2475 shared_lookup_phiargs.truncate (0);
2477 /* Canonicalize the SSA_NAME's to their value number. */
2478 for (i = 0; i < gimple_phi_num_args (phi); i++)
2480 tree def = PHI_ARG_DEF (phi, i);
2481 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2482 shared_lookup_phiargs.safe_push (def);
2484 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2485 vp1.phiargs = shared_lookup_phiargs;
2486 vp1.block = gimple_bb (phi);
2487 vp1.hashcode = vn_phi_compute_hash (&vp1);
2488 slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
2489 NO_INSERT);
2490 if (!slot && current_info == optimistic_info)
2491 slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
2492 NO_INSERT);
2493 if (!slot)
2494 return NULL_TREE;
2495 return ((vn_phi_t)*slot)->result;
2498 /* Insert PHI into the current hash table with a value number of
2499 RESULT. */
2501 static vn_phi_t
2502 vn_phi_insert (gimple phi, tree result)
2504 void **slot;
2505 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2506 unsigned i;
2507 vec<tree> args = vNULL;
2509 /* Canonicalize the SSA_NAME's to their value number. */
2510 for (i = 0; i < gimple_phi_num_args (phi); i++)
2512 tree def = PHI_ARG_DEF (phi, i);
2513 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2514 args.safe_push (def);
2516 vp1->value_id = VN_INFO (result)->value_id;
2517 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2518 vp1->phiargs = args;
2519 vp1->block = gimple_bb (phi);
2520 vp1->result = result;
2521 vp1->hashcode = vn_phi_compute_hash (vp1);
2523 slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
2524 INSERT);
2526 /* Because we iterate over phi operations more than once, it's
2527 possible the slot might already exist here, hence no assert.*/
2528 *slot = vp1;
2529 return vp1;
2533 /* Print set of components in strongly connected component SCC to OUT. */
2535 static void
2536 print_scc (FILE *out, vec<tree> scc)
2538 tree var;
2539 unsigned int i;
2541 fprintf (out, "SCC consists of:");
2542 FOR_EACH_VEC_ELT (scc, i, var)
2544 fprintf (out, " ");
2545 print_generic_expr (out, var, 0);
2547 fprintf (out, "\n");
2550 /* Set the value number of FROM to TO, return true if it has changed
2551 as a result. */
2553 static inline bool
2554 set_ssa_val_to (tree from, tree to)
2556 tree currval = SSA_VAL (from);
2558 if (from != to)
2560 if (currval == from)
2562 if (dump_file && (dump_flags & TDF_DETAILS))
2564 fprintf (dump_file, "Not changing value number of ");
2565 print_generic_expr (dump_file, from, 0);
2566 fprintf (dump_file, " from VARYING to ");
2567 print_generic_expr (dump_file, to, 0);
2568 fprintf (dump_file, "\n");
2570 return false;
2572 else if (TREE_CODE (to) == SSA_NAME
2573 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2574 to = from;
2577 /* The only thing we allow as value numbers are VN_TOP, ssa_names
2578 and invariants. So assert that here. */
2579 gcc_assert (to != NULL_TREE
2580 && (to == VN_TOP
2581 || TREE_CODE (to) == SSA_NAME
2582 || is_gimple_min_invariant (to)));
2584 if (dump_file && (dump_flags & TDF_DETAILS))
2586 fprintf (dump_file, "Setting value number of ");
2587 print_generic_expr (dump_file, from, 0);
2588 fprintf (dump_file, " to ");
2589 print_generic_expr (dump_file, to, 0);
2592 if (currval != to && !operand_equal_p (currval, to, OEP_PURE_SAME))
2594 VN_INFO (from)->valnum = to;
2595 if (dump_file && (dump_flags & TDF_DETAILS))
2596 fprintf (dump_file, " (changed)\n");
2597 return true;
2599 if (dump_file && (dump_flags & TDF_DETAILS))
2600 fprintf (dump_file, "\n");
2601 return false;
2604 /* Mark as processed all the definitions in the defining stmt of USE, or
2605 the USE itself. */
2607 static void
2608 mark_use_processed (tree use)
2610 ssa_op_iter iter;
2611 def_operand_p defp;
2612 gimple stmt = SSA_NAME_DEF_STMT (use);
2614 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2616 VN_INFO (use)->use_processed = true;
2617 return;
2620 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2622 tree def = DEF_FROM_PTR (defp);
2624 VN_INFO (def)->use_processed = true;
2628 /* Set all definitions in STMT to value number to themselves.
2629 Return true if a value number changed. */
2631 static bool
2632 defs_to_varying (gimple stmt)
2634 bool changed = false;
2635 ssa_op_iter iter;
2636 def_operand_p defp;
2638 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2640 tree def = DEF_FROM_PTR (defp);
2641 changed |= set_ssa_val_to (def, def);
2643 return changed;
2646 static bool expr_has_constants (tree expr);
2647 static tree valueize_expr (tree expr);
2649 /* Visit a copy between LHS and RHS, return true if the value number
2650 changed. */
2652 static bool
2653 visit_copy (tree lhs, tree rhs)
2655 /* The copy may have a more interesting constant filled expression
2656 (we don't, since we know our RHS is just an SSA name). */
2657 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2658 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2660 /* And finally valueize. */
2661 rhs = SSA_VAL (rhs);
2663 return set_ssa_val_to (lhs, rhs);
2666 /* Visit a nary operator RHS, value number it, and return true if the
2667 value number of LHS has changed as a result. */
2669 static bool
2670 visit_nary_op (tree lhs, gimple stmt)
2672 bool changed = false;
2673 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2675 if (result)
2676 changed = set_ssa_val_to (lhs, result);
2677 else
2679 changed = set_ssa_val_to (lhs, lhs);
2680 vn_nary_op_insert_stmt (stmt, lhs);
2683 return changed;
2686 /* Visit a call STMT storing into LHS. Return true if the value number
2687 of the LHS has changed as a result. */
2689 static bool
2690 visit_reference_op_call (tree lhs, gimple stmt)
2692 bool changed = false;
2693 struct vn_reference_s vr1;
2694 vn_reference_t vnresult = NULL;
2695 tree vuse = gimple_vuse (stmt);
2696 tree vdef = gimple_vdef (stmt);
2698 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2699 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2700 lhs = NULL_TREE;
2702 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2703 vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2704 vr1.type = gimple_expr_type (stmt);
2705 vr1.set = 0;
2706 vr1.hashcode = vn_reference_compute_hash (&vr1);
2707 vn_reference_lookup_1 (&vr1, &vnresult);
2709 if (vnresult)
2711 if (vnresult->result_vdef)
2712 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2714 if (!vnresult->result && lhs)
2715 vnresult->result = lhs;
2717 if (vnresult->result && lhs)
2719 changed |= set_ssa_val_to (lhs, vnresult->result);
2721 if (VN_INFO (vnresult->result)->has_constants)
2722 VN_INFO (lhs)->has_constants = true;
2725 else
2727 void **slot;
2728 vn_reference_t vr2;
2729 if (vdef)
2730 changed |= set_ssa_val_to (vdef, vdef);
2731 if (lhs)
2732 changed |= set_ssa_val_to (lhs, lhs);
2733 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2734 vr2->vuse = vr1.vuse;
2735 vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2736 vr2->type = vr1.type;
2737 vr2->set = vr1.set;
2738 vr2->hashcode = vr1.hashcode;
2739 vr2->result = lhs;
2740 vr2->result_vdef = vdef;
2741 slot = htab_find_slot_with_hash (current_info->references,
2742 vr2, vr2->hashcode, INSERT);
2743 if (*slot)
2744 free_reference (*slot);
2745 *slot = vr2;
2748 return changed;
2751 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2752 and return true if the value number of the LHS has changed as a result. */
2754 static bool
2755 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2757 bool changed = false;
2758 tree last_vuse;
2759 tree result;
2761 last_vuse = gimple_vuse (stmt);
2762 last_vuse_ptr = &last_vuse;
2763 result = vn_reference_lookup (op, gimple_vuse (stmt),
2764 default_vn_walk_kind, NULL);
2765 last_vuse_ptr = NULL;
2767 /* If we have a VCE, try looking up its operand as it might be stored in
2768 a different type. */
2769 if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2770 result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2771 default_vn_walk_kind, NULL);
2773 /* We handle type-punning through unions by value-numbering based
2774 on offset and size of the access. Be prepared to handle a
2775 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2776 if (result
2777 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2779 /* We will be setting the value number of lhs to the value number
2780 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2781 So first simplify and lookup this expression to see if it
2782 is already available. */
2783 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2784 if ((CONVERT_EXPR_P (val)
2785 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2786 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2788 tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2789 if ((CONVERT_EXPR_P (tem)
2790 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2791 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2792 TREE_TYPE (val), tem)))
2793 val = tem;
2795 result = val;
2796 if (!is_gimple_min_invariant (val)
2797 && TREE_CODE (val) != SSA_NAME)
2798 result = vn_nary_op_lookup (val, NULL);
2799 /* If the expression is not yet available, value-number lhs to
2800 a new SSA_NAME we create. */
2801 if (!result)
2803 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
2804 "vntemp");
2805 /* Initialize value-number information properly. */
2806 VN_INFO_GET (result)->valnum = result;
2807 VN_INFO (result)->value_id = get_next_value_id ();
2808 VN_INFO (result)->expr = val;
2809 VN_INFO (result)->has_constants = expr_has_constants (val);
2810 VN_INFO (result)->needs_insertion = true;
2811 /* As all "inserted" statements are singleton SCCs, insert
2812 to the valid table. This is strictly needed to
2813 avoid re-generating new value SSA_NAMEs for the same
2814 expression during SCC iteration over and over (the
2815 optimistic table gets cleared after each iteration).
2816 We do not need to insert into the optimistic table, as
2817 lookups there will fall back to the valid table. */
2818 if (current_info == optimistic_info)
2820 current_info = valid_info;
2821 vn_nary_op_insert (val, result);
2822 current_info = optimistic_info;
2824 else
2825 vn_nary_op_insert (val, result);
2826 if (dump_file && (dump_flags & TDF_DETAILS))
2828 fprintf (dump_file, "Inserting name ");
2829 print_generic_expr (dump_file, result, 0);
2830 fprintf (dump_file, " for expression ");
2831 print_generic_expr (dump_file, val, 0);
2832 fprintf (dump_file, "\n");
2837 if (result)
2839 changed = set_ssa_val_to (lhs, result);
2840 if (TREE_CODE (result) == SSA_NAME
2841 && VN_INFO (result)->has_constants)
2843 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2844 VN_INFO (lhs)->has_constants = true;
2847 else
2849 changed = set_ssa_val_to (lhs, lhs);
2850 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
2853 return changed;
2857 /* Visit a store to a reference operator LHS, part of STMT, value number it,
2858 and return true if the value number of the LHS has changed as a result. */
2860 static bool
2861 visit_reference_op_store (tree lhs, tree op, gimple stmt)
2863 bool changed = false;
2864 vn_reference_t vnresult = NULL;
2865 tree result, assign;
2866 bool resultsame = false;
2867 tree vuse = gimple_vuse (stmt);
2868 tree vdef = gimple_vdef (stmt);
2870 /* First we want to lookup using the *vuses* from the store and see
2871 if there the last store to this location with the same address
2872 had the same value.
2874 The vuses represent the memory state before the store. If the
2875 memory state, address, and value of the store is the same as the
2876 last store to this location, then this store will produce the
2877 same memory state as that store.
2879 In this case the vdef versions for this store are value numbered to those
2880 vuse versions, since they represent the same memory state after
2881 this store.
2883 Otherwise, the vdefs for the store are used when inserting into
2884 the table, since the store generates a new memory state. */
2886 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
2888 if (result)
2890 if (TREE_CODE (result) == SSA_NAME)
2891 result = SSA_VAL (result);
2892 if (TREE_CODE (op) == SSA_NAME)
2893 op = SSA_VAL (op);
2894 resultsame = expressions_equal_p (result, op);
2897 if (!result || !resultsame)
2899 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2900 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
2901 if (vnresult)
2903 VN_INFO (vdef)->use_processed = true;
2904 return set_ssa_val_to (vdef, vnresult->result_vdef);
2908 if (!result || !resultsame)
2910 if (dump_file && (dump_flags & TDF_DETAILS))
2912 fprintf (dump_file, "No store match\n");
2913 fprintf (dump_file, "Value numbering store ");
2914 print_generic_expr (dump_file, lhs, 0);
2915 fprintf (dump_file, " to ");
2916 print_generic_expr (dump_file, op, 0);
2917 fprintf (dump_file, "\n");
2919 /* Have to set value numbers before insert, since insert is
2920 going to valueize the references in-place. */
2921 if (vdef)
2923 changed |= set_ssa_val_to (vdef, vdef);
2926 /* Do not insert structure copies into the tables. */
2927 if (is_gimple_min_invariant (op)
2928 || is_gimple_reg (op))
2929 vn_reference_insert (lhs, op, vdef, NULL);
2931 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2932 vn_reference_insert (assign, lhs, vuse, vdef);
2934 else
2936 /* We had a match, so value number the vdef to have the value
2937 number of the vuse it came from. */
2939 if (dump_file && (dump_flags & TDF_DETAILS))
2940 fprintf (dump_file, "Store matched earlier value,"
2941 "value numbering store vdefs to matching vuses.\n");
2943 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
2946 return changed;
2949 /* Visit and value number PHI, return true if the value number
2950 changed. */
2952 static bool
2953 visit_phi (gimple phi)
2955 bool changed = false;
2956 tree result;
2957 tree sameval = VN_TOP;
2958 bool allsame = true;
2959 unsigned i;
2961 /* TODO: We could check for this in init_sccvn, and replace this
2962 with a gcc_assert. */
2963 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
2964 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2966 /* See if all non-TOP arguments have the same value. TOP is
2967 equivalent to everything, so we can ignore it. */
2968 for (i = 0; i < gimple_phi_num_args (phi); i++)
2970 tree def = PHI_ARG_DEF (phi, i);
2972 if (TREE_CODE (def) == SSA_NAME)
2973 def = SSA_VAL (def);
2974 if (def == VN_TOP)
2975 continue;
2976 if (sameval == VN_TOP)
2978 sameval = def;
2980 else
2982 if (!expressions_equal_p (def, sameval))
2984 allsame = false;
2985 break;
2990 /* If all value numbered to the same value, the phi node has that
2991 value. */
2992 if (allsame)
2994 if (is_gimple_min_invariant (sameval))
2996 VN_INFO (PHI_RESULT (phi))->has_constants = true;
2997 VN_INFO (PHI_RESULT (phi))->expr = sameval;
2999 else
3001 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3002 VN_INFO (PHI_RESULT (phi))->expr = sameval;
3005 if (TREE_CODE (sameval) == SSA_NAME)
3006 return visit_copy (PHI_RESULT (phi), sameval);
3008 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3011 /* Otherwise, see if it is equivalent to a phi node in this block. */
3012 result = vn_phi_lookup (phi);
3013 if (result)
3015 if (TREE_CODE (result) == SSA_NAME)
3016 changed = visit_copy (PHI_RESULT (phi), result);
3017 else
3018 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3020 else
3022 vn_phi_insert (phi, PHI_RESULT (phi));
3023 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3024 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3025 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3028 return changed;
3031 /* Return true if EXPR contains constants. */
3033 static bool
3034 expr_has_constants (tree expr)
3036 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3038 case tcc_unary:
3039 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3041 case tcc_binary:
3042 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3043 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3044 /* Constants inside reference ops are rarely interesting, but
3045 it can take a lot of looking to find them. */
3046 case tcc_reference:
3047 case tcc_declaration:
3048 return false;
3049 default:
3050 return is_gimple_min_invariant (expr);
3052 return false;
3055 /* Return true if STMT contains constants. */
3057 static bool
3058 stmt_has_constants (gimple stmt)
3060 tree tem;
3062 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3063 return false;
3065 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3067 case GIMPLE_TERNARY_RHS:
3068 tem = gimple_assign_rhs3 (stmt);
3069 if (TREE_CODE (tem) == SSA_NAME)
3070 tem = SSA_VAL (tem);
3071 if (is_gimple_min_invariant (tem))
3072 return true;
3073 /* Fallthru. */
3075 case GIMPLE_BINARY_RHS:
3076 tem = gimple_assign_rhs2 (stmt);
3077 if (TREE_CODE (tem) == SSA_NAME)
3078 tem = SSA_VAL (tem);
3079 if (is_gimple_min_invariant (tem))
3080 return true;
3081 /* Fallthru. */
3083 case GIMPLE_SINGLE_RHS:
3084 /* Constants inside reference ops are rarely interesting, but
3085 it can take a lot of looking to find them. */
3086 case GIMPLE_UNARY_RHS:
3087 tem = gimple_assign_rhs1 (stmt);
3088 if (TREE_CODE (tem) == SSA_NAME)
3089 tem = SSA_VAL (tem);
3090 return is_gimple_min_invariant (tem);
3092 default:
3093 gcc_unreachable ();
3095 return false;
3098 /* Replace SSA_NAMES in expr with their value numbers, and return the
3099 result.
3100 This is performed in place. */
3102 static tree
3103 valueize_expr (tree expr)
3105 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3107 case tcc_binary:
3108 TREE_OPERAND (expr, 1) = vn_valueize (TREE_OPERAND (expr, 1));
3109 /* Fallthru. */
3110 case tcc_unary:
3111 TREE_OPERAND (expr, 0) = vn_valueize (TREE_OPERAND (expr, 0));
3112 break;
3113 default:;
3115 return expr;
3118 /* Simplify the binary expression RHS, and return the result if
3119 simplified. */
3121 static tree
3122 simplify_binary_expression (gimple stmt)
3124 tree result = NULL_TREE;
3125 tree op0 = gimple_assign_rhs1 (stmt);
3126 tree op1 = gimple_assign_rhs2 (stmt);
3127 enum tree_code code = gimple_assign_rhs_code (stmt);
3129 /* This will not catch every single case we could combine, but will
3130 catch those with constants. The goal here is to simultaneously
3131 combine constants between expressions, but avoid infinite
3132 expansion of expressions during simplification. */
3133 if (TREE_CODE (op0) == SSA_NAME)
3135 if (VN_INFO (op0)->has_constants
3136 || TREE_CODE_CLASS (code) == tcc_comparison
3137 || code == COMPLEX_EXPR)
3138 op0 = valueize_expr (vn_get_expr_for (op0));
3139 else
3140 op0 = vn_valueize (op0);
3143 if (TREE_CODE (op1) == SSA_NAME)
3145 if (VN_INFO (op1)->has_constants
3146 || code == COMPLEX_EXPR)
3147 op1 = valueize_expr (vn_get_expr_for (op1));
3148 else
3149 op1 = vn_valueize (op1);
3152 /* Pointer plus constant can be represented as invariant address.
3153 Do so to allow further propatation, see also tree forwprop. */
3154 if (code == POINTER_PLUS_EXPR
3155 && host_integerp (op1, 1)
3156 && TREE_CODE (op0) == ADDR_EXPR
3157 && is_gimple_min_invariant (op0))
3158 return build_invariant_address (TREE_TYPE (op0),
3159 TREE_OPERAND (op0, 0),
3160 TREE_INT_CST_LOW (op1));
3162 /* Avoid folding if nothing changed. */
3163 if (op0 == gimple_assign_rhs1 (stmt)
3164 && op1 == gimple_assign_rhs2 (stmt))
3165 return NULL_TREE;
3167 fold_defer_overflow_warnings ();
3169 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3170 if (result)
3171 STRIP_USELESS_TYPE_CONVERSION (result);
3173 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3174 stmt, 0);
3176 /* Make sure result is not a complex expression consisting
3177 of operators of operators (IE (a + b) + (a + c))
3178 Otherwise, we will end up with unbounded expressions if
3179 fold does anything at all. */
3180 if (result && valid_gimple_rhs_p (result))
3181 return result;
3183 return NULL_TREE;
3186 /* Simplify the unary expression RHS, and return the result if
3187 simplified. */
3189 static tree
3190 simplify_unary_expression (gimple stmt)
3192 tree result = NULL_TREE;
3193 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3194 enum tree_code code = gimple_assign_rhs_code (stmt);
3196 /* We handle some tcc_reference codes here that are all
3197 GIMPLE_ASSIGN_SINGLE codes. */
3198 if (code == REALPART_EXPR
3199 || code == IMAGPART_EXPR
3200 || code == VIEW_CONVERT_EXPR
3201 || code == BIT_FIELD_REF)
3202 op0 = TREE_OPERAND (op0, 0);
3204 if (TREE_CODE (op0) != SSA_NAME)
3205 return NULL_TREE;
3207 orig_op0 = op0;
3208 if (VN_INFO (op0)->has_constants)
3209 op0 = valueize_expr (vn_get_expr_for (op0));
3210 else if (CONVERT_EXPR_CODE_P (code)
3211 || code == REALPART_EXPR
3212 || code == IMAGPART_EXPR
3213 || code == VIEW_CONVERT_EXPR
3214 || code == BIT_FIELD_REF)
3216 /* We want to do tree-combining on conversion-like expressions.
3217 Make sure we feed only SSA_NAMEs or constants to fold though. */
3218 tree tem = valueize_expr (vn_get_expr_for (op0));
3219 if (UNARY_CLASS_P (tem)
3220 || BINARY_CLASS_P (tem)
3221 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3222 || TREE_CODE (tem) == SSA_NAME
3223 || TREE_CODE (tem) == CONSTRUCTOR
3224 || is_gimple_min_invariant (tem))
3225 op0 = tem;
3228 /* Avoid folding if nothing changed, but remember the expression. */
3229 if (op0 == orig_op0)
3230 return NULL_TREE;
3232 if (code == BIT_FIELD_REF)
3234 tree rhs = gimple_assign_rhs1 (stmt);
3235 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3236 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3238 else
3239 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3240 if (result)
3242 STRIP_USELESS_TYPE_CONVERSION (result);
3243 if (valid_gimple_rhs_p (result))
3244 return result;
3247 return NULL_TREE;
3250 /* Try to simplify RHS using equivalences and constant folding. */
3252 static tree
3253 try_to_simplify (gimple stmt)
3255 enum tree_code code = gimple_assign_rhs_code (stmt);
3256 tree tem;
3258 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3259 in this case, there is no point in doing extra work. */
3260 if (code == SSA_NAME)
3261 return NULL_TREE;
3263 /* First try constant folding based on our current lattice. */
3264 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize);
3265 if (tem
3266 && (TREE_CODE (tem) == SSA_NAME
3267 || is_gimple_min_invariant (tem)))
3268 return tem;
3270 /* If that didn't work try combining multiple statements. */
3271 switch (TREE_CODE_CLASS (code))
3273 case tcc_reference:
3274 /* Fallthrough for some unary codes that can operate on registers. */
3275 if (!(code == REALPART_EXPR
3276 || code == IMAGPART_EXPR
3277 || code == VIEW_CONVERT_EXPR
3278 || code == BIT_FIELD_REF))
3279 break;
3280 /* We could do a little more with unary ops, if they expand
3281 into binary ops, but it's debatable whether it is worth it. */
3282 case tcc_unary:
3283 return simplify_unary_expression (stmt);
3285 case tcc_comparison:
3286 case tcc_binary:
3287 return simplify_binary_expression (stmt);
3289 default:
3290 break;
3293 return NULL_TREE;
3296 /* Visit and value number USE, return true if the value number
3297 changed. */
3299 static bool
3300 visit_use (tree use)
3302 bool changed = false;
3303 gimple stmt = SSA_NAME_DEF_STMT (use);
3305 mark_use_processed (use);
3307 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3308 if (dump_file && (dump_flags & TDF_DETAILS)
3309 && !SSA_NAME_IS_DEFAULT_DEF (use))
3311 fprintf (dump_file, "Value numbering ");
3312 print_generic_expr (dump_file, use, 0);
3313 fprintf (dump_file, " stmt = ");
3314 print_gimple_stmt (dump_file, stmt, 0, 0);
3317 /* Handle uninitialized uses. */
3318 if (SSA_NAME_IS_DEFAULT_DEF (use))
3319 changed = set_ssa_val_to (use, use);
3320 else
3322 if (gimple_code (stmt) == GIMPLE_PHI)
3323 changed = visit_phi (stmt);
3324 else if (gimple_has_volatile_ops (stmt))
3325 changed = defs_to_varying (stmt);
3326 else if (is_gimple_assign (stmt))
3328 enum tree_code code = gimple_assign_rhs_code (stmt);
3329 tree lhs = gimple_assign_lhs (stmt);
3330 tree rhs1 = gimple_assign_rhs1 (stmt);
3331 tree simplified;
3333 /* Shortcut for copies. Simplifying copies is pointless,
3334 since we copy the expression and value they represent. */
3335 if (code == SSA_NAME
3336 && TREE_CODE (lhs) == SSA_NAME)
3338 changed = visit_copy (lhs, rhs1);
3339 goto done;
3341 simplified = try_to_simplify (stmt);
3342 if (simplified)
3344 if (dump_file && (dump_flags & TDF_DETAILS))
3346 fprintf (dump_file, "RHS ");
3347 print_gimple_expr (dump_file, stmt, 0, 0);
3348 fprintf (dump_file, " simplified to ");
3349 print_generic_expr (dump_file, simplified, 0);
3350 if (TREE_CODE (lhs) == SSA_NAME)
3351 fprintf (dump_file, " has constants %d\n",
3352 expr_has_constants (simplified));
3353 else
3354 fprintf (dump_file, "\n");
3357 /* Setting value numbers to constants will occasionally
3358 screw up phi congruence because constants are not
3359 uniquely associated with a single ssa name that can be
3360 looked up. */
3361 if (simplified
3362 && is_gimple_min_invariant (simplified)
3363 && TREE_CODE (lhs) == SSA_NAME)
3365 VN_INFO (lhs)->expr = simplified;
3366 VN_INFO (lhs)->has_constants = true;
3367 changed = set_ssa_val_to (lhs, simplified);
3368 goto done;
3370 else if (simplified
3371 && TREE_CODE (simplified) == SSA_NAME
3372 && TREE_CODE (lhs) == SSA_NAME)
3374 changed = visit_copy (lhs, simplified);
3375 goto done;
3377 else if (simplified)
3379 if (TREE_CODE (lhs) == SSA_NAME)
3381 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3382 /* We have to unshare the expression or else
3383 valuizing may change the IL stream. */
3384 VN_INFO (lhs)->expr = unshare_expr (simplified);
3387 else if (stmt_has_constants (stmt)
3388 && TREE_CODE (lhs) == SSA_NAME)
3389 VN_INFO (lhs)->has_constants = true;
3390 else if (TREE_CODE (lhs) == SSA_NAME)
3392 /* We reset expr and constantness here because we may
3393 have been value numbering optimistically, and
3394 iterating. They may become non-constant in this case,
3395 even if they were optimistically constant. */
3397 VN_INFO (lhs)->has_constants = false;
3398 VN_INFO (lhs)->expr = NULL_TREE;
3401 if ((TREE_CODE (lhs) == SSA_NAME
3402 /* We can substitute SSA_NAMEs that are live over
3403 abnormal edges with their constant value. */
3404 && !(gimple_assign_copy_p (stmt)
3405 && is_gimple_min_invariant (rhs1))
3406 && !(simplified
3407 && is_gimple_min_invariant (simplified))
3408 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3409 /* Stores or copies from SSA_NAMEs that are live over
3410 abnormal edges are a problem. */
3411 || (code == SSA_NAME
3412 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3413 changed = defs_to_varying (stmt);
3414 else if (REFERENCE_CLASS_P (lhs)
3415 || DECL_P (lhs))
3416 changed = visit_reference_op_store (lhs, rhs1, stmt);
3417 else if (TREE_CODE (lhs) == SSA_NAME)
3419 if ((gimple_assign_copy_p (stmt)
3420 && is_gimple_min_invariant (rhs1))
3421 || (simplified
3422 && is_gimple_min_invariant (simplified)))
3424 VN_INFO (lhs)->has_constants = true;
3425 if (simplified)
3426 changed = set_ssa_val_to (lhs, simplified);
3427 else
3428 changed = set_ssa_val_to (lhs, rhs1);
3430 else
3432 /* First try to lookup the simplified expression. */
3433 if (simplified)
3435 enum gimple_rhs_class rhs_class;
3438 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3439 if ((rhs_class == GIMPLE_UNARY_RHS
3440 || rhs_class == GIMPLE_BINARY_RHS
3441 || rhs_class == GIMPLE_TERNARY_RHS)
3442 && valid_gimple_rhs_p (simplified))
3444 tree result = vn_nary_op_lookup (simplified, NULL);
3445 if (result)
3447 changed = set_ssa_val_to (lhs, result);
3448 goto done;
3453 /* Otherwise visit the original statement. */
3454 switch (vn_get_stmt_kind (stmt))
3456 case VN_NARY:
3457 changed = visit_nary_op (lhs, stmt);
3458 break;
3459 case VN_REFERENCE:
3460 changed = visit_reference_op_load (lhs, rhs1, stmt);
3461 break;
3462 default:
3463 changed = defs_to_varying (stmt);
3464 break;
3468 else
3469 changed = defs_to_varying (stmt);
3471 else if (is_gimple_call (stmt))
3473 tree lhs = gimple_call_lhs (stmt);
3475 /* ??? We could try to simplify calls. */
3477 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3479 if (stmt_has_constants (stmt))
3480 VN_INFO (lhs)->has_constants = true;
3481 else
3483 /* We reset expr and constantness here because we may
3484 have been value numbering optimistically, and
3485 iterating. They may become non-constant in this case,
3486 even if they were optimistically constant. */
3487 VN_INFO (lhs)->has_constants = false;
3488 VN_INFO (lhs)->expr = NULL_TREE;
3491 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3493 changed = defs_to_varying (stmt);
3494 goto done;
3498 if (!gimple_call_internal_p (stmt)
3499 && (/* Calls to the same function with the same vuse
3500 and the same operands do not necessarily return the same
3501 value, unless they're pure or const. */
3502 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3503 /* If calls have a vdef, subsequent calls won't have
3504 the same incoming vuse. So, if 2 calls with vdef have the
3505 same vuse, we know they're not subsequent.
3506 We can value number 2 calls to the same function with the
3507 same vuse and the same operands which are not subsequent
3508 the same, because there is no code in the program that can
3509 compare the 2 values... */
3510 || (gimple_vdef (stmt)
3511 /* ... unless the call returns a pointer which does
3512 not alias with anything else. In which case the
3513 information that the values are distinct are encoded
3514 in the IL. */
3515 && !(gimple_call_return_flags (stmt) & ERF_NOALIAS))))
3516 changed = visit_reference_op_call (lhs, stmt);
3517 else
3518 changed = defs_to_varying (stmt);
3520 else
3521 changed = defs_to_varying (stmt);
3523 done:
3524 return changed;
3527 /* Compare two operands by reverse postorder index */
3529 static int
3530 compare_ops (const void *pa, const void *pb)
3532 const tree opa = *((const tree *)pa);
3533 const tree opb = *((const tree *)pb);
3534 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3535 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3536 basic_block bba;
3537 basic_block bbb;
3539 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3540 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3541 else if (gimple_nop_p (opstmta))
3542 return -1;
3543 else if (gimple_nop_p (opstmtb))
3544 return 1;
3546 bba = gimple_bb (opstmta);
3547 bbb = gimple_bb (opstmtb);
3549 if (!bba && !bbb)
3550 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3551 else if (!bba)
3552 return -1;
3553 else if (!bbb)
3554 return 1;
3556 if (bba == bbb)
3558 if (gimple_code (opstmta) == GIMPLE_PHI
3559 && gimple_code (opstmtb) == GIMPLE_PHI)
3560 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3561 else if (gimple_code (opstmta) == GIMPLE_PHI)
3562 return -1;
3563 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3564 return 1;
3565 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3566 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3567 else
3568 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3570 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3573 /* Sort an array containing members of a strongly connected component
3574 SCC so that the members are ordered by RPO number.
3575 This means that when the sort is complete, iterating through the
3576 array will give you the members in RPO order. */
3578 static void
3579 sort_scc (vec<tree> scc)
3581 scc.qsort (compare_ops);
3584 /* Insert the no longer used nary ONARY to the hash INFO. */
3586 static void
3587 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3589 size_t size = sizeof_vn_nary_op (onary->length);
3590 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3591 &info->nary_obstack);
3592 memcpy (nary, onary, size);
3593 vn_nary_op_insert_into (nary, info->nary, false);
3596 /* Insert the no longer used phi OPHI to the hash INFO. */
3598 static void
3599 copy_phi (vn_phi_t ophi, vn_tables_t info)
3601 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3602 void **slot;
3603 memcpy (phi, ophi, sizeof (*phi));
3604 ophi->phiargs.create (0);
3605 slot = htab_find_slot_with_hash (info->phis, phi, phi->hashcode, INSERT);
3606 gcc_assert (!*slot);
3607 *slot = phi;
3610 /* Insert the no longer used reference OREF to the hash INFO. */
3612 static void
3613 copy_reference (vn_reference_t oref, vn_tables_t info)
3615 vn_reference_t ref;
3616 void **slot;
3617 ref = (vn_reference_t) pool_alloc (info->references_pool);
3618 memcpy (ref, oref, sizeof (*ref));
3619 oref->operands.create (0);
3620 slot = htab_find_slot_with_hash (info->references, ref, ref->hashcode,
3621 INSERT);
3622 if (*slot)
3623 free_reference (*slot);
3624 *slot = ref;
3627 /* Process a strongly connected component in the SSA graph. */
3629 static void
3630 process_scc (vec<tree> scc)
3632 tree var;
3633 unsigned int i;
3634 unsigned int iterations = 0;
3635 bool changed = true;
3636 htab_iterator hi;
3637 vn_nary_op_t nary;
3638 vn_phi_t phi;
3639 vn_reference_t ref;
3641 /* If the SCC has a single member, just visit it. */
3642 if (scc.length () == 1)
3644 tree use = scc[0];
3645 if (VN_INFO (use)->use_processed)
3646 return;
3647 /* We need to make sure it doesn't form a cycle itself, which can
3648 happen for self-referential PHI nodes. In that case we would
3649 end up inserting an expression with VN_TOP operands into the
3650 valid table which makes us derive bogus equivalences later.
3651 The cheapest way to check this is to assume it for all PHI nodes. */
3652 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3653 /* Fallthru to iteration. */ ;
3654 else
3656 visit_use (use);
3657 return;
3661 /* Iterate over the SCC with the optimistic table until it stops
3662 changing. */
3663 current_info = optimistic_info;
3664 while (changed)
3666 changed = false;
3667 iterations++;
3668 if (dump_file && (dump_flags & TDF_DETAILS))
3669 fprintf (dump_file, "Starting iteration %d\n", iterations);
3670 /* As we are value-numbering optimistically we have to
3671 clear the expression tables and the simplified expressions
3672 in each iteration until we converge. */
3673 htab_empty (optimistic_info->nary);
3674 htab_empty (optimistic_info->phis);
3675 htab_empty (optimistic_info->references);
3676 obstack_free (&optimistic_info->nary_obstack, NULL);
3677 gcc_obstack_init (&optimistic_info->nary_obstack);
3678 empty_alloc_pool (optimistic_info->phis_pool);
3679 empty_alloc_pool (optimistic_info->references_pool);
3680 FOR_EACH_VEC_ELT (scc, i, var)
3681 VN_INFO (var)->expr = NULL_TREE;
3682 FOR_EACH_VEC_ELT (scc, i, var)
3683 changed |= visit_use (var);
3686 statistics_histogram_event (cfun, "SCC iterations", iterations);
3688 /* Finally, copy the contents of the no longer used optimistic
3689 table to the valid table. */
3690 FOR_EACH_HTAB_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hi)
3691 copy_nary (nary, valid_info);
3692 FOR_EACH_HTAB_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hi)
3693 copy_phi (phi, valid_info);
3694 FOR_EACH_HTAB_ELEMENT (optimistic_info->references, ref, vn_reference_t, hi)
3695 copy_reference (ref, valid_info);
3697 current_info = valid_info;
3701 /* Pop the components of the found SCC for NAME off the SCC stack
3702 and process them. Returns true if all went well, false if
3703 we run into resource limits. */
3705 static bool
3706 extract_and_process_scc_for_name (tree name)
3708 vec<tree> scc = vNULL;
3709 tree x;
3711 /* Found an SCC, pop the components off the SCC stack and
3712 process them. */
3715 x = sccstack.pop ();
3717 VN_INFO (x)->on_sccstack = false;
3718 scc.safe_push (x);
3719 } while (x != name);
3721 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3722 if (scc.length ()
3723 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3725 if (dump_file)
3726 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3727 "SCC size %u exceeding %u\n", scc.length (),
3728 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3730 scc.release ();
3731 return false;
3734 if (scc.length () > 1)
3735 sort_scc (scc);
3737 if (dump_file && (dump_flags & TDF_DETAILS))
3738 print_scc (dump_file, scc);
3740 process_scc (scc);
3742 scc.release ();
3744 return true;
3747 /* Depth first search on NAME to discover and process SCC's in the SSA
3748 graph.
3749 Execution of this algorithm relies on the fact that the SCC's are
3750 popped off the stack in topological order.
3751 Returns true if successful, false if we stopped processing SCC's due
3752 to resource constraints. */
3754 static bool
3755 DFS (tree name)
3757 vec<ssa_op_iter> itervec = vNULL;
3758 vec<tree> namevec = vNULL;
3759 use_operand_p usep = NULL;
3760 gimple defstmt;
3761 tree use;
3762 ssa_op_iter iter;
3764 start_over:
3765 /* SCC info */
3766 VN_INFO (name)->dfsnum = next_dfs_num++;
3767 VN_INFO (name)->visited = true;
3768 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3770 sccstack.safe_push (name);
3771 VN_INFO (name)->on_sccstack = true;
3772 defstmt = SSA_NAME_DEF_STMT (name);
3774 /* Recursively DFS on our operands, looking for SCC's. */
3775 if (!gimple_nop_p (defstmt))
3777 /* Push a new iterator. */
3778 if (gimple_code (defstmt) == GIMPLE_PHI)
3779 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3780 else
3781 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3783 else
3784 clear_and_done_ssa_iter (&iter);
3786 while (1)
3788 /* If we are done processing uses of a name, go up the stack
3789 of iterators and process SCCs as we found them. */
3790 if (op_iter_done (&iter))
3792 /* See if we found an SCC. */
3793 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3794 if (!extract_and_process_scc_for_name (name))
3796 namevec.release ();
3797 itervec.release ();
3798 return false;
3801 /* Check if we are done. */
3802 if (namevec.is_empty ())
3804 namevec.release ();
3805 itervec.release ();
3806 return true;
3809 /* Restore the last use walker and continue walking there. */
3810 use = name;
3811 name = namevec.pop ();
3812 memcpy (&iter, &itervec.last (),
3813 sizeof (ssa_op_iter));
3814 itervec.pop ();
3815 goto continue_walking;
3818 use = USE_FROM_PTR (usep);
3820 /* Since we handle phi nodes, we will sometimes get
3821 invariants in the use expression. */
3822 if (TREE_CODE (use) == SSA_NAME)
3824 if (! (VN_INFO (use)->visited))
3826 /* Recurse by pushing the current use walking state on
3827 the stack and starting over. */
3828 itervec.safe_push (iter);
3829 namevec.safe_push (name);
3830 name = use;
3831 goto start_over;
3833 continue_walking:
3834 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3835 VN_INFO (use)->low);
3837 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3838 && VN_INFO (use)->on_sccstack)
3840 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3841 VN_INFO (name)->low);
3845 usep = op_iter_next_use (&iter);
3849 /* Allocate a value number table. */
3851 static void
3852 allocate_vn_table (vn_tables_t table)
3854 table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
3855 table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
3856 table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
3857 free_reference);
3859 gcc_obstack_init (&table->nary_obstack);
3860 table->phis_pool = create_alloc_pool ("VN phis",
3861 sizeof (struct vn_phi_s),
3862 30);
3863 table->references_pool = create_alloc_pool ("VN references",
3864 sizeof (struct vn_reference_s),
3865 30);
3868 /* Free a value number table. */
3870 static void
3871 free_vn_table (vn_tables_t table)
3873 htab_delete (table->phis);
3874 htab_delete (table->nary);
3875 htab_delete (table->references);
3876 obstack_free (&table->nary_obstack, NULL);
3877 free_alloc_pool (table->phis_pool);
3878 free_alloc_pool (table->references_pool);
3881 static void
3882 init_scc_vn (void)
3884 size_t i;
3885 int j;
3886 int *rpo_numbers_temp;
3888 calculate_dominance_info (CDI_DOMINATORS);
3889 sccstack.create (0);
3890 constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
3891 free);
3893 constant_value_ids = BITMAP_ALLOC (NULL);
3895 next_dfs_num = 1;
3896 next_value_id = 1;
3898 vn_ssa_aux_table.create (num_ssa_names + 1);
3899 /* VEC_alloc doesn't actually grow it to the right size, it just
3900 preallocates the space to do so. */
3901 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
3902 gcc_obstack_init (&vn_ssa_aux_obstack);
3904 shared_lookup_phiargs.create (0);
3905 shared_lookup_references.create (0);
3906 rpo_numbers = XNEWVEC (int, last_basic_block);
3907 rpo_numbers_temp = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
3908 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3910 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3911 the i'th block in RPO order is bb. We want to map bb's to RPO
3912 numbers, so we need to rearrange this array. */
3913 for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3914 rpo_numbers[rpo_numbers_temp[j]] = j;
3916 XDELETE (rpo_numbers_temp);
3918 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3920 /* Create the VN_INFO structures, and initialize value numbers to
3921 TOP. */
3922 for (i = 0; i < num_ssa_names; i++)
3924 tree name = ssa_name (i);
3925 if (name)
3927 VN_INFO_GET (name)->valnum = VN_TOP;
3928 VN_INFO (name)->expr = NULL_TREE;
3929 VN_INFO (name)->value_id = 0;
3933 renumber_gimple_stmt_uids ();
3935 /* Create the valid and optimistic value numbering tables. */
3936 valid_info = XCNEW (struct vn_tables_s);
3937 allocate_vn_table (valid_info);
3938 optimistic_info = XCNEW (struct vn_tables_s);
3939 allocate_vn_table (optimistic_info);
3942 void
3943 free_scc_vn (void)
3945 size_t i;
3947 htab_delete (constant_to_value_id);
3948 BITMAP_FREE (constant_value_ids);
3949 shared_lookup_phiargs.release ();
3950 shared_lookup_references.release ();
3951 XDELETEVEC (rpo_numbers);
3953 for (i = 0; i < num_ssa_names; i++)
3955 tree name = ssa_name (i);
3956 if (name
3957 && VN_INFO (name)->needs_insertion)
3958 release_ssa_name (name);
3960 obstack_free (&vn_ssa_aux_obstack, NULL);
3961 vn_ssa_aux_table.release ();
3963 sccstack.release ();
3964 free_vn_table (valid_info);
3965 XDELETE (valid_info);
3966 free_vn_table (optimistic_info);
3967 XDELETE (optimistic_info);
3970 /* Set *ID according to RESULT. */
3972 static void
3973 set_value_id_for_result (tree result, unsigned int *id)
3975 if (result && TREE_CODE (result) == SSA_NAME)
3976 *id = VN_INFO (result)->value_id;
3977 else if (result && is_gimple_min_invariant (result))
3978 *id = get_or_alloc_constant_value_id (result);
3979 else
3980 *id = get_next_value_id ();
3983 /* Set the value ids in the valid hash tables. */
3985 static void
3986 set_hashtable_value_ids (void)
3988 htab_iterator hi;
3989 vn_nary_op_t vno;
3990 vn_reference_t vr;
3991 vn_phi_t vp;
3993 /* Now set the value ids of the things we had put in the hash
3994 table. */
3996 FOR_EACH_HTAB_ELEMENT (valid_info->nary,
3997 vno, vn_nary_op_t, hi)
3998 set_value_id_for_result (vno->result, &vno->value_id);
4000 FOR_EACH_HTAB_ELEMENT (valid_info->phis,
4001 vp, vn_phi_t, hi)
4002 set_value_id_for_result (vp->result, &vp->value_id);
4004 FOR_EACH_HTAB_ELEMENT (valid_info->references,
4005 vr, vn_reference_t, hi)
4006 set_value_id_for_result (vr->result, &vr->value_id);
4009 /* Do SCCVN. Returns true if it finished, false if we bailed out
4010 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4011 how we use the alias oracle walking during the VN process. */
4013 bool
4014 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4016 size_t i;
4017 tree param;
4019 default_vn_walk_kind = default_vn_walk_kind_;
4021 init_scc_vn ();
4022 current_info = valid_info;
4024 for (param = DECL_ARGUMENTS (current_function_decl);
4025 param;
4026 param = DECL_CHAIN (param))
4028 tree def = ssa_default_def (cfun, param);
4029 if (def)
4030 VN_INFO (def)->valnum = def;
4033 for (i = 1; i < num_ssa_names; ++i)
4035 tree name = ssa_name (i);
4036 if (name
4037 && VN_INFO (name)->visited == false
4038 && !has_zero_uses (name))
4039 if (!DFS (name))
4041 free_scc_vn ();
4042 return false;
4046 /* Initialize the value ids. */
4048 for (i = 1; i < num_ssa_names; ++i)
4050 tree name = ssa_name (i);
4051 vn_ssa_aux_t info;
4052 if (!name)
4053 continue;
4054 info = VN_INFO (name);
4055 if (info->valnum == name
4056 || info->valnum == VN_TOP)
4057 info->value_id = get_next_value_id ();
4058 else if (is_gimple_min_invariant (info->valnum))
4059 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4062 /* Propagate. */
4063 for (i = 1; i < num_ssa_names; ++i)
4065 tree name = ssa_name (i);
4066 vn_ssa_aux_t info;
4067 if (!name)
4068 continue;
4069 info = VN_INFO (name);
4070 if (TREE_CODE (info->valnum) == SSA_NAME
4071 && info->valnum != name
4072 && info->value_id != VN_INFO (info->valnum)->value_id)
4073 info->value_id = VN_INFO (info->valnum)->value_id;
4076 set_hashtable_value_ids ();
4078 if (dump_file && (dump_flags & TDF_DETAILS))
4080 fprintf (dump_file, "Value numbers:\n");
4081 for (i = 0; i < num_ssa_names; i++)
4083 tree name = ssa_name (i);
4084 if (name
4085 && VN_INFO (name)->visited
4086 && SSA_VAL (name) != name)
4088 print_generic_expr (dump_file, name, 0);
4089 fprintf (dump_file, " = ");
4090 print_generic_expr (dump_file, SSA_VAL (name), 0);
4091 fprintf (dump_file, "\n");
4096 return true;
4099 /* Return the maximum value id we have ever seen. */
4101 unsigned int
4102 get_max_value_id (void)
4104 return next_value_id;
4107 /* Return the next unique value id. */
4109 unsigned int
4110 get_next_value_id (void)
4112 return next_value_id++;
4116 /* Compare two expressions E1 and E2 and return true if they are equal. */
4118 bool
4119 expressions_equal_p (tree e1, tree e2)
4121 /* The obvious case. */
4122 if (e1 == e2)
4123 return true;
4125 /* If only one of them is null, they cannot be equal. */
4126 if (!e1 || !e2)
4127 return false;
4129 /* Now perform the actual comparison. */
4130 if (TREE_CODE (e1) == TREE_CODE (e2)
4131 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4132 return true;
4134 return false;
4138 /* Return true if the nary operation NARY may trap. This is a copy
4139 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4141 bool
4142 vn_nary_may_trap (vn_nary_op_t nary)
4144 tree type;
4145 tree rhs2 = NULL_TREE;
4146 bool honor_nans = false;
4147 bool honor_snans = false;
4148 bool fp_operation = false;
4149 bool honor_trapv = false;
4150 bool handled, ret;
4151 unsigned i;
4153 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4154 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4155 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4157 type = nary->type;
4158 fp_operation = FLOAT_TYPE_P (type);
4159 if (fp_operation)
4161 honor_nans = flag_trapping_math && !flag_finite_math_only;
4162 honor_snans = flag_signaling_nans != 0;
4164 else if (INTEGRAL_TYPE_P (type)
4165 && TYPE_OVERFLOW_TRAPS (type))
4166 honor_trapv = true;
4168 if (nary->length >= 2)
4169 rhs2 = nary->op[1];
4170 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4171 honor_trapv,
4172 honor_nans, honor_snans, rhs2,
4173 &handled);
4174 if (handled
4175 && ret)
4176 return true;
4178 for (i = 0; i < nary->length; ++i)
4179 if (tree_could_trap_p (nary->op[i]))
4180 return true;
4182 return false;