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