1 /* SCC value numbering for trees
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
24 #include "splay-tree.h"
31 #include "insn-config.h"
35 #include "gimple-pretty-print.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
40 #include "tree-inline.h"
41 #include "internal-fn.h"
42 #include "gimple-iterator.h"
43 #include "gimple-fold.h"
57 #include "tree-ssa-propagate.h"
60 #include "gimple-match.h"
61 #include "stringpool.h"
63 #include "tree-pass.h"
64 #include "statistics.h"
65 #include "langhooks.h"
66 #include "ipa-utils.h"
68 #include "tree-cfgcleanup.h"
69 #include "tree-ssa-loop.h"
70 #include "tree-scalar-evolution.h"
71 #include "tree-ssa-loop-niter.h"
73 #include "fold-const-call.h"
74 #include "ipa-modref-tree.h"
75 #include "ipa-modref.h"
76 #include "tree-ssa-sccvn.h"
78 /* This algorithm is based on the SCC algorithm presented by Keith
79 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
80 (http://citeseer.ist.psu.edu/41805.html). In
81 straight line code, it is equivalent to a regular hash based value
82 numbering that is performed in reverse postorder.
84 For code with cycles, there are two alternatives, both of which
85 require keeping the hashtables separate from the actual list of
86 value numbers for SSA names.
88 1. Iterate value numbering in an RPO walk of the blocks, removing
89 all the entries from the hashtable after each iteration (but
90 keeping the SSA name->value number mapping between iterations).
91 Iterate until it does not change.
93 2. Perform value numbering as part of an SCC walk on the SSA graph,
94 iterating only the cycles in the SSA graph until they do not change
95 (using a separate, optimistic hashtable for value numbering the SCC
98 The second is not just faster in practice (because most SSA graph
99 cycles do not involve all the variables in the graph), it also has
100 some nice properties.
102 One of these nice properties is that when we pop an SCC off the
103 stack, we are guaranteed to have processed all the operands coming from
104 *outside of that SCC*, so we do not need to do anything special to
105 ensure they have value numbers.
107 Another nice property is that the SCC walk is done as part of a DFS
108 of the SSA graph, which makes it easy to perform combining and
109 simplifying operations at the same time.
111 The code below is deliberately written in a way that makes it easy
112 to separate the SCC walk from the other work it does.
114 In order to propagate constants through the code, we track which
115 expressions contain constants, and use those while folding. In
116 theory, we could also track expressions whose value numbers are
117 replaced, in case we end up folding based on expression
120 In order to value number memory, we assign value numbers to vuses.
121 This enables us to note that, for example, stores to the same
122 address of the same value from the same starting memory states are
126 1. We can iterate only the changing portions of the SCC's, but
127 I have not seen an SCC big enough for this to be a win.
128 2. If you differentiate between phi nodes for loops and phi nodes
129 for if-then-else, you can properly consider phi nodes in different
130 blocks for equivalence.
131 3. We could value number vuses in more cases, particularly, whole
135 /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
136 #define BB_EXECUTABLE BB_VISITED
138 static vn_lookup_kind default_vn_walk_kind
;
140 /* vn_nary_op hashtable helpers. */
142 struct vn_nary_op_hasher
: nofree_ptr_hash
<vn_nary_op_s
>
144 typedef vn_nary_op_s
*compare_type
;
145 static inline hashval_t
hash (const vn_nary_op_s
*);
146 static inline bool equal (const vn_nary_op_s
*, const vn_nary_op_s
*);
149 /* Return the computed hashcode for nary operation P1. */
152 vn_nary_op_hasher::hash (const vn_nary_op_s
*vno1
)
154 return vno1
->hashcode
;
157 /* Compare nary operations P1 and P2 and return true if they are
161 vn_nary_op_hasher::equal (const vn_nary_op_s
*vno1
, const vn_nary_op_s
*vno2
)
163 return vno1
== vno2
|| vn_nary_op_eq (vno1
, vno2
);
166 typedef hash_table
<vn_nary_op_hasher
> vn_nary_op_table_type
;
167 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type
;
170 /* vn_phi hashtable helpers. */
173 vn_phi_eq (const_vn_phi_t
const vp1
, const_vn_phi_t
const vp2
);
175 struct vn_phi_hasher
: nofree_ptr_hash
<vn_phi_s
>
177 static inline hashval_t
hash (const vn_phi_s
*);
178 static inline bool equal (const vn_phi_s
*, const vn_phi_s
*);
181 /* Return the computed hashcode for phi operation P1. */
184 vn_phi_hasher::hash (const vn_phi_s
*vp1
)
186 return vp1
->hashcode
;
189 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
192 vn_phi_hasher::equal (const vn_phi_s
*vp1
, const vn_phi_s
*vp2
)
194 return vp1
== vp2
|| vn_phi_eq (vp1
, vp2
);
197 typedef hash_table
<vn_phi_hasher
> vn_phi_table_type
;
198 typedef vn_phi_table_type::iterator vn_phi_iterator_type
;
201 /* Compare two reference operands P1 and P2 for equality. Return true if
202 they are equal, and false otherwise. */
205 vn_reference_op_eq (const void *p1
, const void *p2
)
207 const_vn_reference_op_t
const vro1
= (const_vn_reference_op_t
) p1
;
208 const_vn_reference_op_t
const vro2
= (const_vn_reference_op_t
) p2
;
210 return (vro1
->opcode
== vro2
->opcode
211 /* We do not care for differences in type qualification. */
212 && (vro1
->type
== vro2
->type
213 || (vro1
->type
&& vro2
->type
214 && types_compatible_p (TYPE_MAIN_VARIANT (vro1
->type
),
215 TYPE_MAIN_VARIANT (vro2
->type
))))
216 && expressions_equal_p (vro1
->op0
, vro2
->op0
)
217 && expressions_equal_p (vro1
->op1
, vro2
->op1
)
218 && expressions_equal_p (vro1
->op2
, vro2
->op2
)
219 && (vro1
->opcode
!= CALL_EXPR
|| vro1
->clique
== vro2
->clique
));
222 /* Free a reference operation structure VP. */
225 free_reference (vn_reference_s
*vr
)
227 vr
->operands
.release ();
231 /* vn_reference hashtable helpers. */
233 struct vn_reference_hasher
: nofree_ptr_hash
<vn_reference_s
>
235 static inline hashval_t
hash (const vn_reference_s
*);
236 static inline bool equal (const vn_reference_s
*, const vn_reference_s
*);
239 /* Return the hashcode for a given reference operation P1. */
242 vn_reference_hasher::hash (const vn_reference_s
*vr1
)
244 return vr1
->hashcode
;
248 vn_reference_hasher::equal (const vn_reference_s
*v
, const vn_reference_s
*c
)
250 return v
== c
|| vn_reference_eq (v
, c
);
253 typedef hash_table
<vn_reference_hasher
> vn_reference_table_type
;
254 typedef vn_reference_table_type::iterator vn_reference_iterator_type
;
256 /* Pretty-print OPS to OUTFILE. */
259 print_vn_reference_ops (FILE *outfile
, const vec
<vn_reference_op_s
> ops
)
261 vn_reference_op_t vro
;
263 fprintf (outfile
, "{");
264 for (i
= 0; ops
.iterate (i
, &vro
); i
++)
266 bool closebrace
= false;
267 if (vro
->opcode
!= SSA_NAME
268 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
270 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
271 if (vro
->op0
|| vro
->opcode
== CALL_EXPR
)
273 fprintf (outfile
, "<");
277 if (vro
->op0
|| vro
->opcode
== CALL_EXPR
)
280 fprintf (outfile
, internal_fn_name ((internal_fn
)vro
->clique
));
282 print_generic_expr (outfile
, vro
->op0
);
285 fprintf (outfile
, ",");
286 print_generic_expr (outfile
, vro
->op1
);
290 fprintf (outfile
, ",");
291 print_generic_expr (outfile
, vro
->op2
);
295 fprintf (outfile
, ">");
296 if (i
!= ops
.length () - 1)
297 fprintf (outfile
, ",");
299 fprintf (outfile
, "}");
303 debug_vn_reference_ops (const vec
<vn_reference_op_s
> ops
)
305 print_vn_reference_ops (stderr
, ops
);
306 fputc ('\n', stderr
);
309 /* The set of VN hashtables. */
311 typedef struct vn_tables_s
313 vn_nary_op_table_type
*nary
;
314 vn_phi_table_type
*phis
;
315 vn_reference_table_type
*references
;
319 /* vn_constant hashtable helpers. */
321 struct vn_constant_hasher
: free_ptr_hash
<vn_constant_s
>
323 static inline hashval_t
hash (const vn_constant_s
*);
324 static inline bool equal (const vn_constant_s
*, const vn_constant_s
*);
327 /* Hash table hash function for vn_constant_t. */
330 vn_constant_hasher::hash (const vn_constant_s
*vc1
)
332 return vc1
->hashcode
;
335 /* Hash table equality function for vn_constant_t. */
338 vn_constant_hasher::equal (const vn_constant_s
*vc1
, const vn_constant_s
*vc2
)
340 if (vc1
->hashcode
!= vc2
->hashcode
)
343 return vn_constant_eq_with_type (vc1
->constant
, vc2
->constant
);
346 static hash_table
<vn_constant_hasher
> *constant_to_value_id
;
349 /* Obstack we allocate the vn-tables elements from. */
350 static obstack vn_tables_obstack
;
351 /* Special obstack we never unwind. */
352 static obstack vn_tables_insert_obstack
;
354 static vn_reference_t last_inserted_ref
;
355 static vn_phi_t last_inserted_phi
;
356 static vn_nary_op_t last_inserted_nary
;
357 static vn_ssa_aux_t last_pushed_avail
;
359 /* Valid hashtables storing information we have proven to be
361 static vn_tables_t valid_info
;
364 /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
365 an SSA name, otherwise just return it. */
366 tree (*vn_valueize
) (tree
);
368 vn_valueize_for_srt (tree t
, void* context ATTRIBUTE_UNUSED
)
370 basic_block saved_vn_context_bb
= vn_context_bb
;
371 /* Look for sth available at the definition block of the argument.
372 This avoids inconsistencies between availability there which
373 decides if the stmt can be removed and availability at the
374 use site. The SSA property ensures that things available
375 at the definition are also available at uses. */
376 if (!SSA_NAME_IS_DEFAULT_DEF (t
))
377 vn_context_bb
= gimple_bb (SSA_NAME_DEF_STMT (t
));
378 tree res
= vn_valueize (t
);
379 vn_context_bb
= saved_vn_context_bb
;
384 /* This represents the top of the VN lattice, which is the universal
389 /* Unique counter for our value ids. */
391 static unsigned int next_value_id
;
392 static int next_constant_value_id
;
395 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
396 are allocated on an obstack for locality reasons, and to free them
397 without looping over the vec. */
399 struct vn_ssa_aux_hasher
: typed_noop_remove
<vn_ssa_aux_t
>
401 typedef vn_ssa_aux_t value_type
;
402 typedef tree compare_type
;
403 static inline hashval_t
hash (const value_type
&);
404 static inline bool equal (const value_type
&, const compare_type
&);
405 static inline void mark_deleted (value_type
&) {}
406 static const bool empty_zero_p
= true;
407 static inline void mark_empty (value_type
&e
) { e
= NULL
; }
408 static inline bool is_deleted (value_type
&) { return false; }
409 static inline bool is_empty (value_type
&e
) { return e
== NULL
; }
413 vn_ssa_aux_hasher::hash (const value_type
&entry
)
415 return SSA_NAME_VERSION (entry
->name
);
419 vn_ssa_aux_hasher::equal (const value_type
&entry
, const compare_type
&name
)
421 return name
== entry
->name
;
424 static hash_table
<vn_ssa_aux_hasher
> *vn_ssa_aux_hash
;
425 typedef hash_table
<vn_ssa_aux_hasher
>::iterator vn_ssa_aux_iterator_type
;
426 static struct obstack vn_ssa_aux_obstack
;
428 static vn_nary_op_t
vn_nary_op_insert_stmt (gimple
*, tree
);
429 static vn_nary_op_t
vn_nary_op_insert_into (vn_nary_op_t
,
430 vn_nary_op_table_type
*);
431 static void init_vn_nary_op_from_pieces (vn_nary_op_t
, unsigned int,
432 enum tree_code
, tree
, tree
*);
433 static tree
vn_lookup_simplify_result (gimple_match_op
*);
434 static vn_reference_t vn_reference_lookup_or_insert_for_pieces
435 (tree
, alias_set_type
, alias_set_type
, tree
,
436 vec
<vn_reference_op_s
, va_heap
>, tree
);
438 /* Return whether there is value numbering information for a given SSA name. */
441 has_VN_INFO (tree name
)
443 return vn_ssa_aux_hash
->find_with_hash (name
, SSA_NAME_VERSION (name
));
450 = vn_ssa_aux_hash
->find_slot_with_hash (name
, SSA_NAME_VERSION (name
),
455 vn_ssa_aux_t newinfo
= *res
= XOBNEW (&vn_ssa_aux_obstack
, struct vn_ssa_aux
);
456 memset (newinfo
, 0, sizeof (struct vn_ssa_aux
));
457 newinfo
->name
= name
;
458 newinfo
->valnum
= VN_TOP
;
459 /* We are using the visited flag to handle uses with defs not within the
460 region being value-numbered. */
461 newinfo
->visited
= false;
463 /* Given we create the VN_INFOs on-demand now we have to do initialization
464 different than VN_TOP here. */
465 if (SSA_NAME_IS_DEFAULT_DEF (name
))
466 switch (TREE_CODE (SSA_NAME_VAR (name
)))
469 /* All undefined vars are VARYING. */
470 newinfo
->valnum
= name
;
471 newinfo
->visited
= true;
475 /* Parameters are VARYING but we can record a condition
476 if we know it is a non-NULL pointer. */
477 newinfo
->visited
= true;
478 newinfo
->valnum
= name
;
479 if (POINTER_TYPE_P (TREE_TYPE (name
))
480 && nonnull_arg_p (SSA_NAME_VAR (name
)))
484 ops
[1] = build_int_cst (TREE_TYPE (name
), 0);
486 /* Allocate from non-unwinding stack. */
487 nary
= alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack
);
488 init_vn_nary_op_from_pieces (nary
, 2, NE_EXPR
,
489 boolean_type_node
, ops
);
490 nary
->predicated_values
= 0;
491 nary
->u
.result
= boolean_true_node
;
492 vn_nary_op_insert_into (nary
, valid_info
->nary
);
493 gcc_assert (nary
->unwind_to
== NULL
);
494 /* Also do not link it into the undo chain. */
495 last_inserted_nary
= nary
->next
;
496 nary
->next
= (vn_nary_op_t
)(void *)-1;
497 nary
= alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack
);
498 init_vn_nary_op_from_pieces (nary
, 2, EQ_EXPR
,
499 boolean_type_node
, ops
);
500 nary
->predicated_values
= 0;
501 nary
->u
.result
= boolean_false_node
;
502 vn_nary_op_insert_into (nary
, valid_info
->nary
);
503 gcc_assert (nary
->unwind_to
== NULL
);
504 last_inserted_nary
= nary
->next
;
505 nary
->next
= (vn_nary_op_t
)(void *)-1;
506 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
508 fprintf (dump_file
, "Recording ");
509 print_generic_expr (dump_file
, name
, TDF_SLIM
);
510 fprintf (dump_file
, " != 0\n");
516 /* If the result is passed by invisible reference the default
517 def is initialized, otherwise it's uninitialized. Still
518 undefined is varying. */
519 newinfo
->visited
= true;
520 newinfo
->valnum
= name
;
529 /* Return the SSA value of X. */
532 SSA_VAL (tree x
, bool *visited
= NULL
)
534 vn_ssa_aux_t tem
= vn_ssa_aux_hash
->find_with_hash (x
, SSA_NAME_VERSION (x
));
536 *visited
= tem
&& tem
->visited
;
537 return tem
&& tem
->visited
? tem
->valnum
: x
;
540 /* Return the SSA value of the VUSE x, supporting released VDEFs
541 during elimination which will value-number the VDEF to the
542 associated VUSE (but not substitute in the whole lattice). */
545 vuse_ssa_val (tree x
)
553 gcc_assert (x
!= VN_TOP
);
555 while (SSA_NAME_IN_FREE_LIST (x
));
560 /* Similar to the above but used as callback for walk_non_aliased_vuses
561 and thus should stop at unvisited VUSE to not walk across region
565 vuse_valueize (tree vuse
)
570 vuse
= SSA_VAL (vuse
, &visited
);
573 gcc_assert (vuse
!= VN_TOP
);
575 while (SSA_NAME_IN_FREE_LIST (vuse
));
580 /* Return the vn_kind the expression computed by the stmt should be
584 vn_get_stmt_kind (gimple
*stmt
)
586 switch (gimple_code (stmt
))
594 enum tree_code code
= gimple_assign_rhs_code (stmt
);
595 tree rhs1
= gimple_assign_rhs1 (stmt
);
596 switch (get_gimple_rhs_class (code
))
598 case GIMPLE_UNARY_RHS
:
599 case GIMPLE_BINARY_RHS
:
600 case GIMPLE_TERNARY_RHS
:
602 case GIMPLE_SINGLE_RHS
:
603 switch (TREE_CODE_CLASS (code
))
606 /* VOP-less references can go through unary case. */
607 if ((code
== REALPART_EXPR
608 || code
== IMAGPART_EXPR
609 || code
== VIEW_CONVERT_EXPR
610 || code
== BIT_FIELD_REF
)
611 && (TREE_CODE (TREE_OPERAND (rhs1
, 0)) == SSA_NAME
612 || is_gimple_min_invariant (TREE_OPERAND (rhs1
, 0))))
616 case tcc_declaration
:
623 if (code
== ADDR_EXPR
)
624 return (is_gimple_min_invariant (rhs1
)
625 ? VN_CONSTANT
: VN_REFERENCE
);
626 else if (code
== CONSTRUCTOR
)
639 /* Lookup a value id for CONSTANT and return it. If it does not
643 get_constant_value_id (tree constant
)
645 vn_constant_s
**slot
;
646 struct vn_constant_s vc
;
648 vc
.hashcode
= vn_hash_constant_with_type (constant
);
649 vc
.constant
= constant
;
650 slot
= constant_to_value_id
->find_slot (&vc
, NO_INSERT
);
652 return (*slot
)->value_id
;
656 /* Lookup a value id for CONSTANT, and if it does not exist, create a
657 new one and return it. If it does exist, return it. */
660 get_or_alloc_constant_value_id (tree constant
)
662 vn_constant_s
**slot
;
663 struct vn_constant_s vc
;
666 /* If the hashtable isn't initialized we're not running from PRE and thus
667 do not need value-ids. */
668 if (!constant_to_value_id
)
671 vc
.hashcode
= vn_hash_constant_with_type (constant
);
672 vc
.constant
= constant
;
673 slot
= constant_to_value_id
->find_slot (&vc
, INSERT
);
675 return (*slot
)->value_id
;
677 vcp
= XNEW (struct vn_constant_s
);
678 vcp
->hashcode
= vc
.hashcode
;
679 vcp
->constant
= constant
;
680 vcp
->value_id
= get_next_constant_value_id ();
682 return vcp
->value_id
;
685 /* Compute the hash for a reference operand VRO1. */
688 vn_reference_op_compute_hash (const vn_reference_op_t vro1
, inchash::hash
&hstate
)
690 hstate
.add_int (vro1
->opcode
);
691 if (vro1
->opcode
== CALL_EXPR
&& !vro1
->op0
)
692 hstate
.add_int (vro1
->clique
);
694 inchash::add_expr (vro1
->op0
, hstate
);
696 inchash::add_expr (vro1
->op1
, hstate
);
698 inchash::add_expr (vro1
->op2
, hstate
);
701 /* Compute a hash for the reference operation VR1 and return it. */
704 vn_reference_compute_hash (const vn_reference_t vr1
)
706 inchash::hash hstate
;
709 vn_reference_op_t vro
;
713 FOR_EACH_VEC_ELT (vr1
->operands
, i
, vro
)
715 if (vro
->opcode
== MEM_REF
)
717 else if (vro
->opcode
!= ADDR_EXPR
)
719 if (maybe_ne (vro
->off
, -1))
721 if (known_eq (off
, -1))
727 if (maybe_ne (off
, -1)
728 && maybe_ne (off
, 0))
729 hstate
.add_poly_int (off
);
732 && vro
->opcode
== ADDR_EXPR
)
736 tree op
= TREE_OPERAND (vro
->op0
, 0);
737 hstate
.add_int (TREE_CODE (op
));
738 inchash::add_expr (op
, hstate
);
742 vn_reference_op_compute_hash (vro
, hstate
);
745 result
= hstate
.end ();
746 /* ??? We would ICE later if we hash instead of adding that in. */
748 result
+= SSA_NAME_VERSION (vr1
->vuse
);
753 /* Return true if reference operations VR1 and VR2 are equivalent. This
754 means they have the same set of operands and vuses. */
757 vn_reference_eq (const_vn_reference_t
const vr1
, const_vn_reference_t
const vr2
)
761 /* Early out if this is not a hash collision. */
762 if (vr1
->hashcode
!= vr2
->hashcode
)
765 /* The VOP needs to be the same. */
766 if (vr1
->vuse
!= vr2
->vuse
)
769 /* If the operands are the same we are done. */
770 if (vr1
->operands
== vr2
->operands
)
773 if (!vr1
->type
|| !vr2
->type
)
775 if (vr1
->type
!= vr2
->type
)
778 else if (vr1
->type
== vr2
->type
)
780 else if (COMPLETE_TYPE_P (vr1
->type
) != COMPLETE_TYPE_P (vr2
->type
)
781 || (COMPLETE_TYPE_P (vr1
->type
)
782 && !expressions_equal_p (TYPE_SIZE (vr1
->type
),
783 TYPE_SIZE (vr2
->type
))))
785 else if (vr1
->operands
[0].opcode
== CALL_EXPR
786 && !types_compatible_p (vr1
->type
, vr2
->type
))
788 else if (INTEGRAL_TYPE_P (vr1
->type
)
789 && INTEGRAL_TYPE_P (vr2
->type
))
791 if (TYPE_PRECISION (vr1
->type
) != TYPE_PRECISION (vr2
->type
))
794 else if (INTEGRAL_TYPE_P (vr1
->type
)
795 && (TYPE_PRECISION (vr1
->type
)
796 != TREE_INT_CST_LOW (TYPE_SIZE (vr1
->type
))))
798 else if (INTEGRAL_TYPE_P (vr2
->type
)
799 && (TYPE_PRECISION (vr2
->type
)
800 != TREE_INT_CST_LOW (TYPE_SIZE (vr2
->type
))))
802 else if (VECTOR_BOOLEAN_TYPE_P (vr1
->type
)
803 && VECTOR_BOOLEAN_TYPE_P (vr2
->type
))
805 /* Vector boolean types can have padding, verify we are dealing with
806 the same number of elements, aka the precision of the types.
807 For example, In most architecture the precision_size of vbool*_t
808 types are caculated like below:
809 precision_size = type_size * 8
811 Unfortunately, the RISC-V will adjust the precision_size for the
812 vbool*_t in order to align the ISA as below:
813 type_size = [1, 1, 1, 1, 2, 4, 8]
814 precision_size = [1, 2, 4, 8, 16, 32, 64]
816 Then the precision_size of RISC-V vbool*_t will not be the multiple
817 of the type_size. We take care of this case consolidated here. */
818 if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1
->type
),
819 TYPE_VECTOR_SUBPARTS (vr2
->type
)))
827 poly_int64 off1
= 0, off2
= 0;
828 vn_reference_op_t vro1
, vro2
;
829 vn_reference_op_s tem1
, tem2
;
830 bool deref1
= false, deref2
= false;
831 bool reverse1
= false, reverse2
= false;
832 for (; vr1
->operands
.iterate (i
, &vro1
); i
++)
834 if (vro1
->opcode
== MEM_REF
)
836 /* Do not look through a storage order barrier. */
837 else if (vro1
->opcode
== VIEW_CONVERT_EXPR
&& vro1
->reverse
)
839 reverse1
|= vro1
->reverse
;
840 if (known_eq (vro1
->off
, -1))
844 for (; vr2
->operands
.iterate (j
, &vro2
); j
++)
846 if (vro2
->opcode
== MEM_REF
)
848 /* Do not look through a storage order barrier. */
849 else if (vro2
->opcode
== VIEW_CONVERT_EXPR
&& vro2
->reverse
)
851 reverse2
|= vro2
->reverse
;
852 if (known_eq (vro2
->off
, -1))
856 if (maybe_ne (off1
, off2
) || reverse1
!= reverse2
)
858 if (deref1
&& vro1
->opcode
== ADDR_EXPR
)
860 memset (&tem1
, 0, sizeof (tem1
));
861 tem1
.op0
= TREE_OPERAND (vro1
->op0
, 0);
862 tem1
.type
= TREE_TYPE (tem1
.op0
);
863 tem1
.opcode
= TREE_CODE (tem1
.op0
);
867 if (deref2
&& vro2
->opcode
== ADDR_EXPR
)
869 memset (&tem2
, 0, sizeof (tem2
));
870 tem2
.op0
= TREE_OPERAND (vro2
->op0
, 0);
871 tem2
.type
= TREE_TYPE (tem2
.op0
);
872 tem2
.opcode
= TREE_CODE (tem2
.op0
);
876 if (deref1
!= deref2
)
878 if (!vn_reference_op_eq (vro1
, vro2
))
883 while (vr1
->operands
.length () != i
884 || vr2
->operands
.length () != j
);
889 /* Copy the operations present in load/store REF into RESULT, a vector of
890 vn_reference_op_s's. */
893 copy_reference_ops_from_ref (tree ref
, vec
<vn_reference_op_s
> *result
)
895 /* For non-calls, store the information that makes up the address. */
899 vn_reference_op_s temp
;
901 memset (&temp
, 0, sizeof (temp
));
902 temp
.type
= TREE_TYPE (ref
);
903 temp
.opcode
= TREE_CODE (ref
);
909 temp
.op0
= TREE_OPERAND (ref
, 1);
912 temp
.op0
= TREE_OPERAND (ref
, 1);
916 /* The base address gets its own vn_reference_op_s structure. */
917 temp
.op0
= TREE_OPERAND (ref
, 1);
918 if (!mem_ref_offset (ref
).to_shwi (&temp
.off
))
920 temp
.clique
= MR_DEPENDENCE_CLIQUE (ref
);
921 temp
.base
= MR_DEPENDENCE_BASE (ref
);
922 temp
.reverse
= REF_REVERSE_STORAGE_ORDER (ref
);
925 /* The base address gets its own vn_reference_op_s structure. */
926 temp
.op0
= TMR_INDEX (ref
);
927 temp
.op1
= TMR_STEP (ref
);
928 temp
.op2
= TMR_OFFSET (ref
);
929 temp
.clique
= MR_DEPENDENCE_CLIQUE (ref
);
930 temp
.base
= MR_DEPENDENCE_BASE (ref
);
931 result
->safe_push (temp
);
932 memset (&temp
, 0, sizeof (temp
));
933 temp
.type
= NULL_TREE
;
934 temp
.opcode
= ERROR_MARK
;
935 temp
.op0
= TMR_INDEX2 (ref
);
939 /* Record bits, position and storage order. */
940 temp
.op0
= TREE_OPERAND (ref
, 1);
941 temp
.op1
= TREE_OPERAND (ref
, 2);
942 if (!multiple_p (bit_field_offset (ref
), BITS_PER_UNIT
, &temp
.off
))
944 temp
.reverse
= REF_REVERSE_STORAGE_ORDER (ref
);
947 /* The field decl is enough to unambiguously specify the field,
948 so use its type here. */
949 temp
.type
= TREE_TYPE (TREE_OPERAND (ref
, 1));
950 temp
.op0
= TREE_OPERAND (ref
, 1);
951 temp
.op1
= TREE_OPERAND (ref
, 2);
952 temp
.reverse
= (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref
, 0)))
953 && TYPE_REVERSE_STORAGE_ORDER
954 (TREE_TYPE (TREE_OPERAND (ref
, 0))));
956 tree this_offset
= component_ref_field_offset (ref
);
958 && poly_int_tree_p (this_offset
))
960 tree bit_offset
= DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref
, 1));
961 if (TREE_INT_CST_LOW (bit_offset
) % BITS_PER_UNIT
== 0)
964 = (wi::to_poly_offset (this_offset
)
965 + (wi::to_offset (bit_offset
) >> LOG2_BITS_PER_UNIT
));
966 /* Probibit value-numbering zero offset components
967 of addresses the same before the pass folding
968 __builtin_object_size had a chance to run. */
969 if (TREE_CODE (orig
) != ADDR_EXPR
971 || (cfun
->curr_properties
& PROP_objsz
))
972 off
.to_shwi (&temp
.off
);
977 case ARRAY_RANGE_REF
:
980 tree eltype
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref
, 0)));
981 /* Record index as operand. */
982 temp
.op0
= TREE_OPERAND (ref
, 1);
983 /* Always record lower bounds and element size. */
984 temp
.op1
= array_ref_low_bound (ref
);
985 /* But record element size in units of the type alignment. */
986 temp
.op2
= TREE_OPERAND (ref
, 3);
987 temp
.align
= eltype
->type_common
.align
;
989 temp
.op2
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (eltype
),
990 size_int (TYPE_ALIGN_UNIT (eltype
)));
991 if (poly_int_tree_p (temp
.op0
)
992 && poly_int_tree_p (temp
.op1
)
993 && TREE_CODE (temp
.op2
) == INTEGER_CST
)
995 poly_offset_int off
= ((wi::to_poly_offset (temp
.op0
)
996 - wi::to_poly_offset (temp
.op1
))
997 * wi::to_offset (temp
.op2
)
998 * vn_ref_op_align_unit (&temp
));
999 off
.to_shwi (&temp
.off
);
1001 temp
.reverse
= (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref
, 0)))
1002 && TYPE_REVERSE_STORAGE_ORDER
1003 (TREE_TYPE (TREE_OPERAND (ref
, 0))));
1007 if (DECL_HARD_REGISTER (ref
))
1016 /* Canonicalize decls to MEM[&decl] which is what we end up with
1017 when valueizing MEM[ptr] with ptr = &decl. */
1018 temp
.opcode
= MEM_REF
;
1019 temp
.op0
= build_int_cst (build_pointer_type (TREE_TYPE (ref
)), 0);
1021 result
->safe_push (temp
);
1022 temp
.opcode
= ADDR_EXPR
;
1023 temp
.op0
= build1 (ADDR_EXPR
, TREE_TYPE (temp
.op0
), ref
);
1024 temp
.type
= TREE_TYPE (temp
.op0
);
1039 if (is_gimple_min_invariant (ref
))
1045 /* These are only interesting for their operands, their
1046 existence, and their type. They will never be the last
1047 ref in the chain of references (IE they require an
1048 operand), so we don't have to put anything
1049 for op* as it will be handled by the iteration */
1053 case VIEW_CONVERT_EXPR
:
1055 temp
.reverse
= storage_order_barrier_p (ref
);
1058 /* This is only interesting for its constant offset. */
1059 temp
.off
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref
)));
1064 result
->safe_push (temp
);
1066 if (REFERENCE_CLASS_P (ref
)
1067 || TREE_CODE (ref
) == MODIFY_EXPR
1068 || TREE_CODE (ref
) == WITH_SIZE_EXPR
1069 || (TREE_CODE (ref
) == ADDR_EXPR
1070 && !is_gimple_min_invariant (ref
)))
1071 ref
= TREE_OPERAND (ref
, 0);
1077 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1078 operands in *OPS, the reference alias set SET and the reference type TYPE.
1079 Return true if something useful was produced. */
1082 ao_ref_init_from_vn_reference (ao_ref
*ref
,
1083 alias_set_type set
, alias_set_type base_set
,
1084 tree type
, const vec
<vn_reference_op_s
> &ops
)
1087 tree base
= NULL_TREE
;
1088 tree
*op0_p
= &base
;
1089 poly_offset_int offset
= 0;
1090 poly_offset_int max_size
;
1091 poly_offset_int size
= -1;
1092 tree size_tree
= NULL_TREE
;
1094 /* We don't handle calls. */
1098 machine_mode mode
= TYPE_MODE (type
);
1099 if (mode
== BLKmode
)
1100 size_tree
= TYPE_SIZE (type
);
1102 size
= GET_MODE_BITSIZE (mode
);
1103 if (size_tree
!= NULL_TREE
1104 && poly_int_tree_p (size_tree
))
1105 size
= wi::to_poly_offset (size_tree
);
1107 /* Lower the final access size from the outermost expression. */
1108 const_vn_reference_op_t cst_op
= &ops
[0];
1109 /* Cast away constness for the sake of the const-unsafe
1110 FOR_EACH_VEC_ELT(). */
1111 vn_reference_op_t op
= const_cast<vn_reference_op_t
>(cst_op
);
1112 size_tree
= NULL_TREE
;
1113 if (op
->opcode
== COMPONENT_REF
)
1114 size_tree
= DECL_SIZE (op
->op0
);
1115 else if (op
->opcode
== BIT_FIELD_REF
)
1116 size_tree
= op
->op0
;
1117 if (size_tree
!= NULL_TREE
1118 && poly_int_tree_p (size_tree
)
1119 && (!known_size_p (size
)
1120 || known_lt (wi::to_poly_offset (size_tree
), size
)))
1121 size
= wi::to_poly_offset (size_tree
);
1123 /* Initially, maxsize is the same as the accessed element size.
1124 In the following it will only grow (or become -1). */
1127 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1128 and find the ultimate containing object. */
1129 FOR_EACH_VEC_ELT (ops
, i
, op
)
1133 /* These may be in the reference ops, but we cannot do anything
1134 sensible with them here. */
1136 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1137 if (base
!= NULL_TREE
1138 && TREE_CODE (base
) == MEM_REF
1140 && DECL_P (TREE_OPERAND (op
->op0
, 0)))
1142 const_vn_reference_op_t pop
= &ops
[i
-1];
1143 base
= TREE_OPERAND (op
->op0
, 0);
1144 if (known_eq (pop
->off
, -1))
1150 offset
+= pop
->off
* BITS_PER_UNIT
;
1158 /* Record the base objects. */
1160 *op0_p
= build2 (MEM_REF
, op
->type
,
1161 NULL_TREE
, op
->op0
);
1162 MR_DEPENDENCE_CLIQUE (*op0_p
) = op
->clique
;
1163 MR_DEPENDENCE_BASE (*op0_p
) = op
->base
;
1164 op0_p
= &TREE_OPERAND (*op0_p
, 0);
1175 /* And now the usual component-reference style ops. */
1177 offset
+= wi::to_poly_offset (op
->op1
);
1182 tree field
= op
->op0
;
1183 /* We do not have a complete COMPONENT_REF tree here so we
1184 cannot use component_ref_field_offset. Do the interesting
1186 tree this_offset
= DECL_FIELD_OFFSET (field
);
1188 if (op
->op1
|| !poly_int_tree_p (this_offset
))
1192 poly_offset_int woffset
= (wi::to_poly_offset (this_offset
)
1193 << LOG2_BITS_PER_UNIT
);
1194 woffset
+= wi::to_offset (DECL_FIELD_BIT_OFFSET (field
));
1200 case ARRAY_RANGE_REF
:
1202 /* We recorded the lower bound and the element size. */
1203 if (!poly_int_tree_p (op
->op0
)
1204 || !poly_int_tree_p (op
->op1
)
1205 || TREE_CODE (op
->op2
) != INTEGER_CST
)
1209 poly_offset_int woffset
1210 = wi::sext (wi::to_poly_offset (op
->op0
)
1211 - wi::to_poly_offset (op
->op1
),
1212 TYPE_PRECISION (sizetype
));
1213 woffset
*= wi::to_offset (op
->op2
) * vn_ref_op_align_unit (op
);
1214 woffset
<<= LOG2_BITS_PER_UNIT
;
1226 case VIEW_CONVERT_EXPR
:
1243 if (base
== NULL_TREE
)
1246 ref
->ref
= NULL_TREE
;
1248 ref
->ref_alias_set
= set
;
1249 ref
->base_alias_set
= base_set
;
1250 /* We discount volatiles from value-numbering elsewhere. */
1251 ref
->volatile_p
= false;
1253 if (!size
.to_shwi (&ref
->size
) || maybe_lt (ref
->size
, 0))
1261 if (!offset
.to_shwi (&ref
->offset
))
1268 if (!max_size
.to_shwi (&ref
->max_size
) || maybe_lt (ref
->max_size
, 0))
1274 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1275 vn_reference_op_s's. */
1278 copy_reference_ops_from_call (gcall
*call
,
1279 vec
<vn_reference_op_s
> *result
)
1281 vn_reference_op_s temp
;
1283 tree lhs
= gimple_call_lhs (call
);
1286 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1287 different. By adding the lhs here in the vector, we ensure that the
1288 hashcode is different, guaranteeing a different value number. */
1289 if (lhs
&& TREE_CODE (lhs
) != SSA_NAME
)
1291 memset (&temp
, 0, sizeof (temp
));
1292 temp
.opcode
= MODIFY_EXPR
;
1293 temp
.type
= TREE_TYPE (lhs
);
1296 result
->safe_push (temp
);
1299 /* Copy the type, opcode, function, static chain and EH region, if any. */
1300 memset (&temp
, 0, sizeof (temp
));
1301 temp
.type
= gimple_call_fntype (call
);
1302 temp
.opcode
= CALL_EXPR
;
1303 temp
.op0
= gimple_call_fn (call
);
1304 if (gimple_call_internal_p (call
))
1305 temp
.clique
= gimple_call_internal_fn (call
);
1306 temp
.op1
= gimple_call_chain (call
);
1307 if (stmt_could_throw_p (cfun
, call
) && (lr
= lookup_stmt_eh_lp (call
)) > 0)
1308 temp
.op2
= size_int (lr
);
1310 result
->safe_push (temp
);
1312 /* Copy the call arguments. As they can be references as well,
1313 just chain them together. */
1314 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
1316 tree callarg
= gimple_call_arg (call
, i
);
1317 copy_reference_ops_from_ref (callarg
, result
);
1321 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1322 *I_P to point to the last element of the replacement. */
1324 vn_reference_fold_indirect (vec
<vn_reference_op_s
> *ops
,
1327 unsigned int i
= *i_p
;
1328 vn_reference_op_t op
= &(*ops
)[i
];
1329 vn_reference_op_t mem_op
= &(*ops
)[i
- 1];
1331 poly_int64 addr_offset
= 0;
1333 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1334 from .foo.bar to the preceding MEM_REF offset and replace the
1335 address with &OBJ. */
1336 addr_base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (op
->op0
, 0),
1337 &addr_offset
, vn_valueize
);
1338 gcc_checking_assert (addr_base
&& TREE_CODE (addr_base
) != MEM_REF
);
1339 if (addr_base
!= TREE_OPERAND (op
->op0
, 0))
1342 = (poly_offset_int::from (wi::to_poly_wide (mem_op
->op0
),
1345 mem_op
->op0
= wide_int_to_tree (TREE_TYPE (mem_op
->op0
), off
);
1346 op
->op0
= build_fold_addr_expr (addr_base
);
1347 if (tree_fits_shwi_p (mem_op
->op0
))
1348 mem_op
->off
= tree_to_shwi (mem_op
->op0
);
1356 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1357 *I_P to point to the last element of the replacement. */
1359 vn_reference_maybe_forwprop_address (vec
<vn_reference_op_s
> *ops
,
1362 bool changed
= false;
1363 vn_reference_op_t op
;
1367 unsigned int i
= *i_p
;
1369 vn_reference_op_t mem_op
= &(*ops
)[i
- 1];
1371 enum tree_code code
;
1372 poly_offset_int off
;
1374 def_stmt
= SSA_NAME_DEF_STMT (op
->op0
);
1375 if (!is_gimple_assign (def_stmt
))
1378 code
= gimple_assign_rhs_code (def_stmt
);
1379 if (code
!= ADDR_EXPR
1380 && code
!= POINTER_PLUS_EXPR
)
1383 off
= poly_offset_int::from (wi::to_poly_wide (mem_op
->op0
), SIGNED
);
1385 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1386 from .foo.bar to the preceding MEM_REF offset and replace the
1387 address with &OBJ. */
1388 if (code
== ADDR_EXPR
)
1390 tree addr
, addr_base
;
1391 poly_int64 addr_offset
;
1393 addr
= gimple_assign_rhs1 (def_stmt
);
1394 addr_base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr
, 0),
1397 /* If that didn't work because the address isn't invariant propagate
1398 the reference tree from the address operation in case the current
1399 dereference isn't offsetted. */
1401 && *i_p
== ops
->length () - 1
1402 && known_eq (off
, 0)
1403 /* This makes us disable this transform for PRE where the
1404 reference ops might be also used for code insertion which
1406 && default_vn_walk_kind
== VN_WALKREWRITE
)
1408 auto_vec
<vn_reference_op_s
, 32> tem
;
1409 copy_reference_ops_from_ref (TREE_OPERAND (addr
, 0), &tem
);
1410 /* Make sure to preserve TBAA info. The only objects not
1411 wrapped in MEM_REFs that can have their address taken are
1413 if (tem
.length () >= 2
1414 && tem
[tem
.length () - 2].opcode
== MEM_REF
)
1416 vn_reference_op_t new_mem_op
= &tem
[tem
.length () - 2];
1418 = wide_int_to_tree (TREE_TYPE (mem_op
->op0
),
1419 wi::to_poly_wide (new_mem_op
->op0
));
1422 gcc_assert (tem
.last ().opcode
== STRING_CST
);
1425 ops
->safe_splice (tem
);
1430 || TREE_CODE (addr_base
) != MEM_REF
1431 || (TREE_CODE (TREE_OPERAND (addr_base
, 0)) == SSA_NAME
1432 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base
,
1437 off
+= mem_ref_offset (addr_base
);
1438 op
->op0
= TREE_OPERAND (addr_base
, 0);
1443 ptr
= gimple_assign_rhs1 (def_stmt
);
1444 ptroff
= gimple_assign_rhs2 (def_stmt
);
1445 if (TREE_CODE (ptr
) != SSA_NAME
1446 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr
)
1447 /* Make sure to not endlessly recurse.
1448 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1449 happen when we value-number a PHI to its backedge value. */
1450 || SSA_VAL (ptr
) == op
->op0
1451 || !poly_int_tree_p (ptroff
))
1454 off
+= wi::to_poly_offset (ptroff
);
1458 mem_op
->op0
= wide_int_to_tree (TREE_TYPE (mem_op
->op0
), off
);
1459 if (tree_fits_shwi_p (mem_op
->op0
))
1460 mem_op
->off
= tree_to_shwi (mem_op
->op0
);
1463 /* ??? Can end up with endless recursion here!?
1464 gcc.c-torture/execute/strcmp-1.c */
1465 if (TREE_CODE (op
->op0
) == SSA_NAME
)
1466 op
->op0
= SSA_VAL (op
->op0
);
1467 if (TREE_CODE (op
->op0
) != SSA_NAME
)
1468 op
->opcode
= TREE_CODE (op
->op0
);
1473 while (TREE_CODE (op
->op0
) == SSA_NAME
);
1475 /* Fold a remaining *&. */
1476 if (TREE_CODE (op
->op0
) == ADDR_EXPR
)
1477 vn_reference_fold_indirect (ops
, i_p
);
1482 /* Optimize the reference REF to a constant if possible or return
1483 NULL_TREE if not. */
1486 fully_constant_vn_reference_p (vn_reference_t ref
)
1488 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1489 vn_reference_op_t op
;
1491 /* Try to simplify the translated expression if it is
1492 a call to a builtin function with at most two arguments. */
1494 if (op
->opcode
== CALL_EXPR
1496 || (TREE_CODE (op
->op0
) == ADDR_EXPR
1497 && TREE_CODE (TREE_OPERAND (op
->op0
, 0)) == FUNCTION_DECL
1498 && fndecl_built_in_p (TREE_OPERAND (op
->op0
, 0),
1500 && operands
.length () >= 2
1501 && operands
.length () <= 3)
1503 vn_reference_op_t arg0
, arg1
= NULL
;
1504 bool anyconst
= false;
1505 arg0
= &operands
[1];
1506 if (operands
.length () > 2)
1507 arg1
= &operands
[2];
1508 if (TREE_CODE_CLASS (arg0
->opcode
) == tcc_constant
1509 || (arg0
->opcode
== ADDR_EXPR
1510 && is_gimple_min_invariant (arg0
->op0
)))
1513 && (TREE_CODE_CLASS (arg1
->opcode
) == tcc_constant
1514 || (arg1
->opcode
== ADDR_EXPR
1515 && is_gimple_min_invariant (arg1
->op0
))))
1521 fn
= as_combined_fn (DECL_FUNCTION_CODE
1522 (TREE_OPERAND (op
->op0
, 0)));
1524 fn
= as_combined_fn ((internal_fn
) op
->clique
);
1527 folded
= fold_const_call (fn
, ref
->type
, arg0
->op0
, arg1
->op0
);
1529 folded
= fold_const_call (fn
, ref
->type
, arg0
->op0
);
1531 && is_gimple_min_invariant (folded
))
1536 /* Simplify reads from constants or constant initializers. */
1537 else if (BITS_PER_UNIT
== 8
1539 && COMPLETE_TYPE_P (ref
->type
)
1540 && is_gimple_reg_type (ref
->type
))
1544 if (INTEGRAL_TYPE_P (ref
->type
))
1545 size
= TYPE_PRECISION (ref
->type
);
1546 else if (tree_fits_shwi_p (TYPE_SIZE (ref
->type
)))
1547 size
= tree_to_shwi (TYPE_SIZE (ref
->type
));
1550 if (size
% BITS_PER_UNIT
!= 0
1551 || size
> MAX_BITSIZE_MODE_ANY_MODE
)
1553 size
/= BITS_PER_UNIT
;
1555 for (i
= 0; i
< operands
.length (); ++i
)
1557 if (TREE_CODE_CLASS (operands
[i
].opcode
) == tcc_constant
)
1562 if (known_eq (operands
[i
].off
, -1))
1564 off
+= operands
[i
].off
;
1565 if (operands
[i
].opcode
== MEM_REF
)
1571 vn_reference_op_t base
= &operands
[--i
];
1572 tree ctor
= error_mark_node
;
1573 tree decl
= NULL_TREE
;
1574 if (TREE_CODE_CLASS (base
->opcode
) == tcc_constant
)
1576 else if (base
->opcode
== MEM_REF
1577 && base
[1].opcode
== ADDR_EXPR
1578 && (VAR_P (TREE_OPERAND (base
[1].op0
, 0))
1579 || TREE_CODE (TREE_OPERAND (base
[1].op0
, 0)) == CONST_DECL
1580 || TREE_CODE (TREE_OPERAND (base
[1].op0
, 0)) == STRING_CST
))
1582 decl
= TREE_OPERAND (base
[1].op0
, 0);
1583 if (TREE_CODE (decl
) == STRING_CST
)
1586 ctor
= ctor_for_folding (decl
);
1588 if (ctor
== NULL_TREE
)
1589 return build_zero_cst (ref
->type
);
1590 else if (ctor
!= error_mark_node
)
1592 HOST_WIDE_INT const_off
;
1595 tree res
= fold_ctor_reference (ref
->type
, ctor
,
1596 off
* BITS_PER_UNIT
,
1597 size
* BITS_PER_UNIT
, decl
);
1600 STRIP_USELESS_TYPE_CONVERSION (res
);
1601 if (is_gimple_min_invariant (res
))
1605 else if (off
.is_constant (&const_off
))
1607 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
1608 int len
= native_encode_expr (ctor
, buf
, size
, const_off
);
1610 return native_interpret_expr (ref
->type
, buf
, len
);
1618 /* Return true if OPS contain a storage order barrier. */
1621 contains_storage_order_barrier_p (vec
<vn_reference_op_s
> ops
)
1623 vn_reference_op_t op
;
1626 FOR_EACH_VEC_ELT (ops
, i
, op
)
1627 if (op
->opcode
== VIEW_CONVERT_EXPR
&& op
->reverse
)
1633 /* Return true if OPS represent an access with reverse storage order. */
1636 reverse_storage_order_for_component_p (vec
<vn_reference_op_s
> ops
)
1639 if (ops
[i
].opcode
== REALPART_EXPR
|| ops
[i
].opcode
== IMAGPART_EXPR
)
1641 switch (ops
[i
].opcode
)
1647 return ops
[i
].reverse
;
1653 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1654 structures into their value numbers. This is done in-place, and
1655 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1656 whether any operands were valueized. */
1659 valueize_refs_1 (vec
<vn_reference_op_s
> *orig
, bool *valueized_anything
,
1660 bool with_avail
= false)
1662 *valueized_anything
= false;
1664 for (unsigned i
= 0; i
< orig
->length (); ++i
)
1667 vn_reference_op_t vro
= &(*orig
)[i
];
1668 if (vro
->opcode
== SSA_NAME
1669 || (vro
->op0
&& TREE_CODE (vro
->op0
) == SSA_NAME
))
1671 tree tem
= with_avail
? vn_valueize (vro
->op0
) : SSA_VAL (vro
->op0
);
1672 if (tem
!= vro
->op0
)
1674 *valueized_anything
= true;
1677 /* If it transforms from an SSA_NAME to a constant, update
1679 if (TREE_CODE (vro
->op0
) != SSA_NAME
&& vro
->opcode
== SSA_NAME
)
1680 vro
->opcode
= TREE_CODE (vro
->op0
);
1682 if (vro
->op1
&& TREE_CODE (vro
->op1
) == SSA_NAME
)
1684 tree tem
= with_avail
? vn_valueize (vro
->op1
) : SSA_VAL (vro
->op1
);
1685 if (tem
!= vro
->op1
)
1687 *valueized_anything
= true;
1691 if (vro
->op2
&& TREE_CODE (vro
->op2
) == SSA_NAME
)
1693 tree tem
= with_avail
? vn_valueize (vro
->op2
) : SSA_VAL (vro
->op2
);
1694 if (tem
!= vro
->op2
)
1696 *valueized_anything
= true;
1700 /* If it transforms from an SSA_NAME to an address, fold with
1701 a preceding indirect reference. */
1704 && TREE_CODE (vro
->op0
) == ADDR_EXPR
1705 && (*orig
)[i
- 1].opcode
== MEM_REF
)
1707 if (vn_reference_fold_indirect (orig
, &i
))
1708 *valueized_anything
= true;
1711 && vro
->opcode
== SSA_NAME
1712 && (*orig
)[i
- 1].opcode
== MEM_REF
)
1714 if (vn_reference_maybe_forwprop_address (orig
, &i
))
1716 *valueized_anything
= true;
1717 /* Re-valueize the current operand. */
1721 /* If it transforms a non-constant ARRAY_REF into a constant
1722 one, adjust the constant offset. */
1723 else if (vro
->opcode
== ARRAY_REF
1724 && known_eq (vro
->off
, -1)
1725 && poly_int_tree_p (vro
->op0
)
1726 && poly_int_tree_p (vro
->op1
)
1727 && TREE_CODE (vro
->op2
) == INTEGER_CST
)
1729 poly_offset_int off
= ((wi::to_poly_offset (vro
->op0
)
1730 - wi::to_poly_offset (vro
->op1
))
1731 * wi::to_offset (vro
->op2
)
1732 * vn_ref_op_align_unit (vro
));
1733 off
.to_shwi (&vro
->off
);
1739 valueize_refs (vec
<vn_reference_op_s
> *orig
)
1742 valueize_refs_1 (orig
, &tem
);
1745 static vec
<vn_reference_op_s
> shared_lookup_references
;
1747 /* Create a vector of vn_reference_op_s structures from REF, a
1748 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1749 this function. *VALUEIZED_ANYTHING will specify whether any
1750 operands were valueized. */
1752 static vec
<vn_reference_op_s
>
1753 valueize_shared_reference_ops_from_ref (tree ref
, bool *valueized_anything
)
1757 shared_lookup_references
.truncate (0);
1758 copy_reference_ops_from_ref (ref
, &shared_lookup_references
);
1759 valueize_refs_1 (&shared_lookup_references
, valueized_anything
);
1760 return shared_lookup_references
;
1763 /* Create a vector of vn_reference_op_s structures from CALL, a
1764 call statement. The vector is shared among all callers of
1767 static vec
<vn_reference_op_s
>
1768 valueize_shared_reference_ops_from_call (gcall
*call
)
1772 shared_lookup_references
.truncate (0);
1773 copy_reference_ops_from_call (call
, &shared_lookup_references
);
1774 valueize_refs (&shared_lookup_references
);
1775 return shared_lookup_references
;
1778 /* Lookup a SCCVN reference operation VR in the current hash table.
1779 Returns the resulting value number if it exists in the hash table,
1780 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1781 vn_reference_t stored in the hashtable if something is found. */
1784 vn_reference_lookup_1 (vn_reference_t vr
, vn_reference_t
*vnresult
)
1786 vn_reference_s
**slot
;
1789 hash
= vr
->hashcode
;
1790 slot
= valid_info
->references
->find_slot_with_hash (vr
, hash
, NO_INSERT
);
1794 *vnresult
= (vn_reference_t
)*slot
;
1795 return ((vn_reference_t
)*slot
)->result
;
1802 /* Partial definition tracking support. */
1806 HOST_WIDE_INT offset
;
1813 HOST_WIDE_INT rhs_off
;
1814 HOST_WIDE_INT offset
;
1818 /* Context for alias walking. */
1820 struct vn_walk_cb_data
1822 vn_walk_cb_data (vn_reference_t vr_
, tree orig_ref_
, tree
*last_vuse_ptr_
,
1823 vn_lookup_kind vn_walk_kind_
, bool tbaa_p_
, tree mask_
,
1824 bool redundant_store_removal_p_
)
1825 : vr (vr_
), last_vuse_ptr (last_vuse_ptr_
), last_vuse (NULL_TREE
),
1826 mask (mask_
), masked_result (NULL_TREE
), same_val (NULL_TREE
),
1827 vn_walk_kind (vn_walk_kind_
),
1828 tbaa_p (tbaa_p_
), redundant_store_removal_p (redundant_store_removal_p_
),
1829 saved_operands (vNULL
), first_set (-2), first_base_set (-2),
1833 last_vuse_ptr
= &last_vuse
;
1834 ao_ref_init (&orig_ref
, orig_ref_
);
1837 wide_int w
= wi::to_wide (mask
);
1838 unsigned int pos
= 0, prec
= w
.get_precision ();
1840 pd
.rhs
= build_constructor (NULL_TREE
, NULL
);
1842 /* When bitwise and with a constant is done on a memory load,
1843 we don't really need all the bits to be defined or defined
1844 to constants, we don't really care what is in the position
1845 corresponding to 0 bits in the mask.
1846 So, push the ranges of those 0 bits in the mask as artificial
1847 zero stores and let the partial def handling code do the
1851 int tz
= wi::ctz (w
);
1852 if (pos
+ tz
> prec
)
1856 if (BYTES_BIG_ENDIAN
)
1857 pd
.offset
= prec
- pos
- tz
;
1861 void *r
= push_partial_def (pd
, 0, 0, 0, prec
);
1862 gcc_assert (r
== NULL_TREE
);
1867 w
= wi::lrshift (w
, tz
);
1868 tz
= wi::ctz (wi::bit_not (w
));
1869 if (pos
+ tz
> prec
)
1872 w
= wi::lrshift (w
, tz
);
1876 ~vn_walk_cb_data ();
1877 void *finish (alias_set_type
, alias_set_type
, tree
);
1878 void *push_partial_def (pd_data pd
,
1879 alias_set_type
, alias_set_type
, HOST_WIDE_INT
,
1884 tree
*last_vuse_ptr
;
1889 vn_lookup_kind vn_walk_kind
;
1891 bool redundant_store_removal_p
;
1892 vec
<vn_reference_op_s
> saved_operands
;
1894 /* The VDEFs of partial defs we come along. */
1895 auto_vec
<pd_data
, 2> partial_defs
;
1896 /* The first defs range to avoid splay tree setup in most cases. */
1897 pd_range first_range
;
1898 alias_set_type first_set
;
1899 alias_set_type first_base_set
;
1900 splay_tree known_ranges
;
1901 obstack ranges_obstack
;
1904 vn_walk_cb_data::~vn_walk_cb_data ()
1908 splay_tree_delete (known_ranges
);
1909 obstack_free (&ranges_obstack
, NULL
);
1911 saved_operands
.release ();
1915 vn_walk_cb_data::finish (alias_set_type set
, alias_set_type base_set
, tree val
)
1917 if (first_set
!= -2)
1920 base_set
= first_base_set
;
1924 masked_result
= val
;
1927 if (same_val
&& !operand_equal_p (val
, same_val
))
1929 vec
<vn_reference_op_s
> &operands
1930 = saved_operands
.exists () ? saved_operands
: vr
->operands
;
1931 return vn_reference_lookup_or_insert_for_pieces (last_vuse
, set
, base_set
,
1932 vr
->type
, operands
, val
);
1935 /* pd_range splay-tree helpers. */
1938 pd_range_compare (splay_tree_key offset1p
, splay_tree_key offset2p
)
1940 HOST_WIDE_INT offset1
= *(HOST_WIDE_INT
*)offset1p
;
1941 HOST_WIDE_INT offset2
= *(HOST_WIDE_INT
*)offset2p
;
1942 if (offset1
< offset2
)
1944 else if (offset1
> offset2
)
1950 pd_tree_alloc (int size
, void *data_
)
1952 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
1953 return obstack_alloc (&data
->ranges_obstack
, size
);
1957 pd_tree_dealloc (void *, void *)
1961 /* Push PD to the vector of partial definitions returning a
1962 value when we are ready to combine things with VUSE, SET and MAXSIZEI,
1963 NULL when we want to continue looking for partial defs or -1
1967 vn_walk_cb_data::push_partial_def (pd_data pd
,
1968 alias_set_type set
, alias_set_type base_set
,
1969 HOST_WIDE_INT offseti
,
1970 HOST_WIDE_INT maxsizei
)
1972 const HOST_WIDE_INT bufsize
= 64;
1973 /* We're using a fixed buffer for encoding so fail early if the object
1974 we want to interpret is bigger. */
1975 if (maxsizei
> bufsize
* BITS_PER_UNIT
1977 || BITS_PER_UNIT
!= 8
1978 /* Not prepared to handle PDP endian. */
1979 || BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
1982 /* Turn too large constant stores into non-constant stores. */
1983 if (CONSTANT_CLASS_P (pd
.rhs
) && pd
.size
> bufsize
* BITS_PER_UNIT
)
1984 pd
.rhs
= error_mark_node
;
1986 /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
1987 most a partial byte before and/or after the region. */
1988 if (!CONSTANT_CLASS_P (pd
.rhs
))
1990 if (pd
.offset
< offseti
)
1992 HOST_WIDE_INT o
= ROUND_DOWN (offseti
- pd
.offset
, BITS_PER_UNIT
);
1993 gcc_assert (pd
.size
> o
);
1997 if (pd
.size
> maxsizei
)
1998 pd
.size
= maxsizei
+ ((pd
.size
- maxsizei
) % BITS_PER_UNIT
);
2001 pd
.offset
-= offseti
;
2003 bool pd_constant_p
= (TREE_CODE (pd
.rhs
) == CONSTRUCTOR
2004 || CONSTANT_CLASS_P (pd
.rhs
));
2006 if (partial_defs
.is_empty ())
2008 /* If we get a clobber upfront, fail. */
2009 if (TREE_CLOBBER_P (pd
.rhs
))
2013 partial_defs
.safe_push (pd
);
2014 first_range
.offset
= pd
.offset
;
2015 first_range
.size
= pd
.size
;
2017 first_base_set
= base_set
;
2018 last_vuse_ptr
= NULL
;
2020 /* Go check if the first partial definition was a full one in case
2021 the caller didn't optimize for this. */
2027 /* ??? Optimize the case where the 2nd partial def completes
2029 gcc_obstack_init (&ranges_obstack
);
2030 known_ranges
= splay_tree_new_with_allocator (pd_range_compare
, 0, 0,
2032 pd_tree_dealloc
, this);
2033 splay_tree_insert (known_ranges
,
2034 (splay_tree_key
)&first_range
.offset
,
2035 (splay_tree_value
)&first_range
);
2038 pd_range newr
= { pd
.offset
, pd
.size
};
2040 /* Lookup the predecessor of offset + 1 and see if we need to merge. */
2041 HOST_WIDE_INT loffset
= newr
.offset
+ 1;
2042 if ((n
= splay_tree_predecessor (known_ranges
, (splay_tree_key
)&loffset
))
2043 && ((r
= (pd_range
*)n
->value
), true)
2044 && ranges_known_overlap_p (r
->offset
, r
->size
+ 1,
2045 newr
.offset
, newr
.size
))
2047 /* Ignore partial defs already covered. Here we also drop shadowed
2048 clobbers arriving here at the floor. */
2049 if (known_subrange_p (newr
.offset
, newr
.size
, r
->offset
, r
->size
))
2052 = MAX (r
->offset
+ r
->size
, newr
.offset
+ newr
.size
) - r
->offset
;
2056 /* newr.offset wasn't covered yet, insert the range. */
2057 r
= XOBNEW (&ranges_obstack
, pd_range
);
2059 splay_tree_insert (known_ranges
, (splay_tree_key
)&r
->offset
,
2060 (splay_tree_value
)r
);
2062 /* Merge r which now contains newr and is a member of the splay tree with
2063 adjacent overlapping ranges. */
2065 while ((n
= splay_tree_successor (known_ranges
,
2066 (splay_tree_key
)&r
->offset
))
2067 && ((rafter
= (pd_range
*)n
->value
), true)
2068 && ranges_known_overlap_p (r
->offset
, r
->size
+ 1,
2069 rafter
->offset
, rafter
->size
))
2071 r
->size
= MAX (r
->offset
+ r
->size
,
2072 rafter
->offset
+ rafter
->size
) - r
->offset
;
2073 splay_tree_remove (known_ranges
, (splay_tree_key
)&rafter
->offset
);
2075 /* If we get a clobber, fail. */
2076 if (TREE_CLOBBER_P (pd
.rhs
))
2078 /* Non-constants are OK as long as they are shadowed by a constant. */
2081 partial_defs
.safe_push (pd
);
2084 /* Now we have merged newr into the range tree. When we have covered
2085 [offseti, sizei] then the tree will contain exactly one node which has
2086 the desired properties and it will be 'r'. */
2087 if (!known_subrange_p (0, maxsizei
, r
->offset
, r
->size
))
2088 /* Continue looking for partial defs. */
2091 /* Now simply native encode all partial defs in reverse order. */
2092 unsigned ndefs
= partial_defs
.length ();
2093 /* We support up to 512-bit values (for V8DFmode). */
2094 unsigned char buffer
[bufsize
+ 1];
2095 unsigned char this_buffer
[bufsize
+ 1];
2098 memset (buffer
, 0, bufsize
+ 1);
2099 unsigned needed_len
= ROUND_UP (maxsizei
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
2100 while (!partial_defs
.is_empty ())
2102 pd_data pd
= partial_defs
.pop ();
2104 if (TREE_CODE (pd
.rhs
) == CONSTRUCTOR
)
2106 /* Empty CONSTRUCTOR. */
2107 if (pd
.size
>= needed_len
* BITS_PER_UNIT
)
2110 len
= ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
2111 memset (this_buffer
, 0, len
);
2113 else if (pd
.rhs_off
>= 0)
2115 len
= native_encode_expr (pd
.rhs
, this_buffer
, bufsize
,
2116 (MAX (0, -pd
.offset
)
2117 + pd
.rhs_off
) / BITS_PER_UNIT
);
2119 || len
< (ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
2120 - MAX (0, -pd
.offset
) / BITS_PER_UNIT
))
2122 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2123 fprintf (dump_file
, "Failed to encode %u "
2124 "partial definitions\n", ndefs
);
2128 else /* negative pd.rhs_off indicates we want to chop off first bits */
2130 if (-pd
.rhs_off
>= bufsize
)
2132 len
= native_encode_expr (pd
.rhs
,
2133 this_buffer
+ -pd
.rhs_off
/ BITS_PER_UNIT
,
2134 bufsize
- -pd
.rhs_off
/ BITS_PER_UNIT
,
2135 MAX (0, -pd
.offset
) / BITS_PER_UNIT
);
2137 || len
< (ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
2138 - MAX (0, -pd
.offset
) / BITS_PER_UNIT
))
2140 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2141 fprintf (dump_file
, "Failed to encode %u "
2142 "partial definitions\n", ndefs
);
2147 unsigned char *p
= buffer
;
2148 HOST_WIDE_INT size
= pd
.size
;
2150 size
-= ROUND_DOWN (-pd
.offset
, BITS_PER_UNIT
);
2151 this_buffer
[len
] = 0;
2152 if (BYTES_BIG_ENDIAN
)
2154 /* LSB of this_buffer[len - 1] byte should be at
2155 pd.offset + pd.size - 1 bits in buffer. */
2156 amnt
= ((unsigned HOST_WIDE_INT
) pd
.offset
2157 + pd
.size
) % BITS_PER_UNIT
;
2159 shift_bytes_in_array_right (this_buffer
, len
+ 1, amnt
);
2160 unsigned char *q
= this_buffer
;
2161 unsigned int off
= 0;
2165 off
= pd
.offset
/ BITS_PER_UNIT
;
2166 gcc_assert (off
< needed_len
);
2170 msk
= ((1 << size
) - 1) << (BITS_PER_UNIT
- amnt
);
2171 *p
= (*p
& ~msk
) | (this_buffer
[len
] & msk
);
2176 if (TREE_CODE (pd
.rhs
) != CONSTRUCTOR
)
2177 q
= (this_buffer
+ len
2178 - (ROUND_UP (size
- amnt
, BITS_PER_UNIT
)
2180 if (pd
.offset
% BITS_PER_UNIT
)
2182 msk
= -1U << (BITS_PER_UNIT
2183 - (pd
.offset
% BITS_PER_UNIT
));
2184 *p
= (*p
& msk
) | (*q
& ~msk
);
2188 size
-= BITS_PER_UNIT
- (pd
.offset
% BITS_PER_UNIT
);
2189 gcc_assert (size
>= 0);
2193 else if (TREE_CODE (pd
.rhs
) != CONSTRUCTOR
)
2195 q
= (this_buffer
+ len
2196 - (ROUND_UP (size
- amnt
, BITS_PER_UNIT
)
2198 if (pd
.offset
% BITS_PER_UNIT
)
2201 size
-= BITS_PER_UNIT
- ((unsigned HOST_WIDE_INT
) pd
.offset
2203 gcc_assert (size
>= 0);
2206 if ((unsigned HOST_WIDE_INT
) size
/ BITS_PER_UNIT
+ off
2208 size
= (needed_len
- off
) * BITS_PER_UNIT
;
2209 memcpy (p
, q
, size
/ BITS_PER_UNIT
);
2210 if (size
% BITS_PER_UNIT
)
2213 = -1U << (BITS_PER_UNIT
- (size
% BITS_PER_UNIT
));
2214 p
+= size
/ BITS_PER_UNIT
;
2215 q
+= size
/ BITS_PER_UNIT
;
2216 *p
= (*q
& msk
) | (*p
& ~msk
);
2223 /* LSB of this_buffer[0] byte should be at pd.offset bits
2226 size
= MIN (size
, (HOST_WIDE_INT
) needed_len
* BITS_PER_UNIT
);
2227 amnt
= pd
.offset
% BITS_PER_UNIT
;
2229 shift_bytes_in_array_left (this_buffer
, len
+ 1, amnt
);
2230 unsigned int off
= pd
.offset
/ BITS_PER_UNIT
;
2231 gcc_assert (off
< needed_len
);
2233 (HOST_WIDE_INT
) (needed_len
- off
) * BITS_PER_UNIT
);
2235 if (amnt
+ size
< BITS_PER_UNIT
)
2237 /* Low amnt bits come from *p, then size bits
2238 from this_buffer[0] and the remaining again from
2240 msk
= ((1 << size
) - 1) << amnt
;
2241 *p
= (*p
& ~msk
) | (this_buffer
[0] & msk
);
2247 *p
= (*p
& ~msk
) | (this_buffer
[0] & msk
);
2249 size
-= (BITS_PER_UNIT
- amnt
);
2254 amnt
= (unsigned HOST_WIDE_INT
) pd
.offset
% BITS_PER_UNIT
;
2256 size
-= BITS_PER_UNIT
- amnt
;
2257 size
= MIN (size
, (HOST_WIDE_INT
) needed_len
* BITS_PER_UNIT
);
2259 shift_bytes_in_array_left (this_buffer
, len
+ 1, amnt
);
2261 memcpy (p
, this_buffer
+ (amnt
!= 0), size
/ BITS_PER_UNIT
);
2262 p
+= size
/ BITS_PER_UNIT
;
2263 if (size
% BITS_PER_UNIT
)
2265 unsigned int msk
= -1U << (size
% BITS_PER_UNIT
);
2266 *p
= (this_buffer
[(amnt
!= 0) + size
/ BITS_PER_UNIT
]
2267 & ~msk
) | (*p
& msk
);
2272 tree type
= vr
->type
;
2273 /* Make sure to interpret in a type that has a range covering the whole
2275 if (INTEGRAL_TYPE_P (vr
->type
) && maxsizei
!= TYPE_PRECISION (vr
->type
))
2276 type
= build_nonstandard_integer_type (maxsizei
, TYPE_UNSIGNED (type
));
2278 if (BYTES_BIG_ENDIAN
)
2280 unsigned sz
= needed_len
;
2281 if (maxsizei
% BITS_PER_UNIT
)
2282 shift_bytes_in_array_right (buffer
, needed_len
,
2284 - (maxsizei
% BITS_PER_UNIT
));
2285 if (INTEGRAL_TYPE_P (type
))
2286 sz
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
2287 if (sz
> needed_len
)
2289 memcpy (this_buffer
+ (sz
- needed_len
), buffer
, needed_len
);
2290 val
= native_interpret_expr (type
, this_buffer
, sz
);
2293 val
= native_interpret_expr (type
, buffer
, needed_len
);
2296 val
= native_interpret_expr (type
, buffer
, bufsize
);
2297 /* If we chop off bits because the types precision doesn't match the memory
2298 access size this is ok when optimizing reads but not when called from
2299 the DSE code during elimination. */
2300 if (val
&& type
!= vr
->type
)
2302 if (! int_fits_type_p (val
, vr
->type
))
2305 val
= fold_convert (vr
->type
, val
);
2310 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2312 "Successfully combined %u partial definitions\n", ndefs
);
2313 /* We are using the alias-set of the first store we encounter which
2314 should be appropriate here. */
2315 return finish (first_set
, first_base_set
, val
);
2319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2321 "Failed to interpret %u encoded partial definitions\n", ndefs
);
2326 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2327 with the current VUSE and performs the expression lookup. */
2330 vn_reference_lookup_2 (ao_ref
*op ATTRIBUTE_UNUSED
, tree vuse
, void *data_
)
2332 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
2333 vn_reference_t vr
= data
->vr
;
2334 vn_reference_s
**slot
;
2337 /* If we have partial definitions recorded we have to go through
2338 vn_reference_lookup_3. */
2339 if (!data
->partial_defs
.is_empty ())
2342 if (data
->last_vuse_ptr
)
2344 *data
->last_vuse_ptr
= vuse
;
2345 data
->last_vuse
= vuse
;
2348 /* Fixup vuse and hash. */
2350 vr
->hashcode
= vr
->hashcode
- SSA_NAME_VERSION (vr
->vuse
);
2351 vr
->vuse
= vuse_ssa_val (vuse
);
2353 vr
->hashcode
= vr
->hashcode
+ SSA_NAME_VERSION (vr
->vuse
);
2355 hash
= vr
->hashcode
;
2356 slot
= valid_info
->references
->find_slot_with_hash (vr
, hash
, NO_INSERT
);
2359 if ((*slot
)->result
&& data
->saved_operands
.exists ())
2360 return data
->finish (vr
->set
, vr
->base_set
, (*slot
)->result
);
2367 /* Lookup an existing or insert a new vn_reference entry into the
2368 value table for the VUSE, SET, TYPE, OPERANDS reference which
2369 has the value VALUE which is either a constant or an SSA name. */
2371 static vn_reference_t
2372 vn_reference_lookup_or_insert_for_pieces (tree vuse
,
2374 alias_set_type base_set
,
2376 vec
<vn_reference_op_s
,
2381 vn_reference_t result
;
2383 vr1
.vuse
= vuse
? SSA_VAL (vuse
) : NULL_TREE
;
2384 vr1
.operands
= operands
;
2387 vr1
.base_set
= base_set
;
2388 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
2389 if (vn_reference_lookup_1 (&vr1
, &result
))
2391 if (TREE_CODE (value
) == SSA_NAME
)
2392 value_id
= VN_INFO (value
)->value_id
;
2394 value_id
= get_or_alloc_constant_value_id (value
);
2395 return vn_reference_insert_pieces (vuse
, set
, base_set
, type
,
2396 operands
.copy (), value
, value_id
);
2399 /* Return a value-number for RCODE OPS... either by looking up an existing
2400 value-number for the possibly simplified result or by inserting the
2401 operation if INSERT is true. If SIMPLIFY is false, return a value
2402 number for the unsimplified expression. */
2405 vn_nary_build_or_lookup_1 (gimple_match_op
*res_op
, bool insert
,
2408 tree result
= NULL_TREE
;
2409 /* We will be creating a value number for
2411 So first simplify and lookup this expression to see if it
2412 is already available. */
2413 /* For simplification valueize. */
2416 for (i
= 0; i
< res_op
->num_ops
; ++i
)
2417 if (TREE_CODE (res_op
->ops
[i
]) == SSA_NAME
)
2419 tree tem
= vn_valueize (res_op
->ops
[i
]);
2422 res_op
->ops
[i
] = tem
;
2424 /* If valueization of an operand fails (it is not available), skip
2427 if (i
== res_op
->num_ops
)
2429 mprts_hook
= vn_lookup_simplify_result
;
2430 res
= res_op
->resimplify (NULL
, vn_valueize
);
2433 gimple
*new_stmt
= NULL
;
2435 && gimple_simplified_result_is_gimple_val (res_op
))
2437 /* The expression is already available. */
2438 result
= res_op
->ops
[0];
2439 /* Valueize it, simplification returns sth in AVAIL only. */
2440 if (TREE_CODE (result
) == SSA_NAME
)
2441 result
= SSA_VAL (result
);
2445 tree val
= vn_lookup_simplify_result (res_op
);
2448 gimple_seq stmts
= NULL
;
2449 result
= maybe_push_res_to_seq (res_op
, &stmts
);
2452 gcc_assert (gimple_seq_singleton_p (stmts
));
2453 new_stmt
= gimple_seq_first_stmt (stmts
);
2457 /* The expression is already available. */
2462 /* The expression is not yet available, value-number lhs to
2463 the new SSA_NAME we created. */
2464 /* Initialize value-number information properly. */
2465 vn_ssa_aux_t result_info
= VN_INFO (result
);
2466 result_info
->valnum
= result
;
2467 result_info
->value_id
= get_next_value_id ();
2468 result_info
->visited
= 1;
2469 gimple_seq_add_stmt_without_update (&VN_INFO (result
)->expr
,
2471 result_info
->needs_insertion
= true;
2472 /* ??? PRE phi-translation inserts NARYs without corresponding
2473 SSA name result. Re-use those but set their result according
2474 to the stmt we just built. */
2475 vn_nary_op_t nary
= NULL
;
2476 vn_nary_op_lookup_stmt (new_stmt
, &nary
);
2479 gcc_assert (! nary
->predicated_values
&& nary
->u
.result
== NULL_TREE
);
2480 nary
->u
.result
= gimple_assign_lhs (new_stmt
);
2482 /* As all "inserted" statements are singleton SCCs, insert
2483 to the valid table. This is strictly needed to
2484 avoid re-generating new value SSA_NAMEs for the same
2485 expression during SCC iteration over and over (the
2486 optimistic table gets cleared after each iteration).
2487 We do not need to insert into the optimistic table, as
2488 lookups there will fall back to the valid table. */
2491 unsigned int length
= vn_nary_length_from_stmt (new_stmt
);
2493 = alloc_vn_nary_op_noinit (length
, &vn_tables_insert_obstack
);
2494 vno1
->value_id
= result_info
->value_id
;
2495 vno1
->length
= length
;
2496 vno1
->predicated_values
= 0;
2497 vno1
->u
.result
= result
;
2498 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (new_stmt
));
2499 vn_nary_op_insert_into (vno1
, valid_info
->nary
);
2500 /* Also do not link it into the undo chain. */
2501 last_inserted_nary
= vno1
->next
;
2502 vno1
->next
= (vn_nary_op_t
)(void *)-1;
2504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2506 fprintf (dump_file
, "Inserting name ");
2507 print_generic_expr (dump_file
, result
);
2508 fprintf (dump_file
, " for expression ");
2509 print_gimple_expr (dump_file
, new_stmt
, 0, TDF_SLIM
);
2510 fprintf (dump_file
, "\n");
2516 /* Return a value-number for RCODE OPS... either by looking up an existing
2517 value-number for the simplified result or by inserting the operation. */
2520 vn_nary_build_or_lookup (gimple_match_op
*res_op
)
2522 return vn_nary_build_or_lookup_1 (res_op
, true, true);
2525 /* Try to simplify the expression RCODE OPS... of type TYPE and return
2526 its value if present. */
2529 vn_nary_simplify (vn_nary_op_t nary
)
2531 if (nary
->length
> gimple_match_op::MAX_NUM_OPS
)
2533 gimple_match_op
op (gimple_match_cond::UNCOND
, nary
->opcode
,
2534 nary
->type
, nary
->length
);
2535 memcpy (op
.ops
, nary
->op
, sizeof (tree
) * nary
->length
);
2536 return vn_nary_build_or_lookup_1 (&op
, false, true);
2539 /* Elimination engine. */
2541 class eliminate_dom_walker
: public dom_walker
2544 eliminate_dom_walker (cdi_direction
, bitmap
);
2545 ~eliminate_dom_walker ();
2547 edge
before_dom_children (basic_block
) final override
;
2548 void after_dom_children (basic_block
) final override
;
2550 virtual tree
eliminate_avail (basic_block
, tree op
);
2551 virtual void eliminate_push_avail (basic_block
, tree op
);
2552 tree
eliminate_insert (basic_block
, gimple_stmt_iterator
*gsi
, tree val
);
2554 void eliminate_stmt (basic_block
, gimple_stmt_iterator
*);
2556 unsigned eliminate_cleanup (bool region_p
= false);
2559 unsigned int el_todo
;
2560 unsigned int eliminations
;
2561 unsigned int insertions
;
2563 /* SSA names that had their defs inserted by PRE if do_pre. */
2564 bitmap inserted_exprs
;
2566 /* Blocks with statements that have had their EH properties changed. */
2567 bitmap need_eh_cleanup
;
2569 /* Blocks with statements that have had their AB properties changed. */
2570 bitmap need_ab_cleanup
;
2572 /* Local state for the eliminate domwalk. */
2573 auto_vec
<gimple
*> to_remove
;
2574 auto_vec
<gimple
*> to_fixup
;
2575 auto_vec
<tree
> avail
;
2576 auto_vec
<tree
> avail_stack
;
2579 /* Adaptor to the elimination engine using RPO availability. */
2581 class rpo_elim
: public eliminate_dom_walker
2584 rpo_elim(basic_block entry_
)
2585 : eliminate_dom_walker (CDI_DOMINATORS
, NULL
), entry (entry_
),
2586 m_avail_freelist (NULL
) {}
2588 tree
eliminate_avail (basic_block
, tree op
) final override
;
2590 void eliminate_push_avail (basic_block
, tree
) final override
;
2593 /* Freelist of avail entries which are allocated from the vn_ssa_aux
2595 vn_avail
*m_avail_freelist
;
2598 /* Global RPO state for access from hooks. */
2599 static eliminate_dom_walker
*rpo_avail
;
2600 basic_block vn_context_bb
;
2602 /* Return true if BASE1 and BASE2 can be adjusted so they have the
2603 same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2604 Otherwise return false. */
2607 adjust_offsets_for_equal_base_address (tree base1
, poly_int64
*offset1
,
2608 tree base2
, poly_int64
*offset2
)
2611 if (TREE_CODE (base1
) == MEM_REF
2612 && TREE_CODE (base2
) == MEM_REF
)
2614 if (mem_ref_offset (base1
).to_shwi (&soff
))
2616 base1
= TREE_OPERAND (base1
, 0);
2617 *offset1
+= soff
* BITS_PER_UNIT
;
2619 if (mem_ref_offset (base2
).to_shwi (&soff
))
2621 base2
= TREE_OPERAND (base2
, 0);
2622 *offset2
+= soff
* BITS_PER_UNIT
;
2624 return operand_equal_p (base1
, base2
, 0);
2626 return operand_equal_p (base1
, base2
, OEP_ADDRESS_OF
);
2629 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2630 from the statement defining VUSE and if not successful tries to
2631 translate *REFP and VR_ through an aggregate copy at the definition
2632 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2633 of *REF and *VR. If only disambiguation was performed then
2634 *DISAMBIGUATE_ONLY is set to true. */
2637 vn_reference_lookup_3 (ao_ref
*ref
, tree vuse
, void *data_
,
2638 translate_flags
*disambiguate_only
)
2640 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
2641 vn_reference_t vr
= data
->vr
;
2642 gimple
*def_stmt
= SSA_NAME_DEF_STMT (vuse
);
2643 tree base
= ao_ref_base (ref
);
2644 HOST_WIDE_INT offseti
= 0, maxsizei
, sizei
= 0;
2645 static vec
<vn_reference_op_s
> lhs_ops
;
2647 bool lhs_ref_ok
= false;
2648 poly_int64 copy_size
;
2650 /* First try to disambiguate after value-replacing in the definitions LHS. */
2651 if (is_gimple_assign (def_stmt
))
2653 tree lhs
= gimple_assign_lhs (def_stmt
);
2654 bool valueized_anything
= false;
2655 /* Avoid re-allocation overhead. */
2656 lhs_ops
.truncate (0);
2657 basic_block saved_rpo_bb
= vn_context_bb
;
2658 vn_context_bb
= gimple_bb (def_stmt
);
2659 if (*disambiguate_only
<= TR_VALUEIZE_AND_DISAMBIGUATE
)
2661 copy_reference_ops_from_ref (lhs
, &lhs_ops
);
2662 valueize_refs_1 (&lhs_ops
, &valueized_anything
, true);
2664 vn_context_bb
= saved_rpo_bb
;
2665 ao_ref_init (&lhs_ref
, lhs
);
2667 if (valueized_anything
2668 && ao_ref_init_from_vn_reference
2669 (&lhs_ref
, ao_ref_alias_set (&lhs_ref
),
2670 ao_ref_base_alias_set (&lhs_ref
), TREE_TYPE (lhs
), lhs_ops
)
2671 && !refs_may_alias_p_1 (ref
, &lhs_ref
, data
->tbaa_p
))
2673 *disambiguate_only
= TR_VALUEIZE_AND_DISAMBIGUATE
;
2677 /* When the def is a CLOBBER we can optimistically disambiguate
2678 against it since any overlap it would be undefined behavior.
2679 Avoid this for obvious must aliases to save compile-time though.
2680 We also may not do this when the query is used for redundant
2682 if (!data
->redundant_store_removal_p
2683 && gimple_clobber_p (def_stmt
)
2684 && !operand_equal_p (ao_ref_base (&lhs_ref
), base
, OEP_ADDRESS_OF
))
2686 *disambiguate_only
= TR_DISAMBIGUATE
;
2690 /* Besides valueizing the LHS we can also use access-path based
2691 disambiguation on the original non-valueized ref. */
2694 && data
->orig_ref
.ref
)
2696 /* We want to use the non-valueized LHS for this, but avoid redundant
2698 ao_ref
*lref
= &lhs_ref
;
2700 if (valueized_anything
)
2702 ao_ref_init (&lref_alt
, lhs
);
2705 if (!refs_may_alias_p_1 (&data
->orig_ref
, lref
, data
->tbaa_p
))
2707 *disambiguate_only
= (valueized_anything
2708 ? TR_VALUEIZE_AND_DISAMBIGUATE
2714 /* If we reach a clobbering statement try to skip it and see if
2715 we find a VN result with exactly the same value as the
2716 possible clobber. In this case we can ignore the clobber
2717 and return the found value. */
2718 if (is_gimple_reg_type (TREE_TYPE (lhs
))
2719 && types_compatible_p (TREE_TYPE (lhs
), vr
->type
)
2720 && (ref
->ref
|| data
->orig_ref
.ref
)
2722 && data
->partial_defs
.is_empty ()
2723 && multiple_p (get_object_alignment
2724 (ref
->ref
? ref
->ref
: data
->orig_ref
.ref
),
2726 && multiple_p (get_object_alignment (lhs
), ref
->size
))
2728 tree rhs
= gimple_assign_rhs1 (def_stmt
);
2729 /* ??? We may not compare to ahead values which might be from
2730 a different loop iteration but only to loop invariants. Use
2731 CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2732 The one-hop lookup below doesn't have this issue since there's
2733 a virtual PHI before we ever reach a backedge to cross.
2734 We can skip multiple defs as long as they are from the same
2737 && !operand_equal_p (data
->same_val
, rhs
))
2739 else if (CONSTANT_CLASS_P (rhs
))
2741 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2744 "Skipping possible redundant definition ");
2745 print_gimple_stmt (dump_file
, def_stmt
, 0);
2747 /* Delay the actual compare of the values to the end of the walk
2748 but do not update last_vuse from here. */
2749 data
->last_vuse_ptr
= NULL
;
2750 data
->same_val
= rhs
;
2755 tree
*saved_last_vuse_ptr
= data
->last_vuse_ptr
;
2756 /* Do not update last_vuse_ptr in vn_reference_lookup_2. */
2757 data
->last_vuse_ptr
= NULL
;
2758 tree saved_vuse
= vr
->vuse
;
2759 hashval_t saved_hashcode
= vr
->hashcode
;
2760 void *res
= vn_reference_lookup_2 (ref
, gimple_vuse (def_stmt
),
2762 /* Need to restore vr->vuse and vr->hashcode. */
2763 vr
->vuse
= saved_vuse
;
2764 vr
->hashcode
= saved_hashcode
;
2765 data
->last_vuse_ptr
= saved_last_vuse_ptr
;
2766 if (res
&& res
!= (void *)-1)
2768 vn_reference_t vnresult
= (vn_reference_t
) res
;
2769 if (TREE_CODE (rhs
) == SSA_NAME
)
2770 rhs
= SSA_VAL (rhs
);
2771 if (vnresult
->result
2772 && operand_equal_p (vnresult
->result
, rhs
, 0))
2778 else if (*disambiguate_only
<= TR_VALUEIZE_AND_DISAMBIGUATE
2779 && gimple_call_builtin_p (def_stmt
, BUILT_IN_NORMAL
)
2780 && gimple_call_num_args (def_stmt
) <= 4)
2782 /* For builtin calls valueize its arguments and call the
2783 alias oracle again. Valueization may improve points-to
2784 info of pointers and constify size and position arguments.
2785 Originally this was motivated by PR61034 which has
2786 conditional calls to free falsely clobbering ref because
2787 of imprecise points-to info of the argument. */
2789 bool valueized_anything
= false;
2790 for (unsigned i
= 0; i
< gimple_call_num_args (def_stmt
); ++i
)
2792 oldargs
[i
] = gimple_call_arg (def_stmt
, i
);
2793 tree val
= vn_valueize (oldargs
[i
]);
2794 if (val
!= oldargs
[i
])
2796 gimple_call_set_arg (def_stmt
, i
, val
);
2797 valueized_anything
= true;
2800 if (valueized_anything
)
2802 bool res
= call_may_clobber_ref_p_1 (as_a
<gcall
*> (def_stmt
),
2804 for (unsigned i
= 0; i
< gimple_call_num_args (def_stmt
); ++i
)
2805 gimple_call_set_arg (def_stmt
, i
, oldargs
[i
]);
2808 *disambiguate_only
= TR_VALUEIZE_AND_DISAMBIGUATE
;
2814 if (*disambiguate_only
> TR_TRANSLATE
)
2817 /* If we cannot constrain the size of the reference we cannot
2818 test if anything kills it. */
2819 if (!ref
->max_size_known_p ())
2822 poly_int64 offset
= ref
->offset
;
2823 poly_int64 maxsize
= ref
->max_size
;
2825 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2826 from that definition.
2828 if (is_gimple_reg_type (vr
->type
)
2829 && (gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMSET
)
2830 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMSET_CHK
))
2831 && (integer_zerop (gimple_call_arg (def_stmt
, 1))
2832 || ((TREE_CODE (gimple_call_arg (def_stmt
, 1)) == INTEGER_CST
2833 || (INTEGRAL_TYPE_P (vr
->type
) && known_eq (ref
->size
, 8)))
2835 && BITS_PER_UNIT
== 8
2836 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
2837 && offset
.is_constant (&offseti
)
2838 && ref
->size
.is_constant (&sizei
)
2839 && (offseti
% BITS_PER_UNIT
== 0
2840 || TREE_CODE (gimple_call_arg (def_stmt
, 1)) == INTEGER_CST
)))
2841 && (poly_int_tree_p (gimple_call_arg (def_stmt
, 2))
2842 || (TREE_CODE (gimple_call_arg (def_stmt
, 2)) == SSA_NAME
2843 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt
, 2)))))
2844 && (TREE_CODE (gimple_call_arg (def_stmt
, 0)) == ADDR_EXPR
2845 || TREE_CODE (gimple_call_arg (def_stmt
, 0)) == SSA_NAME
))
2848 poly_int64 offset2
, size2
, maxsize2
;
2850 tree ref2
= gimple_call_arg (def_stmt
, 0);
2851 if (TREE_CODE (ref2
) == SSA_NAME
)
2853 ref2
= SSA_VAL (ref2
);
2854 if (TREE_CODE (ref2
) == SSA_NAME
2855 && (TREE_CODE (base
) != MEM_REF
2856 || TREE_OPERAND (base
, 0) != ref2
))
2858 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref2
);
2859 if (gimple_assign_single_p (def_stmt
)
2860 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
2861 ref2
= gimple_assign_rhs1 (def_stmt
);
2864 if (TREE_CODE (ref2
) == ADDR_EXPR
)
2866 ref2
= TREE_OPERAND (ref2
, 0);
2867 base2
= get_ref_base_and_extent (ref2
, &offset2
, &size2
, &maxsize2
,
2869 if (!known_size_p (maxsize2
)
2870 || !known_eq (maxsize2
, size2
)
2871 || !operand_equal_p (base
, base2
, OEP_ADDRESS_OF
))
2874 else if (TREE_CODE (ref2
) == SSA_NAME
)
2877 if (TREE_CODE (base
) != MEM_REF
2878 || !(mem_ref_offset (base
)
2879 << LOG2_BITS_PER_UNIT
).to_shwi (&soff
))
2883 if (TREE_OPERAND (base
, 0) != ref2
)
2885 gimple
*def
= SSA_NAME_DEF_STMT (ref2
);
2886 if (is_gimple_assign (def
)
2887 && gimple_assign_rhs_code (def
) == POINTER_PLUS_EXPR
2888 && gimple_assign_rhs1 (def
) == TREE_OPERAND (base
, 0)
2889 && poly_int_tree_p (gimple_assign_rhs2 (def
)))
2891 tree rhs2
= gimple_assign_rhs2 (def
);
2892 if (!(poly_offset_int::from (wi::to_poly_wide (rhs2
),
2894 << LOG2_BITS_PER_UNIT
).to_shwi (&offset2
))
2896 ref2
= gimple_assign_rhs1 (def
);
2897 if (TREE_CODE (ref2
) == SSA_NAME
)
2898 ref2
= SSA_VAL (ref2
);
2906 tree len
= gimple_call_arg (def_stmt
, 2);
2907 HOST_WIDE_INT leni
, offset2i
;
2908 if (TREE_CODE (len
) == SSA_NAME
)
2909 len
= SSA_VAL (len
);
2910 /* Sometimes the above trickery is smarter than alias analysis. Take
2911 advantage of that. */
2912 if (!ranges_maybe_overlap_p (offset
, maxsize
, offset2
,
2913 (wi::to_poly_offset (len
)
2914 << LOG2_BITS_PER_UNIT
)))
2916 if (data
->partial_defs
.is_empty ()
2917 && known_subrange_p (offset
, maxsize
, offset2
,
2918 wi::to_poly_offset (len
) << LOG2_BITS_PER_UNIT
))
2921 if (integer_zerop (gimple_call_arg (def_stmt
, 1)))
2922 val
= build_zero_cst (vr
->type
);
2923 else if (INTEGRAL_TYPE_P (vr
->type
)
2924 && known_eq (ref
->size
, 8)
2925 && offseti
% BITS_PER_UNIT
== 0)
2927 gimple_match_op
res_op (gimple_match_cond::UNCOND
, NOP_EXPR
,
2928 vr
->type
, gimple_call_arg (def_stmt
, 1));
2929 val
= vn_nary_build_or_lookup (&res_op
);
2931 || (TREE_CODE (val
) == SSA_NAME
2932 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
)))
2937 unsigned buflen
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr
->type
)) + 1;
2938 if (INTEGRAL_TYPE_P (vr
->type
))
2939 buflen
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr
->type
)) + 1;
2940 unsigned char *buf
= XALLOCAVEC (unsigned char, buflen
);
2941 memset (buf
, TREE_INT_CST_LOW (gimple_call_arg (def_stmt
, 1)),
2943 if (BYTES_BIG_ENDIAN
)
2946 = (((unsigned HOST_WIDE_INT
) offseti
+ sizei
)
2950 shift_bytes_in_array_right (buf
, buflen
,
2951 BITS_PER_UNIT
- amnt
);
2956 else if (offseti
% BITS_PER_UNIT
!= 0)
2959 = BITS_PER_UNIT
- ((unsigned HOST_WIDE_INT
) offseti
2961 shift_bytes_in_array_left (buf
, buflen
, amnt
);
2965 val
= native_interpret_expr (vr
->type
, buf
, buflen
);
2969 return data
->finish (0, 0, val
);
2971 /* For now handle clearing memory with partial defs. */
2972 else if (known_eq (ref
->size
, maxsize
)
2973 && integer_zerop (gimple_call_arg (def_stmt
, 1))
2974 && tree_fits_poly_int64_p (len
)
2975 && tree_to_poly_int64 (len
).is_constant (&leni
)
2976 && leni
<= INTTYPE_MAXIMUM (HOST_WIDE_INT
) / BITS_PER_UNIT
2977 && offset
.is_constant (&offseti
)
2978 && offset2
.is_constant (&offset2i
)
2979 && maxsize
.is_constant (&maxsizei
)
2980 && ranges_known_overlap_p (offseti
, maxsizei
, offset2i
,
2981 leni
<< LOG2_BITS_PER_UNIT
))
2984 pd
.rhs
= build_constructor (NULL_TREE
, NULL
);
2986 pd
.offset
= offset2i
;
2987 pd
.size
= leni
<< LOG2_BITS_PER_UNIT
;
2988 return data
->push_partial_def (pd
, 0, 0, offseti
, maxsizei
);
2992 /* 2) Assignment from an empty CONSTRUCTOR. */
2993 else if (is_gimple_reg_type (vr
->type
)
2994 && gimple_assign_single_p (def_stmt
)
2995 && gimple_assign_rhs_code (def_stmt
) == CONSTRUCTOR
2996 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt
)) == 0)
2999 poly_int64 offset2
, size2
, maxsize2
;
3000 HOST_WIDE_INT offset2i
, size2i
;
3001 gcc_assert (lhs_ref_ok
);
3002 base2
= ao_ref_base (&lhs_ref
);
3003 offset2
= lhs_ref
.offset
;
3004 size2
= lhs_ref
.size
;
3005 maxsize2
= lhs_ref
.max_size
;
3006 if (known_size_p (maxsize2
)
3007 && known_eq (maxsize2
, size2
)
3008 && adjust_offsets_for_equal_base_address (base
, &offset
,
3011 if (data
->partial_defs
.is_empty ()
3012 && known_subrange_p (offset
, maxsize
, offset2
, size2
))
3014 /* While technically undefined behavior do not optimize
3015 a full read from a clobber. */
3016 if (gimple_clobber_p (def_stmt
))
3018 tree val
= build_zero_cst (vr
->type
);
3019 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3020 ao_ref_base_alias_set (&lhs_ref
), val
);
3022 else if (known_eq (ref
->size
, maxsize
)
3023 && maxsize
.is_constant (&maxsizei
)
3024 && offset
.is_constant (&offseti
)
3025 && offset2
.is_constant (&offset2i
)
3026 && size2
.is_constant (&size2i
)
3027 && ranges_known_overlap_p (offseti
, maxsizei
,
3030 /* Let clobbers be consumed by the partial-def tracker
3031 which can choose to ignore them if they are shadowed
3034 pd
.rhs
= gimple_assign_rhs1 (def_stmt
);
3036 pd
.offset
= offset2i
;
3038 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3039 ao_ref_base_alias_set (&lhs_ref
),
3045 /* 3) Assignment from a constant. We can use folds native encode/interpret
3046 routines to extract the assigned bits. */
3047 else if (known_eq (ref
->size
, maxsize
)
3048 && is_gimple_reg_type (vr
->type
)
3049 && !reverse_storage_order_for_component_p (vr
->operands
)
3050 && !contains_storage_order_barrier_p (vr
->operands
)
3051 && gimple_assign_single_p (def_stmt
)
3053 && BITS_PER_UNIT
== 8
3054 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
3055 /* native_encode and native_decode operate on arrays of bytes
3056 and so fundamentally need a compile-time size and offset. */
3057 && maxsize
.is_constant (&maxsizei
)
3058 && offset
.is_constant (&offseti
)
3059 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
))
3060 || (TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
3061 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt
))))))
3063 tree lhs
= gimple_assign_lhs (def_stmt
);
3065 poly_int64 offset2
, size2
, maxsize2
;
3066 HOST_WIDE_INT offset2i
, size2i
;
3068 gcc_assert (lhs_ref_ok
);
3069 base2
= ao_ref_base (&lhs_ref
);
3070 offset2
= lhs_ref
.offset
;
3071 size2
= lhs_ref
.size
;
3072 maxsize2
= lhs_ref
.max_size
;
3073 reverse
= reverse_storage_order_for_component_p (lhs
);
3076 && !storage_order_barrier_p (lhs
)
3077 && known_eq (maxsize2
, size2
)
3078 && adjust_offsets_for_equal_base_address (base
, &offset
,
3080 && offset
.is_constant (&offseti
)
3081 && offset2
.is_constant (&offset2i
)
3082 && size2
.is_constant (&size2i
))
3084 if (data
->partial_defs
.is_empty ()
3085 && known_subrange_p (offseti
, maxsizei
, offset2
, size2
))
3087 /* We support up to 512-bit values (for V8DFmode). */
3088 unsigned char buffer
[65];
3091 tree rhs
= gimple_assign_rhs1 (def_stmt
);
3092 if (TREE_CODE (rhs
) == SSA_NAME
)
3093 rhs
= SSA_VAL (rhs
);
3094 len
= native_encode_expr (rhs
,
3095 buffer
, sizeof (buffer
) - 1,
3096 (offseti
- offset2i
) / BITS_PER_UNIT
);
3097 if (len
> 0 && len
* BITS_PER_UNIT
>= maxsizei
)
3099 tree type
= vr
->type
;
3100 unsigned char *buf
= buffer
;
3101 unsigned int amnt
= 0;
3102 /* Make sure to interpret in a type that has a range
3103 covering the whole access size. */
3104 if (INTEGRAL_TYPE_P (vr
->type
)
3105 && maxsizei
!= TYPE_PRECISION (vr
->type
))
3106 type
= build_nonstandard_integer_type (maxsizei
,
3107 TYPE_UNSIGNED (type
));
3108 if (BYTES_BIG_ENDIAN
)
3110 /* For big-endian native_encode_expr stored the rhs
3111 such that the LSB of it is the LSB of buffer[len - 1].
3112 That bit is stored into memory at position
3113 offset2 + size2 - 1, i.e. in byte
3114 base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3115 E.g. for offset2 1 and size2 14, rhs -1 and memory
3116 previously cleared that is:
3119 Now, if we want to extract offset 2 and size 12 from
3120 it using native_interpret_expr (which actually works
3121 for integral bitfield types in terms of byte size of
3122 the mode), the native_encode_expr stored the value
3125 and returned len 2 (the X bits are outside of
3127 Let sz be maxsize / BITS_PER_UNIT if not extracting
3128 a bitfield, and GET_MODE_SIZE otherwise.
3129 We need to align the LSB of the value we want to
3130 extract as the LSB of buf[sz - 1].
3131 The LSB from memory we need to read is at position
3132 offset + maxsize - 1. */
3133 HOST_WIDE_INT sz
= maxsizei
/ BITS_PER_UNIT
;
3134 if (INTEGRAL_TYPE_P (type
))
3135 sz
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
3136 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
+ size2i
3137 - offseti
- maxsizei
) % BITS_PER_UNIT
;
3139 shift_bytes_in_array_right (buffer
, len
, amnt
);
3140 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
+ size2i
3141 - offseti
- maxsizei
- amnt
) / BITS_PER_UNIT
;
3142 if ((unsigned HOST_WIDE_INT
) sz
+ amnt
> (unsigned) len
)
3146 buf
= buffer
+ len
- sz
- amnt
;
3147 len
-= (buf
- buffer
);
3152 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
3153 - offseti
) % BITS_PER_UNIT
;
3157 shift_bytes_in_array_left (buffer
, len
+ 1, amnt
);
3161 tree val
= native_interpret_expr (type
, buf
, len
);
3162 /* If we chop off bits because the types precision doesn't
3163 match the memory access size this is ok when optimizing
3164 reads but not when called from the DSE code during
3167 && type
!= vr
->type
)
3169 if (! int_fits_type_p (val
, vr
->type
))
3172 val
= fold_convert (vr
->type
, val
);
3176 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3177 ao_ref_base_alias_set (&lhs_ref
), val
);
3180 else if (ranges_known_overlap_p (offseti
, maxsizei
, offset2i
,
3184 tree rhs
= gimple_assign_rhs1 (def_stmt
);
3185 if (TREE_CODE (rhs
) == SSA_NAME
)
3186 rhs
= SSA_VAL (rhs
);
3189 pd
.offset
= offset2i
;
3191 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3192 ao_ref_base_alias_set (&lhs_ref
),
3198 /* 4) Assignment from an SSA name which definition we may be able
3199 to access pieces from or we can combine to a larger entity. */
3200 else if (known_eq (ref
->size
, maxsize
)
3201 && is_gimple_reg_type (vr
->type
)
3202 && !reverse_storage_order_for_component_p (vr
->operands
)
3203 && !contains_storage_order_barrier_p (vr
->operands
)
3204 && gimple_assign_single_p (def_stmt
)
3205 && TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
)
3207 tree lhs
= gimple_assign_lhs (def_stmt
);
3209 poly_int64 offset2
, size2
, maxsize2
;
3210 HOST_WIDE_INT offset2i
, size2i
, offseti
;
3212 gcc_assert (lhs_ref_ok
);
3213 base2
= ao_ref_base (&lhs_ref
);
3214 offset2
= lhs_ref
.offset
;
3215 size2
= lhs_ref
.size
;
3216 maxsize2
= lhs_ref
.max_size
;
3217 reverse
= reverse_storage_order_for_component_p (lhs
);
3218 tree def_rhs
= gimple_assign_rhs1 (def_stmt
);
3220 && !storage_order_barrier_p (lhs
)
3221 && known_size_p (maxsize2
)
3222 && known_eq (maxsize2
, size2
)
3223 && adjust_offsets_for_equal_base_address (base
, &offset
,
3226 if (data
->partial_defs
.is_empty ()
3227 && known_subrange_p (offset
, maxsize
, offset2
, size2
)
3228 /* ??? We can't handle bitfield precision extracts without
3229 either using an alternate type for the BIT_FIELD_REF and
3230 then doing a conversion or possibly adjusting the offset
3231 according to endianness. */
3232 && (! INTEGRAL_TYPE_P (vr
->type
)
3233 || known_eq (ref
->size
, TYPE_PRECISION (vr
->type
)))
3234 && multiple_p (ref
->size
, BITS_PER_UNIT
))
3236 tree val
= NULL_TREE
;
3237 if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs
))
3238 || type_has_mode_precision_p (TREE_TYPE (def_rhs
)))
3240 gimple_match_op
op (gimple_match_cond::UNCOND
,
3241 BIT_FIELD_REF
, vr
->type
,
3243 bitsize_int (ref
->size
),
3244 bitsize_int (offset
- offset2
));
3245 val
= vn_nary_build_or_lookup (&op
);
3247 else if (known_eq (ref
->size
, size2
))
3249 gimple_match_op
op (gimple_match_cond::UNCOND
,
3250 VIEW_CONVERT_EXPR
, vr
->type
,
3252 val
= vn_nary_build_or_lookup (&op
);
3255 && (TREE_CODE (val
) != SSA_NAME
3256 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
)))
3257 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3258 ao_ref_base_alias_set (&lhs_ref
), val
);
3260 else if (maxsize
.is_constant (&maxsizei
)
3261 && offset
.is_constant (&offseti
)
3262 && offset2
.is_constant (&offset2i
)
3263 && size2
.is_constant (&size2i
)
3264 && ranges_known_overlap_p (offset
, maxsize
, offset2
, size2
))
3267 pd
.rhs
= SSA_VAL (def_rhs
);
3269 pd
.offset
= offset2i
;
3271 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3272 ao_ref_base_alias_set (&lhs_ref
),
3278 /* 4b) Assignment done via one of the vectorizer internal store
3279 functions where we may be able to access pieces from or we can
3280 combine to a larger entity. */
3281 else if (known_eq (ref
->size
, maxsize
)
3282 && is_gimple_reg_type (vr
->type
)
3283 && !reverse_storage_order_for_component_p (vr
->operands
)
3284 && !contains_storage_order_barrier_p (vr
->operands
)
3285 && is_gimple_call (def_stmt
)
3286 && gimple_call_internal_p (def_stmt
)
3287 && internal_store_fn_p (gimple_call_internal_fn (def_stmt
)))
3289 gcall
*call
= as_a
<gcall
*> (def_stmt
);
3290 internal_fn fn
= gimple_call_internal_fn (call
);
3292 tree mask
= NULL_TREE
, len
= NULL_TREE
, bias
= NULL_TREE
;
3295 case IFN_MASK_STORE
:
3296 mask
= gimple_call_arg (call
, internal_fn_mask_index (fn
));
3297 mask
= vn_valueize (mask
);
3298 if (TREE_CODE (mask
) != VECTOR_CST
)
3302 len
= gimple_call_arg (call
, 2);
3303 bias
= gimple_call_arg (call
, 4);
3304 if (!tree_fits_uhwi_p (len
) || !tree_fits_shwi_p (bias
))
3310 tree def_rhs
= gimple_call_arg (call
,
3311 internal_fn_stored_value_index (fn
));
3312 def_rhs
= vn_valueize (def_rhs
);
3313 if (TREE_CODE (def_rhs
) != VECTOR_CST
)
3316 ao_ref_init_from_ptr_and_size (&lhs_ref
,
3317 vn_valueize (gimple_call_arg (call
, 0)),
3318 TYPE_SIZE_UNIT (TREE_TYPE (def_rhs
)));
3320 poly_int64 offset2
, size2
, maxsize2
;
3321 HOST_WIDE_INT offset2i
, size2i
, offseti
;
3322 base2
= ao_ref_base (&lhs_ref
);
3323 offset2
= lhs_ref
.offset
;
3324 size2
= lhs_ref
.size
;
3325 maxsize2
= lhs_ref
.max_size
;
3326 if (known_size_p (maxsize2
)
3327 && known_eq (maxsize2
, size2
)
3328 && adjust_offsets_for_equal_base_address (base
, &offset
,
3330 && maxsize
.is_constant (&maxsizei
)
3331 && offset
.is_constant (&offseti
)
3332 && offset2
.is_constant (&offset2i
)
3333 && size2
.is_constant (&size2i
))
3335 if (!ranges_maybe_overlap_p (offset
, maxsize
, offset2
, size2
))
3336 /* Poor-mans disambiguation. */
3338 else if (ranges_known_overlap_p (offset
, maxsize
, offset2
, size2
))
3342 tree aa
= gimple_call_arg (call
, 1);
3343 alias_set_type set
= get_deref_alias_set (TREE_TYPE (aa
));
3344 tree vectype
= TREE_TYPE (def_rhs
);
3345 unsigned HOST_WIDE_INT elsz
3346 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype
)));
3349 HOST_WIDE_INT start
= 0, len
= 0;
3350 unsigned mask_idx
= 0;
3353 if (integer_zerop (VECTOR_CST_ELT (mask
, mask_idx
)))
3358 pd
.offset
= offset2i
+ start
;
3360 if (ranges_known_overlap_p
3361 (offset
, maxsize
, pd
.offset
, pd
.size
))
3363 void *res
= data
->push_partial_def
3364 (pd
, set
, set
, offseti
, maxsizei
);
3369 start
= (mask_idx
+ 1) * elsz
;
3376 while (known_lt (mask_idx
, TYPE_VECTOR_SUBPARTS (vectype
)));
3380 pd
.offset
= offset2i
+ start
;
3382 if (ranges_known_overlap_p (offset
, maxsize
,
3383 pd
.offset
, pd
.size
))
3384 return data
->push_partial_def (pd
, set
, set
,
3388 else if (fn
== IFN_LEN_STORE
)
3390 pd
.offset
= offset2i
;
3391 pd
.size
= (tree_to_uhwi (len
)
3392 + -tree_to_shwi (bias
)) * BITS_PER_UNIT
;
3393 if (BYTES_BIG_ENDIAN
)
3394 pd
.rhs_off
= pd
.size
- tree_to_uhwi (TYPE_SIZE (vectype
));
3397 if (ranges_known_overlap_p (offset
, maxsize
,
3398 pd
.offset
, pd
.size
))
3399 return data
->push_partial_def (pd
, set
, set
,
3409 /* 5) For aggregate copies translate the reference through them if
3410 the copy kills ref. */
3411 else if (data
->vn_walk_kind
== VN_WALKREWRITE
3412 && gimple_assign_single_p (def_stmt
)
3413 && (DECL_P (gimple_assign_rhs1 (def_stmt
))
3414 || TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == MEM_REF
3415 || handled_component_p (gimple_assign_rhs1 (def_stmt
))))
3419 auto_vec
<vn_reference_op_s
> rhs
;
3420 vn_reference_op_t vro
;
3423 gcc_assert (lhs_ref_ok
);
3425 /* See if the assignment kills REF. */
3426 base2
= ao_ref_base (&lhs_ref
);
3427 if (!lhs_ref
.max_size_known_p ()
3429 && (TREE_CODE (base
) != MEM_REF
3430 || TREE_CODE (base2
) != MEM_REF
3431 || TREE_OPERAND (base
, 0) != TREE_OPERAND (base2
, 0)
3432 || !tree_int_cst_equal (TREE_OPERAND (base
, 1),
3433 TREE_OPERAND (base2
, 1))))
3434 || !stmt_kills_ref_p (def_stmt
, ref
))
3437 /* Find the common base of ref and the lhs. lhs_ops already
3438 contains valueized operands for the lhs. */
3439 i
= vr
->operands
.length () - 1;
3440 j
= lhs_ops
.length () - 1;
3441 while (j
>= 0 && i
>= 0
3442 && vn_reference_op_eq (&vr
->operands
[i
], &lhs_ops
[j
]))
3448 /* ??? The innermost op should always be a MEM_REF and we already
3449 checked that the assignment to the lhs kills vr. Thus for
3450 aggregate copies using char[] types the vn_reference_op_eq
3451 may fail when comparing types for compatibility. But we really
3452 don't care here - further lookups with the rewritten operands
3453 will simply fail if we messed up types too badly. */
3454 poly_int64 extra_off
= 0;
3455 if (j
== 0 && i
>= 0
3456 && lhs_ops
[0].opcode
== MEM_REF
3457 && maybe_ne (lhs_ops
[0].off
, -1))
3459 if (known_eq (lhs_ops
[0].off
, vr
->operands
[i
].off
))
3461 else if (vr
->operands
[i
].opcode
== MEM_REF
3462 && maybe_ne (vr
->operands
[i
].off
, -1))
3464 extra_off
= vr
->operands
[i
].off
- lhs_ops
[0].off
;
3469 /* i now points to the first additional op.
3470 ??? LHS may not be completely contained in VR, one or more
3471 VIEW_CONVERT_EXPRs could be in its way. We could at least
3472 try handling outermost VIEW_CONVERT_EXPRs. */
3476 /* Punt if the additional ops contain a storage order barrier. */
3477 for (k
= i
; k
>= 0; k
--)
3479 vro
= &vr
->operands
[k
];
3480 if (vro
->opcode
== VIEW_CONVERT_EXPR
&& vro
->reverse
)
3484 /* Now re-write REF to be based on the rhs of the assignment. */
3485 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
3486 copy_reference_ops_from_ref (rhs1
, &rhs
);
3488 /* Apply an extra offset to the inner MEM_REF of the RHS. */
3489 bool force_no_tbaa
= false;
3490 if (maybe_ne (extra_off
, 0))
3492 if (rhs
.length () < 2)
3494 int ix
= rhs
.length () - 2;
3495 if (rhs
[ix
].opcode
!= MEM_REF
3496 || known_eq (rhs
[ix
].off
, -1))
3498 rhs
[ix
].off
+= extra_off
;
3499 rhs
[ix
].op0
= int_const_binop (PLUS_EXPR
, rhs
[ix
].op0
,
3500 build_int_cst (TREE_TYPE (rhs
[ix
].op0
),
3502 /* When we have offsetted the RHS, reading only parts of it,
3503 we can no longer use the original TBAA type, force alias-set
3505 force_no_tbaa
= true;
3508 /* Save the operands since we need to use the original ones for
3509 the hash entry we use. */
3510 if (!data
->saved_operands
.exists ())
3511 data
->saved_operands
= vr
->operands
.copy ();
3513 /* We need to pre-pend vr->operands[0..i] to rhs. */
3514 vec
<vn_reference_op_s
> old
= vr
->operands
;
3515 if (i
+ 1 + rhs
.length () > vr
->operands
.length ())
3516 vr
->operands
.safe_grow (i
+ 1 + rhs
.length (), true);
3518 vr
->operands
.truncate (i
+ 1 + rhs
.length ());
3519 FOR_EACH_VEC_ELT (rhs
, j
, vro
)
3520 vr
->operands
[i
+ 1 + j
] = *vro
;
3521 valueize_refs (&vr
->operands
);
3522 if (old
== shared_lookup_references
)
3523 shared_lookup_references
= vr
->operands
;
3524 vr
->hashcode
= vn_reference_compute_hash (vr
);
3526 /* Try folding the new reference to a constant. */
3527 tree val
= fully_constant_vn_reference_p (vr
);
3530 if (data
->partial_defs
.is_empty ())
3531 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3532 ao_ref_base_alias_set (&lhs_ref
), val
);
3533 /* This is the only interesting case for partial-def handling
3534 coming from targets that like to gimplify init-ctors as
3535 aggregate copies from constant data like aarch64 for
3537 if (maxsize
.is_constant (&maxsizei
) && known_eq (ref
->size
, maxsize
))
3544 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3545 ao_ref_base_alias_set (&lhs_ref
),
3550 /* Continuing with partial defs isn't easily possible here, we
3551 have to find a full def from further lookups from here. Probably
3552 not worth the special-casing everywhere. */
3553 if (!data
->partial_defs
.is_empty ())
3556 /* Adjust *ref from the new operands. */
3558 ao_ref_init (&rhs1_ref
, rhs1
);
3559 if (!ao_ref_init_from_vn_reference (&r
,
3561 : ao_ref_alias_set (&rhs1_ref
),
3563 : ao_ref_base_alias_set (&rhs1_ref
),
3564 vr
->type
, vr
->operands
))
3566 /* This can happen with bitfields. */
3567 if (maybe_ne (ref
->size
, r
.size
))
3569 /* If the access lacks some subsetting simply apply that by
3570 shortening it. That in the end can only be successful
3571 if we can pun the lookup result which in turn requires
3573 if (known_eq (r
.size
, r
.max_size
)
3574 && known_lt (ref
->size
, r
.size
))
3575 r
.size
= r
.max_size
= ref
->size
;
3581 /* Do not update last seen VUSE after translating. */
3582 data
->last_vuse_ptr
= NULL
;
3583 /* Invalidate the original access path since it now contains
3585 data
->orig_ref
.ref
= NULL_TREE
;
3586 /* Use the alias-set of this LHS for recording an eventual result. */
3587 if (data
->first_set
== -2)
3589 data
->first_set
= ao_ref_alias_set (&lhs_ref
);
3590 data
->first_base_set
= ao_ref_base_alias_set (&lhs_ref
);
3593 /* Keep looking for the adjusted *REF / VR pair. */
3597 /* 6) For memcpy copies translate the reference through them if the copy
3598 kills ref. But we cannot (easily) do this translation if the memcpy is
3599 a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3600 can modify the storage order of objects (see storage_order_barrier_p). */
3601 else if (data
->vn_walk_kind
== VN_WALKREWRITE
3602 && is_gimple_reg_type (vr
->type
)
3603 /* ??? Handle BCOPY as well. */
3604 && (gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMCPY
)
3605 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMCPY_CHK
)
3606 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMPCPY
)
3607 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMPCPY_CHK
)
3608 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMMOVE
)
3609 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMMOVE_CHK
))
3610 && (TREE_CODE (gimple_call_arg (def_stmt
, 0)) == ADDR_EXPR
3611 || TREE_CODE (gimple_call_arg (def_stmt
, 0)) == SSA_NAME
)
3612 && (TREE_CODE (gimple_call_arg (def_stmt
, 1)) == ADDR_EXPR
3613 || TREE_CODE (gimple_call_arg (def_stmt
, 1)) == SSA_NAME
)
3614 && (poly_int_tree_p (gimple_call_arg (def_stmt
, 2), ©_size
)
3615 || (TREE_CODE (gimple_call_arg (def_stmt
, 2)) == SSA_NAME
3616 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt
, 2)),
3618 /* Handling this is more complicated, give up for now. */
3619 && data
->partial_defs
.is_empty ())
3623 poly_int64 rhs_offset
, lhs_offset
;
3624 vn_reference_op_s op
;
3625 poly_uint64 mem_offset
;
3626 poly_int64 at
, byte_maxsize
;
3628 /* Only handle non-variable, addressable refs. */
3629 if (maybe_ne (ref
->size
, maxsize
)
3630 || !multiple_p (offset
, BITS_PER_UNIT
, &at
)
3631 || !multiple_p (maxsize
, BITS_PER_UNIT
, &byte_maxsize
))
3634 /* Extract a pointer base and an offset for the destination. */
3635 lhs
= gimple_call_arg (def_stmt
, 0);
3637 if (TREE_CODE (lhs
) == SSA_NAME
)
3639 lhs
= vn_valueize (lhs
);
3640 if (TREE_CODE (lhs
) == SSA_NAME
)
3642 gimple
*def_stmt
= SSA_NAME_DEF_STMT (lhs
);
3643 if (gimple_assign_single_p (def_stmt
)
3644 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
3645 lhs
= gimple_assign_rhs1 (def_stmt
);
3648 if (TREE_CODE (lhs
) == ADDR_EXPR
)
3650 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs
)))
3651 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs
))))
3653 tree tem
= get_addr_base_and_unit_offset (TREE_OPERAND (lhs
, 0),
3657 if (TREE_CODE (tem
) == MEM_REF
3658 && poly_int_tree_p (TREE_OPERAND (tem
, 1), &mem_offset
))
3660 lhs
= TREE_OPERAND (tem
, 0);
3661 if (TREE_CODE (lhs
) == SSA_NAME
)
3662 lhs
= vn_valueize (lhs
);
3663 lhs_offset
+= mem_offset
;
3665 else if (DECL_P (tem
))
3666 lhs
= build_fold_addr_expr (tem
);
3670 if (TREE_CODE (lhs
) != SSA_NAME
3671 && TREE_CODE (lhs
) != ADDR_EXPR
)
3674 /* Extract a pointer base and an offset for the source. */
3675 rhs
= gimple_call_arg (def_stmt
, 1);
3677 if (TREE_CODE (rhs
) == SSA_NAME
)
3678 rhs
= vn_valueize (rhs
);
3679 if (TREE_CODE (rhs
) == ADDR_EXPR
)
3681 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs
)))
3682 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs
))))
3684 tree tem
= get_addr_base_and_unit_offset (TREE_OPERAND (rhs
, 0),
3688 if (TREE_CODE (tem
) == MEM_REF
3689 && poly_int_tree_p (TREE_OPERAND (tem
, 1), &mem_offset
))
3691 rhs
= TREE_OPERAND (tem
, 0);
3692 rhs_offset
+= mem_offset
;
3694 else if (DECL_P (tem
)
3695 || TREE_CODE (tem
) == STRING_CST
)
3696 rhs
= build_fold_addr_expr (tem
);
3700 if (TREE_CODE (rhs
) == SSA_NAME
)
3701 rhs
= SSA_VAL (rhs
);
3702 else if (TREE_CODE (rhs
) != ADDR_EXPR
)
3705 /* The bases of the destination and the references have to agree. */
3706 if (TREE_CODE (base
) == MEM_REF
)
3708 if (TREE_OPERAND (base
, 0) != lhs
3709 || !poly_int_tree_p (TREE_OPERAND (base
, 1), &mem_offset
))
3713 else if (!DECL_P (base
)
3714 || TREE_CODE (lhs
) != ADDR_EXPR
3715 || TREE_OPERAND (lhs
, 0) != base
)
3718 /* If the access is completely outside of the memcpy destination
3719 area there is no aliasing. */
3720 if (!ranges_maybe_overlap_p (lhs_offset
, copy_size
, at
, byte_maxsize
))
3722 /* And the access has to be contained within the memcpy destination. */
3723 if (!known_subrange_p (at
, byte_maxsize
, lhs_offset
, copy_size
))
3726 /* Save the operands since we need to use the original ones for
3727 the hash entry we use. */
3728 if (!data
->saved_operands
.exists ())
3729 data
->saved_operands
= vr
->operands
.copy ();
3731 /* Make room for 2 operands in the new reference. */
3732 if (vr
->operands
.length () < 2)
3734 vec
<vn_reference_op_s
> old
= vr
->operands
;
3735 vr
->operands
.safe_grow_cleared (2, true);
3736 if (old
== shared_lookup_references
)
3737 shared_lookup_references
= vr
->operands
;
3740 vr
->operands
.truncate (2);
3742 /* The looked-through reference is a simple MEM_REF. */
3743 memset (&op
, 0, sizeof (op
));
3745 op
.opcode
= MEM_REF
;
3746 op
.op0
= build_int_cst (ptr_type_node
, at
- lhs_offset
+ rhs_offset
);
3747 op
.off
= at
- lhs_offset
+ rhs_offset
;
3748 vr
->operands
[0] = op
;
3749 op
.type
= TREE_TYPE (rhs
);
3750 op
.opcode
= TREE_CODE (rhs
);
3753 vr
->operands
[1] = op
;
3754 vr
->hashcode
= vn_reference_compute_hash (vr
);
3756 /* Try folding the new reference to a constant. */
3757 tree val
= fully_constant_vn_reference_p (vr
);
3759 return data
->finish (0, 0, val
);
3761 /* Adjust *ref from the new operands. */
3762 if (!ao_ref_init_from_vn_reference (&r
, 0, 0, vr
->type
, vr
->operands
))
3764 /* This can happen with bitfields. */
3765 if (maybe_ne (ref
->size
, r
.size
))
3769 /* Do not update last seen VUSE after translating. */
3770 data
->last_vuse_ptr
= NULL
;
3771 /* Invalidate the original access path since it now contains
3773 data
->orig_ref
.ref
= NULL_TREE
;
3774 /* Use the alias-set of this stmt for recording an eventual result. */
3775 if (data
->first_set
== -2)
3777 data
->first_set
= 0;
3778 data
->first_base_set
= 0;
3781 /* Keep looking for the adjusted *REF / VR pair. */
3785 /* Bail out and stop walking. */
3789 /* Return a reference op vector from OP that can be used for
3790 vn_reference_lookup_pieces. The caller is responsible for releasing
3793 vec
<vn_reference_op_s
>
3794 vn_reference_operands_for_lookup (tree op
)
3797 return valueize_shared_reference_ops_from_ref (op
, &valueized
).copy ();
3800 /* Lookup a reference operation by it's parts, in the current hash table.
3801 Returns the resulting value number if it exists in the hash table,
3802 NULL_TREE otherwise. VNRESULT will be filled in with the actual
3803 vn_reference_t stored in the hashtable if something is found. */
3806 vn_reference_lookup_pieces (tree vuse
, alias_set_type set
,
3807 alias_set_type base_set
, tree type
,
3808 vec
<vn_reference_op_s
> operands
,
3809 vn_reference_t
*vnresult
, vn_lookup_kind kind
)
3811 struct vn_reference_s vr1
;
3819 vr1
.vuse
= vuse_ssa_val (vuse
);
3820 shared_lookup_references
.truncate (0);
3821 shared_lookup_references
.safe_grow (operands
.length (), true);
3822 memcpy (shared_lookup_references
.address (),
3823 operands
.address (),
3824 sizeof (vn_reference_op_s
)
3825 * operands
.length ());
3827 valueize_refs_1 (&shared_lookup_references
, &valueized_p
);
3828 vr1
.operands
= shared_lookup_references
;
3831 vr1
.base_set
= base_set
;
3832 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
3833 if ((cst
= fully_constant_vn_reference_p (&vr1
)))
3836 vn_reference_lookup_1 (&vr1
, vnresult
);
3838 && kind
!= VN_NOWALK
3842 unsigned limit
= param_sccvn_max_alias_queries_per_access
;
3843 vn_walk_cb_data
data (&vr1
, NULL_TREE
, NULL
, kind
, true, NULL_TREE
,
3845 vec
<vn_reference_op_s
> ops_for_ref
;
3847 ops_for_ref
= vr1
.operands
;
3850 /* For ao_ref_from_mem we have to ensure only available SSA names
3851 end up in base and the only convenient way to make this work
3852 for PRE is to re-valueize with that in mind. */
3853 ops_for_ref
.create (operands
.length ());
3854 ops_for_ref
.quick_grow (operands
.length ());
3855 memcpy (ops_for_ref
.address (),
3856 operands
.address (),
3857 sizeof (vn_reference_op_s
)
3858 * operands
.length ());
3859 valueize_refs_1 (&ops_for_ref
, &valueized_p
, true);
3861 if (ao_ref_init_from_vn_reference (&r
, set
, base_set
, type
,
3865 walk_non_aliased_vuses (&r
, vr1
.vuse
, true, vn_reference_lookup_2
,
3866 vn_reference_lookup_3
, vuse_valueize
,
3868 if (ops_for_ref
!= shared_lookup_references
)
3869 ops_for_ref
.release ();
3870 gcc_checking_assert (vr1
.operands
== shared_lookup_references
);
3873 && (!(*vnresult
)->result
3874 || !operand_equal_p ((*vnresult
)->result
, data
.same_val
)))
3882 return (*vnresult
)->result
;
3887 /* Lookup OP in the current hash table, and return the resulting value
3888 number if it exists in the hash table. Return NULL_TREE if it does
3889 not exist in the hash table or if the result field of the structure
3890 was NULL.. VNRESULT will be filled in with the vn_reference_t
3891 stored in the hashtable if one exists. When TBAA_P is false assume
3892 we are looking up a store and treat it as having alias-set zero.
3893 *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
3894 MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
3895 load is bitwise anded with MASK and so we are only interested in a subset
3896 of the bits and can ignore if the other bits are uninitialized or
3897 not initialized with constants. When doing redundant store removal
3898 the caller has to set REDUNDANT_STORE_REMOVAL_P. */
3901 vn_reference_lookup (tree op
, tree vuse
, vn_lookup_kind kind
,
3902 vn_reference_t
*vnresult
, bool tbaa_p
,
3903 tree
*last_vuse_ptr
, tree mask
,
3904 bool redundant_store_removal_p
)
3906 vec
<vn_reference_op_s
> operands
;
3907 struct vn_reference_s vr1
;
3908 bool valueized_anything
;
3913 vr1
.vuse
= vuse_ssa_val (vuse
);
3914 vr1
.operands
= operands
3915 = valueize_shared_reference_ops_from_ref (op
, &valueized_anything
);
3917 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
3918 this before the pass folding __builtin_object_size had a chance to run. */
3919 if ((cfun
->curr_properties
& PROP_objsz
)
3920 && operands
[0].opcode
== ADDR_EXPR
3921 && operands
.last ().opcode
== SSA_NAME
)
3924 vn_reference_op_t vro
;
3926 for (i
= 1; operands
.iterate (i
, &vro
); ++i
)
3928 if (vro
->opcode
== SSA_NAME
)
3930 else if (known_eq (vro
->off
, -1))
3934 if (i
== operands
.length () - 1
3935 /* Make sure we the offset we accumulated in a 64bit int
3936 fits the address computation carried out in target
3937 offset precision. */
3939 == sext_hwi (off
.coeffs
[0], TYPE_PRECISION (sizetype
))))
3941 gcc_assert (operands
[i
-1].opcode
== MEM_REF
);
3943 ops
[0] = operands
[i
].op0
;
3944 ops
[1] = wide_int_to_tree (sizetype
, off
);
3945 tree res
= vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR
,
3946 TREE_TYPE (op
), ops
, NULL
);
3953 vr1
.type
= TREE_TYPE (op
);
3955 ao_ref_init (&op_ref
, op
);
3956 vr1
.set
= ao_ref_alias_set (&op_ref
);
3957 vr1
.base_set
= ao_ref_base_alias_set (&op_ref
);
3958 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
3959 if (mask
== NULL_TREE
)
3960 if (tree cst
= fully_constant_vn_reference_p (&vr1
))
3963 if (kind
!= VN_NOWALK
&& vr1
.vuse
)
3965 vn_reference_t wvnresult
;
3967 unsigned limit
= param_sccvn_max_alias_queries_per_access
;
3968 auto_vec
<vn_reference_op_s
> ops_for_ref
;
3969 if (valueized_anything
)
3971 copy_reference_ops_from_ref (op
, &ops_for_ref
);
3973 valueize_refs_1 (&ops_for_ref
, &tem
, true);
3975 /* Make sure to use a valueized reference if we valueized anything.
3976 Otherwise preserve the full reference for advanced TBAA. */
3977 if (!valueized_anything
3978 || !ao_ref_init_from_vn_reference (&r
, vr1
.set
, vr1
.base_set
,
3979 vr1
.type
, ops_for_ref
))
3980 ao_ref_init (&r
, op
);
3981 vn_walk_cb_data
data (&vr1
, r
.ref
? NULL_TREE
: op
,
3982 last_vuse_ptr
, kind
, tbaa_p
, mask
,
3983 redundant_store_removal_p
);
3987 walk_non_aliased_vuses (&r
, vr1
.vuse
, tbaa_p
, vn_reference_lookup_2
,
3988 vn_reference_lookup_3
, vuse_valueize
, limit
,
3990 gcc_checking_assert (vr1
.operands
== shared_lookup_references
);
3993 gcc_assert (mask
== NULL_TREE
);
3995 && (!wvnresult
->result
3996 || !operand_equal_p (wvnresult
->result
, data
.same_val
)))
3999 *vnresult
= wvnresult
;
4000 return wvnresult
->result
;
4003 return data
.masked_result
;
4009 *last_vuse_ptr
= vr1
.vuse
;
4012 return vn_reference_lookup_1 (&vr1
, vnresult
);
4015 /* Lookup CALL in the current hash table and return the entry in
4016 *VNRESULT if found. Populates *VR for the hashtable lookup. */
4019 vn_reference_lookup_call (gcall
*call
, vn_reference_t
*vnresult
,
4025 tree vuse
= gimple_vuse (call
);
4027 vr
->vuse
= vuse
? SSA_VAL (vuse
) : NULL_TREE
;
4028 vr
->operands
= valueize_shared_reference_ops_from_call (call
);
4029 tree lhs
= gimple_call_lhs (call
);
4030 /* For non-SSA return values the referece ops contain the LHS. */
4031 vr
->type
= ((lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
4032 ? TREE_TYPE (lhs
) : NULL_TREE
);
4036 vr
->hashcode
= vn_reference_compute_hash (vr
);
4037 vn_reference_lookup_1 (vr
, vnresult
);
4040 /* Insert OP into the current hash table with a value number of RESULT. */
4043 vn_reference_insert (tree op
, tree result
, tree vuse
, tree vdef
)
4045 vn_reference_s
**slot
;
4049 vec
<vn_reference_op_s
> operands
4050 = valueize_shared_reference_ops_from_ref (op
, &tem
);
4051 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4052 before the pass folding __builtin_object_size had a chance to run. */
4053 if ((cfun
->curr_properties
& PROP_objsz
)
4054 && operands
[0].opcode
== ADDR_EXPR
4055 && operands
.last ().opcode
== SSA_NAME
)
4058 vn_reference_op_t vro
;
4060 for (i
= 1; operands
.iterate (i
, &vro
); ++i
)
4062 if (vro
->opcode
== SSA_NAME
)
4064 else if (known_eq (vro
->off
, -1))
4068 if (i
== operands
.length () - 1
4069 /* Make sure we the offset we accumulated in a 64bit int
4070 fits the address computation carried out in target
4071 offset precision. */
4073 == sext_hwi (off
.coeffs
[0], TYPE_PRECISION (sizetype
))))
4075 gcc_assert (operands
[i
-1].opcode
== MEM_REF
);
4077 ops
[0] = operands
[i
].op0
;
4078 ops
[1] = wide_int_to_tree (sizetype
, off
);
4079 vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR
,
4080 TREE_TYPE (op
), ops
, result
,
4081 VN_INFO (result
)->value_id
);
4086 vr1
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
4087 if (TREE_CODE (result
) == SSA_NAME
)
4088 vr1
->value_id
= VN_INFO (result
)->value_id
;
4090 vr1
->value_id
= get_or_alloc_constant_value_id (result
);
4091 vr1
->vuse
= vuse_ssa_val (vuse
);
4092 vr1
->operands
= operands
.copy ();
4093 vr1
->type
= TREE_TYPE (op
);
4094 vr1
->punned
= false;
4096 ao_ref_init (&op_ref
, op
);
4097 vr1
->set
= ao_ref_alias_set (&op_ref
);
4098 vr1
->base_set
= ao_ref_base_alias_set (&op_ref
);
4099 vr1
->hashcode
= vn_reference_compute_hash (vr1
);
4100 vr1
->result
= TREE_CODE (result
) == SSA_NAME
? SSA_VAL (result
) : result
;
4101 vr1
->result_vdef
= vdef
;
4103 slot
= valid_info
->references
->find_slot_with_hash (vr1
, vr1
->hashcode
,
4106 /* Because IL walking on reference lookup can end up visiting
4107 a def that is only to be visited later in iteration order
4108 when we are about to make an irreducible region reducible
4109 the def can be effectively processed and its ref being inserted
4110 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4111 but save a lookup if we deal with already inserted refs here. */
4114 /* We cannot assert that we have the same value either because
4115 when disentangling an irreducible region we may end up visiting
4116 a use before the corresponding def. That's a missed optimization
4117 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4118 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
4119 && !operand_equal_p ((*slot
)->result
, vr1
->result
, 0))
4121 fprintf (dump_file
, "Keeping old value ");
4122 print_generic_expr (dump_file
, (*slot
)->result
);
4123 fprintf (dump_file
, " because of collision\n");
4125 free_reference (vr1
);
4126 obstack_free (&vn_tables_obstack
, vr1
);
4131 vr1
->next
= last_inserted_ref
;
4132 last_inserted_ref
= vr1
;
4135 /* Insert a reference by it's pieces into the current hash table with
4136 a value number of RESULT. Return the resulting reference
4137 structure we created. */
4140 vn_reference_insert_pieces (tree vuse
, alias_set_type set
,
4141 alias_set_type base_set
, tree type
,
4142 vec
<vn_reference_op_s
> operands
,
4143 tree result
, unsigned int value_id
)
4146 vn_reference_s
**slot
;
4149 vr1
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
4150 vr1
->value_id
= value_id
;
4151 vr1
->vuse
= vuse_ssa_val (vuse
);
4152 vr1
->operands
= operands
;
4153 valueize_refs (&vr1
->operands
);
4155 vr1
->punned
= false;
4157 vr1
->base_set
= base_set
;
4158 vr1
->hashcode
= vn_reference_compute_hash (vr1
);
4159 if (result
&& TREE_CODE (result
) == SSA_NAME
)
4160 result
= SSA_VAL (result
);
4161 vr1
->result
= result
;
4162 vr1
->result_vdef
= NULL_TREE
;
4164 slot
= valid_info
->references
->find_slot_with_hash (vr1
, vr1
->hashcode
,
4167 /* At this point we should have all the things inserted that we have
4168 seen before, and we should never try inserting something that
4170 gcc_assert (!*slot
);
4173 vr1
->next
= last_inserted_ref
;
4174 last_inserted_ref
= vr1
;
4178 /* Compute and return the hash value for nary operation VBO1. */
4181 vn_nary_op_compute_hash (const vn_nary_op_t vno1
)
4183 inchash::hash hstate
;
4186 if (((vno1
->length
== 2
4187 && commutative_tree_code (vno1
->opcode
))
4188 || (vno1
->length
== 3
4189 && commutative_ternary_tree_code (vno1
->opcode
)))
4190 && tree_swap_operands_p (vno1
->op
[0], vno1
->op
[1]))
4191 std::swap (vno1
->op
[0], vno1
->op
[1]);
4192 else if (TREE_CODE_CLASS (vno1
->opcode
) == tcc_comparison
4193 && tree_swap_operands_p (vno1
->op
[0], vno1
->op
[1]))
4195 std::swap (vno1
->op
[0], vno1
->op
[1]);
4196 vno1
->opcode
= swap_tree_comparison (vno1
->opcode
);
4199 hstate
.add_int (vno1
->opcode
);
4200 for (i
= 0; i
< vno1
->length
; ++i
)
4201 inchash::add_expr (vno1
->op
[i
], hstate
);
4203 return hstate
.end ();
4206 /* Compare nary operations VNO1 and VNO2 and return true if they are
4210 vn_nary_op_eq (const_vn_nary_op_t
const vno1
, const_vn_nary_op_t
const vno2
)
4214 if (vno1
->hashcode
!= vno2
->hashcode
)
4217 if (vno1
->length
!= vno2
->length
)
4220 if (vno1
->opcode
!= vno2
->opcode
4221 || !types_compatible_p (vno1
->type
, vno2
->type
))
4224 for (i
= 0; i
< vno1
->length
; ++i
)
4225 if (!expressions_equal_p (vno1
->op
[i
], vno2
->op
[i
]))
4228 /* BIT_INSERT_EXPR has an implict operand as the type precision
4229 of op1. Need to check to make sure they are the same. */
4230 if (vno1
->opcode
== BIT_INSERT_EXPR
4231 && TREE_CODE (vno1
->op
[1]) == INTEGER_CST
4232 && TYPE_PRECISION (TREE_TYPE (vno1
->op
[1]))
4233 != TYPE_PRECISION (TREE_TYPE (vno2
->op
[1])))
4239 /* Initialize VNO from the pieces provided. */
4242 init_vn_nary_op_from_pieces (vn_nary_op_t vno
, unsigned int length
,
4243 enum tree_code code
, tree type
, tree
*ops
)
4246 vno
->length
= length
;
4248 memcpy (&vno
->op
[0], ops
, sizeof (tree
) * length
);
4251 /* Return the number of operands for a vn_nary ops structure from STMT. */
4254 vn_nary_length_from_stmt (gimple
*stmt
)
4256 switch (gimple_assign_rhs_code (stmt
))
4260 case VIEW_CONVERT_EXPR
:
4267 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt
));
4270 return gimple_num_ops (stmt
) - 1;
4274 /* Initialize VNO from STMT. */
4277 init_vn_nary_op_from_stmt (vn_nary_op_t vno
, gassign
*stmt
)
4281 vno
->opcode
= gimple_assign_rhs_code (stmt
);
4282 vno
->type
= TREE_TYPE (gimple_assign_lhs (stmt
));
4283 switch (vno
->opcode
)
4287 case VIEW_CONVERT_EXPR
:
4289 vno
->op
[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
4294 vno
->op
[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
4295 vno
->op
[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1);
4296 vno
->op
[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2);
4300 vno
->length
= CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt
));
4301 for (i
= 0; i
< vno
->length
; ++i
)
4302 vno
->op
[i
] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt
), i
)->value
;
4306 gcc_checking_assert (!gimple_assign_single_p (stmt
));
4307 vno
->length
= gimple_num_ops (stmt
) - 1;
4308 for (i
= 0; i
< vno
->length
; ++i
)
4309 vno
->op
[i
] = gimple_op (stmt
, i
+ 1);
4313 /* Compute the hashcode for VNO and look for it in the hash table;
4314 return the resulting value number if it exists in the hash table.
4315 Return NULL_TREE if it does not exist in the hash table or if the
4316 result field of the operation is NULL. VNRESULT will contain the
4317 vn_nary_op_t from the hashtable if it exists. */
4320 vn_nary_op_lookup_1 (vn_nary_op_t vno
, vn_nary_op_t
*vnresult
)
4322 vn_nary_op_s
**slot
;
4327 for (unsigned i
= 0; i
< vno
->length
; ++i
)
4328 if (TREE_CODE (vno
->op
[i
]) == SSA_NAME
)
4329 vno
->op
[i
] = SSA_VAL (vno
->op
[i
]);
4331 vno
->hashcode
= vn_nary_op_compute_hash (vno
);
4332 slot
= valid_info
->nary
->find_slot_with_hash (vno
, vno
->hashcode
, NO_INSERT
);
4337 return (*slot
)->predicated_values
? NULL_TREE
: (*slot
)->u
.result
;
4340 /* Lookup a n-ary operation by its pieces and return the resulting value
4341 number if it exists in the hash table. Return NULL_TREE if it does
4342 not exist in the hash table or if the result field of the operation
4343 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4347 vn_nary_op_lookup_pieces (unsigned int length
, enum tree_code code
,
4348 tree type
, tree
*ops
, vn_nary_op_t
*vnresult
)
4350 vn_nary_op_t vno1
= XALLOCAVAR (struct vn_nary_op_s
,
4351 sizeof_vn_nary_op (length
));
4352 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4353 return vn_nary_op_lookup_1 (vno1
, vnresult
);
4356 /* Lookup the rhs of STMT in the current hash table, and return the resulting
4357 value number if it exists in the hash table. Return NULL_TREE if
4358 it does not exist in the hash table. VNRESULT will contain the
4359 vn_nary_op_t from the hashtable if it exists. */
4362 vn_nary_op_lookup_stmt (gimple
*stmt
, vn_nary_op_t
*vnresult
)
4365 = XALLOCAVAR (struct vn_nary_op_s
,
4366 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt
)));
4367 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (stmt
));
4368 return vn_nary_op_lookup_1 (vno1
, vnresult
);
4371 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4374 alloc_vn_nary_op_noinit (unsigned int length
, struct obstack
*stack
)
4376 return (vn_nary_op_t
) obstack_alloc (stack
, sizeof_vn_nary_op (length
));
4379 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4383 alloc_vn_nary_op (unsigned int length
, tree result
, unsigned int value_id
)
4385 vn_nary_op_t vno1
= alloc_vn_nary_op_noinit (length
, &vn_tables_obstack
);
4387 vno1
->value_id
= value_id
;
4388 vno1
->length
= length
;
4389 vno1
->predicated_values
= 0;
4390 vno1
->u
.result
= result
;
4395 /* Insert VNO into TABLE. */
4398 vn_nary_op_insert_into (vn_nary_op_t vno
, vn_nary_op_table_type
*table
)
4400 vn_nary_op_s
**slot
;
4402 gcc_assert (! vno
->predicated_values
4403 || (! vno
->u
.values
->next
4404 && vno
->u
.values
->n
== 1));
4406 for (unsigned i
= 0; i
< vno
->length
; ++i
)
4407 if (TREE_CODE (vno
->op
[i
]) == SSA_NAME
)
4408 vno
->op
[i
] = SSA_VAL (vno
->op
[i
]);
4410 vno
->hashcode
= vn_nary_op_compute_hash (vno
);
4411 slot
= table
->find_slot_with_hash (vno
, vno
->hashcode
, INSERT
);
4412 vno
->unwind_to
= *slot
;
4415 /* Prefer non-predicated values.
4416 ??? Only if those are constant, otherwise, with constant predicated
4417 value, turn them into predicated values with entry-block validity
4418 (??? but we always find the first valid result currently). */
4419 if ((*slot
)->predicated_values
4420 && ! vno
->predicated_values
)
4422 /* ??? We cannot remove *slot from the unwind stack list.
4423 For the moment we deal with this by skipping not found
4424 entries but this isn't ideal ... */
4426 /* ??? Maintain a stack of states we can unwind in
4427 vn_nary_op_s? But how far do we unwind? In reality
4428 we need to push change records somewhere... Or not
4429 unwind vn_nary_op_s and linking them but instead
4430 unwind the results "list", linking that, which also
4431 doesn't move on hashtable resize. */
4432 /* We can also have a ->unwind_to recording *slot there.
4433 That way we can make u.values a fixed size array with
4434 recording the number of entries but of course we then
4435 have always N copies for each unwind_to-state. Or we
4436 make sure to only ever append and each unwinding will
4437 pop off one entry (but how to deal with predicated
4438 replaced with non-predicated here?) */
4439 vno
->next
= last_inserted_nary
;
4440 last_inserted_nary
= vno
;
4443 else if (vno
->predicated_values
4444 && ! (*slot
)->predicated_values
)
4446 else if (vno
->predicated_values
4447 && (*slot
)->predicated_values
)
4449 /* ??? Factor this all into a insert_single_predicated_value
4451 gcc_assert (!vno
->u
.values
->next
&& vno
->u
.values
->n
== 1);
4453 = BASIC_BLOCK_FOR_FN (cfun
, vno
->u
.values
->valid_dominated_by_p
[0]);
4454 vn_pval
*nval
= vno
->u
.values
;
4455 vn_pval
**next
= &vno
->u
.values
;
4457 for (vn_pval
*val
= (*slot
)->u
.values
; val
; val
= val
->next
)
4459 if (expressions_equal_p (val
->result
, nval
->result
))
4462 for (unsigned i
= 0; i
< val
->n
; ++i
)
4465 = BASIC_BLOCK_FOR_FN (cfun
,
4466 val
->valid_dominated_by_p
[i
]);
4467 if (dominated_by_p (CDI_DOMINATORS
, vno_bb
, val_bb
))
4468 /* Value registered with more generic predicate. */
4470 else if (flag_checking
)
4471 /* Shouldn't happen, we insert in RPO order. */
4472 gcc_assert (!dominated_by_p (CDI_DOMINATORS
,
4476 *next
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4478 + val
->n
* sizeof (int));
4479 (*next
)->next
= NULL
;
4480 (*next
)->result
= val
->result
;
4481 (*next
)->n
= val
->n
+ 1;
4482 memcpy ((*next
)->valid_dominated_by_p
,
4483 val
->valid_dominated_by_p
,
4484 val
->n
* sizeof (int));
4485 (*next
)->valid_dominated_by_p
[val
->n
] = vno_bb
->index
;
4486 next
= &(*next
)->next
;
4487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4488 fprintf (dump_file
, "Appending predicate to value.\n");
4491 /* Copy other predicated values. */
4492 *next
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4494 + (val
->n
-1) * sizeof (int));
4495 memcpy (*next
, val
, sizeof (vn_pval
) + (val
->n
-1) * sizeof (int));
4496 (*next
)->next
= NULL
;
4497 next
= &(*next
)->next
;
4503 vno
->next
= last_inserted_nary
;
4504 last_inserted_nary
= vno
;
4508 /* While we do not want to insert things twice it's awkward to
4509 avoid it in the case where visit_nary_op pattern-matches stuff
4510 and ends up simplifying the replacement to itself. We then
4511 get two inserts, one from visit_nary_op and one from
4512 vn_nary_build_or_lookup.
4513 So allow inserts with the same value number. */
4514 if ((*slot
)->u
.result
== vno
->u
.result
)
4518 /* ??? There's also optimistic vs. previous commited state merging
4519 that is problematic for the case of unwinding. */
4521 /* ??? We should return NULL if we do not use 'vno' and have the
4522 caller release it. */
4523 gcc_assert (!*slot
);
4526 vno
->next
= last_inserted_nary
;
4527 last_inserted_nary
= vno
;
4531 /* Insert a n-ary operation into the current hash table using it's
4532 pieces. Return the vn_nary_op_t structure we created and put in
4536 vn_nary_op_insert_pieces (unsigned int length
, enum tree_code code
,
4537 tree type
, tree
*ops
,
4538 tree result
, unsigned int value_id
)
4540 vn_nary_op_t vno1
= alloc_vn_nary_op (length
, result
, value_id
);
4541 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4542 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4545 /* Return whether we can track a predicate valid when PRED_E is executed. */
4548 can_track_predicate_on_edge (edge pred_e
)
4550 /* ??? As we are currently recording the destination basic-block index in
4551 vn_pval.valid_dominated_by_p and using dominance for the
4552 validity check we cannot track predicates on all edges. */
4553 if (single_pred_p (pred_e
->dest
))
4555 /* Never record for backedges. */
4556 if (pred_e
->flags
& EDGE_DFS_BACK
)
4558 /* When there's more than one predecessor we cannot track
4559 predicate validity based on the destination block. The
4560 exception is when all other incoming edges sources are
4561 dominated by the destination block. */
4564 FOR_EACH_EDGE (e
, ei
, pred_e
->dest
->preds
)
4565 if (e
!= pred_e
&& ! dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
4571 vn_nary_op_insert_pieces_predicated (unsigned int length
, enum tree_code code
,
4572 tree type
, tree
*ops
,
4573 tree result
, unsigned int value_id
,
4576 gcc_assert (can_track_predicate_on_edge (pred_e
));
4578 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
4579 /* ??? Fix dumping, but currently we only get comparisons. */
4580 && TREE_CODE_CLASS (code
) == tcc_comparison
)
4582 fprintf (dump_file
, "Recording on edge %d->%d ", pred_e
->src
->index
,
4583 pred_e
->dest
->index
);
4584 print_generic_expr (dump_file
, ops
[0], TDF_SLIM
);
4585 fprintf (dump_file
, " %s ", get_tree_code_name (code
));
4586 print_generic_expr (dump_file
, ops
[1], TDF_SLIM
);
4587 fprintf (dump_file
, " == %s\n",
4588 integer_zerop (result
) ? "false" : "true");
4590 vn_nary_op_t vno1
= alloc_vn_nary_op (length
, NULL_TREE
, value_id
);
4591 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4592 vno1
->predicated_values
= 1;
4593 vno1
->u
.values
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4595 vno1
->u
.values
->next
= NULL
;
4596 vno1
->u
.values
->result
= result
;
4597 vno1
->u
.values
->n
= 1;
4598 vno1
->u
.values
->valid_dominated_by_p
[0] = pred_e
->dest
->index
;
4599 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4603 dominated_by_p_w_unex (basic_block bb1
, basic_block bb2
, bool);
4606 vn_nary_op_get_predicated_value (vn_nary_op_t vno
, basic_block bb
,
4609 if (! vno
->predicated_values
)
4610 return vno
->u
.result
;
4611 for (vn_pval
*val
= vno
->u
.values
; val
; val
= val
->next
)
4612 for (unsigned i
= 0; i
< val
->n
; ++i
)
4615 = BASIC_BLOCK_FOR_FN (cfun
, val
->valid_dominated_by_p
[i
]);
4616 /* Do not handle backedge executability optimistically since
4617 when figuring out whether to iterate we do not consider
4618 changed predication.
4619 When asking for predicated values on an edge avoid looking
4620 at edge executability for edges forward in our iteration
4622 if (e
&& (e
->flags
& EDGE_DFS_BACK
))
4624 if (dominated_by_p (CDI_DOMINATORS
, bb
, cand
))
4627 else if (dominated_by_p_w_unex (bb
, cand
, false))
4634 vn_nary_op_get_predicated_value (vn_nary_op_t vno
, edge e
)
4636 return vn_nary_op_get_predicated_value (vno
, e
->src
, e
);
4639 /* Insert the rhs of STMT into the current hash table with a value number of
4643 vn_nary_op_insert_stmt (gimple
*stmt
, tree result
)
4646 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt
),
4647 result
, VN_INFO (result
)->value_id
);
4648 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (stmt
));
4649 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4652 /* Compute a hashcode for PHI operation VP1 and return it. */
4654 static inline hashval_t
4655 vn_phi_compute_hash (vn_phi_t vp1
)
4657 inchash::hash hstate
;
4663 hstate
.add_int (EDGE_COUNT (vp1
->block
->preds
));
4664 switch (EDGE_COUNT (vp1
->block
->preds
))
4669 /* When this is a PHI node subject to CSE for different blocks
4670 avoid hashing the block index. */
4675 hstate
.add_int (vp1
->block
->index
);
4678 /* If all PHI arguments are constants we need to distinguish
4679 the PHI node via its type. */
4681 hstate
.merge_hash (vn_hash_type (type
));
4683 FOR_EACH_EDGE (e
, ei
, vp1
->block
->preds
)
4685 /* Don't hash backedge values they need to be handled as VN_TOP
4686 for optimistic value-numbering. */
4687 if (e
->flags
& EDGE_DFS_BACK
)
4690 phi1op
= vp1
->phiargs
[e
->dest_idx
];
4691 if (phi1op
== VN_TOP
)
4693 inchash::add_expr (phi1op
, hstate
);
4696 return hstate
.end ();
4700 /* Return true if COND1 and COND2 represent the same condition, set
4701 *INVERTED_P if one needs to be inverted to make it the same as
4705 cond_stmts_equal_p (gcond
*cond1
, tree lhs1
, tree rhs1
,
4706 gcond
*cond2
, tree lhs2
, tree rhs2
, bool *inverted_p
)
4708 enum tree_code code1
= gimple_cond_code (cond1
);
4709 enum tree_code code2
= gimple_cond_code (cond2
);
4711 *inverted_p
= false;
4714 else if (code1
== swap_tree_comparison (code2
))
4715 std::swap (lhs2
, rhs2
);
4716 else if (code1
== invert_tree_comparison (code2
, HONOR_NANS (lhs2
)))
4718 else if (code1
== invert_tree_comparison
4719 (swap_tree_comparison (code2
), HONOR_NANS (lhs2
)))
4721 std::swap (lhs2
, rhs2
);
4727 return ((expressions_equal_p (lhs1
, lhs2
)
4728 && expressions_equal_p (rhs1
, rhs2
))
4729 || (commutative_tree_code (code1
)
4730 && expressions_equal_p (lhs1
, rhs2
)
4731 && expressions_equal_p (rhs1
, lhs2
)));
4734 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4737 vn_phi_eq (const_vn_phi_t
const vp1
, const_vn_phi_t
const vp2
)
4739 if (vp1
->hashcode
!= vp2
->hashcode
)
4742 if (vp1
->block
!= vp2
->block
)
4744 if (EDGE_COUNT (vp1
->block
->preds
) != EDGE_COUNT (vp2
->block
->preds
))
4747 switch (EDGE_COUNT (vp1
->block
->preds
))
4750 /* Single-arg PHIs are just copies. */
4755 /* Make sure both PHIs are classified as CSEable. */
4756 if (! vp1
->cclhs
|| ! vp2
->cclhs
)
4759 /* Rule out backedges into the PHI. */
4761 (vp1
->block
->loop_father
->header
!= vp1
->block
4762 && vp2
->block
->loop_father
->header
!= vp2
->block
);
4764 /* If the PHI nodes do not have compatible types
4765 they are not the same. */
4766 if (!types_compatible_p (vp1
->type
, vp2
->type
))
4769 /* If the immediate dominator end in switch stmts multiple
4770 values may end up in the same PHI arg via intermediate
4773 = get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4775 = get_immediate_dominator (CDI_DOMINATORS
, vp2
->block
);
4776 gcc_checking_assert (EDGE_COUNT (idom1
->succs
) == 2
4777 && EDGE_COUNT (idom2
->succs
) == 2);
4779 /* Verify the controlling stmt is the same. */
4780 gcond
*last1
= as_a
<gcond
*> (*gsi_last_bb (idom1
));
4781 gcond
*last2
= as_a
<gcond
*> (*gsi_last_bb (idom2
));
4783 if (! cond_stmts_equal_p (last1
, vp1
->cclhs
, vp1
->ccrhs
,
4784 last2
, vp2
->cclhs
, vp2
->ccrhs
,
4788 /* Get at true/false controlled edges into the PHI. */
4789 edge te1
, te2
, fe1
, fe2
;
4790 if (! extract_true_false_controlled_edges (idom1
, vp1
->block
,
4792 || ! extract_true_false_controlled_edges (idom2
, vp2
->block
,
4796 /* Swap edges if the second condition is the inverted of the
4799 std::swap (te2
, fe2
);
4801 /* Since we do not know which edge will be executed we have
4802 to be careful when matching VN_TOP. Be conservative and
4803 only match VN_TOP == VN_TOP for now, we could allow
4804 VN_TOP on the not prevailing PHI though. See for example
4806 if (! expressions_equal_p (vp1
->phiargs
[te1
->dest_idx
],
4807 vp2
->phiargs
[te2
->dest_idx
], false)
4808 || ! expressions_equal_p (vp1
->phiargs
[fe1
->dest_idx
],
4809 vp2
->phiargs
[fe2
->dest_idx
], false))
4820 /* If the PHI nodes do not have compatible types
4821 they are not the same. */
4822 if (!types_compatible_p (vp1
->type
, vp2
->type
))
4825 /* Any phi in the same block will have it's arguments in the
4826 same edge order, because of how we store phi nodes. */
4827 unsigned nargs
= EDGE_COUNT (vp1
->block
->preds
);
4828 for (unsigned i
= 0; i
< nargs
; ++i
)
4830 tree phi1op
= vp1
->phiargs
[i
];
4831 tree phi2op
= vp2
->phiargs
[i
];
4832 if (phi1op
== phi2op
)
4834 if (!expressions_equal_p (phi1op
, phi2op
, false))
4841 /* Lookup PHI in the current hash table, and return the resulting
4842 value number if it exists in the hash table. Return NULL_TREE if
4843 it does not exist in the hash table. */
4846 vn_phi_lookup (gimple
*phi
, bool backedges_varying_p
)
4849 struct vn_phi_s
*vp1
;
4853 vp1
= XALLOCAVAR (struct vn_phi_s
,
4854 sizeof (struct vn_phi_s
)
4855 + (gimple_phi_num_args (phi
) - 1) * sizeof (tree
));
4857 /* Canonicalize the SSA_NAME's to their value number. */
4858 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
4860 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4861 if (TREE_CODE (def
) == SSA_NAME
4862 && (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
)))
4864 if (!virtual_operand_p (def
)
4865 && ssa_undefined_value_p (def
, false))
4868 def
= SSA_VAL (def
);
4870 vp1
->phiargs
[e
->dest_idx
] = def
;
4872 vp1
->type
= TREE_TYPE (gimple_phi_result (phi
));
4873 vp1
->block
= gimple_bb (phi
);
4874 /* Extract values of the controlling condition. */
4875 vp1
->cclhs
= NULL_TREE
;
4876 vp1
->ccrhs
= NULL_TREE
;
4877 if (EDGE_COUNT (vp1
->block
->preds
) == 2
4878 && vp1
->block
->loop_father
->header
!= vp1
->block
)
4880 basic_block idom1
= get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4881 if (EDGE_COUNT (idom1
->succs
) == 2)
4882 if (gcond
*last1
= safe_dyn_cast
<gcond
*> (*gsi_last_bb (idom1
)))
4884 /* ??? We want to use SSA_VAL here. But possibly not
4886 vp1
->cclhs
= vn_valueize (gimple_cond_lhs (last1
));
4887 vp1
->ccrhs
= vn_valueize (gimple_cond_rhs (last1
));
4890 vp1
->hashcode
= vn_phi_compute_hash (vp1
);
4891 slot
= valid_info
->phis
->find_slot_with_hash (vp1
, vp1
->hashcode
, NO_INSERT
);
4894 return (*slot
)->result
;
4897 /* Insert PHI into the current hash table with a value number of
4901 vn_phi_insert (gimple
*phi
, tree result
, bool backedges_varying_p
)
4904 vn_phi_t vp1
= (vn_phi_t
) obstack_alloc (&vn_tables_obstack
,
4906 + ((gimple_phi_num_args (phi
) - 1)
4911 /* Canonicalize the SSA_NAME's to their value number. */
4912 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
4914 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4915 if (TREE_CODE (def
) == SSA_NAME
4916 && (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
)))
4918 if (!virtual_operand_p (def
)
4919 && ssa_undefined_value_p (def
, false))
4922 def
= SSA_VAL (def
);
4924 vp1
->phiargs
[e
->dest_idx
] = def
;
4926 vp1
->value_id
= VN_INFO (result
)->value_id
;
4927 vp1
->type
= TREE_TYPE (gimple_phi_result (phi
));
4928 vp1
->block
= gimple_bb (phi
);
4929 /* Extract values of the controlling condition. */
4930 vp1
->cclhs
= NULL_TREE
;
4931 vp1
->ccrhs
= NULL_TREE
;
4932 if (EDGE_COUNT (vp1
->block
->preds
) == 2
4933 && vp1
->block
->loop_father
->header
!= vp1
->block
)
4935 basic_block idom1
= get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4936 if (EDGE_COUNT (idom1
->succs
) == 2)
4937 if (gcond
*last1
= safe_dyn_cast
<gcond
*> (*gsi_last_bb (idom1
)))
4939 /* ??? We want to use SSA_VAL here. But possibly not
4941 vp1
->cclhs
= vn_valueize (gimple_cond_lhs (last1
));
4942 vp1
->ccrhs
= vn_valueize (gimple_cond_rhs (last1
));
4945 vp1
->result
= result
;
4946 vp1
->hashcode
= vn_phi_compute_hash (vp1
);
4948 slot
= valid_info
->phis
->find_slot_with_hash (vp1
, vp1
->hashcode
, INSERT
);
4949 gcc_assert (!*slot
);
4952 vp1
->next
= last_inserted_phi
;
4953 last_inserted_phi
= vp1
;
4958 /* Return true if BB1 is dominated by BB2 taking into account edges
4959 that are not executable. When ALLOW_BACK is false consider not
4960 executable backedges as executable. */
4963 dominated_by_p_w_unex (basic_block bb1
, basic_block bb2
, bool allow_back
)
4968 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
4971 /* Before iterating we'd like to know if there exists a
4972 (executable) path from bb2 to bb1 at all, if not we can
4973 directly return false. For now simply iterate once. */
4975 /* Iterate to the single executable bb1 predecessor. */
4976 if (EDGE_COUNT (bb1
->preds
) > 1)
4979 FOR_EACH_EDGE (e
, ei
, bb1
->preds
)
4980 if ((e
->flags
& EDGE_EXECUTABLE
)
4981 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
)))
4994 /* Re-do the dominance check with changed bb1. */
4995 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
5000 /* Iterate to the single executable bb2 successor. */
5001 if (EDGE_COUNT (bb2
->succs
) > 1)
5004 FOR_EACH_EDGE (e
, ei
, bb2
->succs
)
5005 if ((e
->flags
& EDGE_EXECUTABLE
)
5006 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
)))
5017 /* Verify the reached block is only reached through succe.
5018 If there is only one edge we can spare us the dominator
5019 check and iterate directly. */
5020 if (EDGE_COUNT (succe
->dest
->preds
) > 1)
5022 FOR_EACH_EDGE (e
, ei
, succe
->dest
->preds
)
5024 && ((e
->flags
& EDGE_EXECUTABLE
)
5025 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
))))
5035 /* Re-do the dominance check with changed bb2. */
5036 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
5042 /* We could now iterate updating bb1 / bb2. */
5046 /* Set the value number of FROM to TO, return true if it has changed
5050 set_ssa_val_to (tree from
, tree to
)
5052 vn_ssa_aux_t from_info
= VN_INFO (from
);
5053 tree currval
= from_info
->valnum
; // SSA_VAL (from)
5054 poly_int64 toff
, coff
;
5055 bool curr_undefined
= false;
5056 bool curr_invariant
= false;
5058 /* The only thing we allow as value numbers are ssa_names
5059 and invariants. So assert that here. We don't allow VN_TOP
5060 as visiting a stmt should produce a value-number other than
5062 ??? Still VN_TOP can happen for unreachable code, so force
5063 it to varying in that case. Not all code is prepared to
5064 get VN_TOP on valueization. */
5067 /* ??? When iterating and visiting PHI <undef, backedge-value>
5068 for the first time we rightfully get VN_TOP and we need to
5069 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5070 With SCCVN we were simply lucky we iterated the other PHI
5071 cycles first and thus visited the backedge-value DEF. */
5072 if (currval
== VN_TOP
)
5074 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5075 fprintf (dump_file
, "Forcing value number to varying on "
5076 "receiving VN_TOP\n");
5080 gcc_checking_assert (to
!= NULL_TREE
5081 && ((TREE_CODE (to
) == SSA_NAME
5082 && (to
== from
|| SSA_VAL (to
) == to
))
5083 || is_gimple_min_invariant (to
)));
5087 if (currval
== from
)
5089 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5091 fprintf (dump_file
, "Not changing value number of ");
5092 print_generic_expr (dump_file
, from
);
5093 fprintf (dump_file
, " from VARYING to ");
5094 print_generic_expr (dump_file
, to
);
5095 fprintf (dump_file
, "\n");
5099 curr_invariant
= is_gimple_min_invariant (currval
);
5100 curr_undefined
= (TREE_CODE (currval
) == SSA_NAME
5101 && !virtual_operand_p (currval
)
5102 && ssa_undefined_value_p (currval
, false));
5103 if (currval
!= VN_TOP
5106 && is_gimple_min_invariant (to
))
5108 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5110 fprintf (dump_file
, "Forcing VARYING instead of changing "
5111 "value number of ");
5112 print_generic_expr (dump_file
, from
);
5113 fprintf (dump_file
, " from ");
5114 print_generic_expr (dump_file
, currval
);
5115 fprintf (dump_file
, " (non-constant) to ");
5116 print_generic_expr (dump_file
, to
);
5117 fprintf (dump_file
, " (constant)\n");
5121 else if (currval
!= VN_TOP
5123 && TREE_CODE (to
) == SSA_NAME
5124 && !virtual_operand_p (to
)
5125 && ssa_undefined_value_p (to
, false))
5127 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5129 fprintf (dump_file
, "Forcing VARYING instead of changing "
5130 "value number of ");
5131 print_generic_expr (dump_file
, from
);
5132 fprintf (dump_file
, " from ");
5133 print_generic_expr (dump_file
, currval
);
5134 fprintf (dump_file
, " (non-undefined) to ");
5135 print_generic_expr (dump_file
, to
);
5136 fprintf (dump_file
, " (undefined)\n");
5140 else if (TREE_CODE (to
) == SSA_NAME
5141 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to
))
5146 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5148 fprintf (dump_file
, "Setting value number of ");
5149 print_generic_expr (dump_file
, from
);
5150 fprintf (dump_file
, " to ");
5151 print_generic_expr (dump_file
, to
);
5155 && !operand_equal_p (currval
, to
, 0)
5156 /* Different undefined SSA names are not actually different. See
5157 PR82320 for a testcase were we'd otherwise not terminate iteration. */
5159 && TREE_CODE (to
) == SSA_NAME
5160 && !virtual_operand_p (to
)
5161 && ssa_undefined_value_p (to
, false))
5162 /* ??? For addresses involving volatile objects or types operand_equal_p
5163 does not reliably detect ADDR_EXPRs as equal. We know we are only
5164 getting invariant gimple addresses here, so can use
5165 get_addr_base_and_unit_offset to do this comparison. */
5166 && !(TREE_CODE (currval
) == ADDR_EXPR
5167 && TREE_CODE (to
) == ADDR_EXPR
5168 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval
, 0), &coff
)
5169 == get_addr_base_and_unit_offset (TREE_OPERAND (to
, 0), &toff
))
5170 && known_eq (coff
, toff
)))
5173 && currval
!= VN_TOP
5175 /* We do not want to allow lattice transitions from one value
5176 to another since that may lead to not terminating iteration
5177 (see PR95049). Since there's no convenient way to check
5178 for the allowed transition of VAL -> PHI (loop entry value,
5179 same on two PHIs, to same PHI result) we restrict the check
5182 && is_gimple_min_invariant (to
))
5184 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5185 fprintf (dump_file
, " forced VARYING");
5188 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5189 fprintf (dump_file
, " (changed)\n");
5190 from_info
->valnum
= to
;
5193 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5194 fprintf (dump_file
, "\n");
5198 /* Set all definitions in STMT to value number to themselves.
5199 Return true if a value number changed. */
5202 defs_to_varying (gimple
*stmt
)
5204 bool changed
= false;
5208 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_ALL_DEFS
)
5210 tree def
= DEF_FROM_PTR (defp
);
5211 changed
|= set_ssa_val_to (def
, def
);
5216 /* Visit a copy between LHS and RHS, return true if the value number
5220 visit_copy (tree lhs
, tree rhs
)
5223 rhs
= SSA_VAL (rhs
);
5225 return set_ssa_val_to (lhs
, rhs
);
5228 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5232 valueized_wider_op (tree wide_type
, tree op
, bool allow_truncate
)
5234 if (TREE_CODE (op
) == SSA_NAME
)
5235 op
= vn_valueize (op
);
5237 /* Either we have the op widened available. */
5240 tree tem
= vn_nary_op_lookup_pieces (1, NOP_EXPR
,
5241 wide_type
, ops
, NULL
);
5245 /* Or the op is truncated from some existing value. */
5246 if (allow_truncate
&& TREE_CODE (op
) == SSA_NAME
)
5248 gimple
*def
= SSA_NAME_DEF_STMT (op
);
5249 if (is_gimple_assign (def
)
5250 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def
)))
5252 tem
= gimple_assign_rhs1 (def
);
5253 if (useless_type_conversion_p (wide_type
, TREE_TYPE (tem
)))
5255 if (TREE_CODE (tem
) == SSA_NAME
)
5256 tem
= vn_valueize (tem
);
5262 /* For constants simply extend it. */
5263 if (TREE_CODE (op
) == INTEGER_CST
)
5264 return wide_int_to_tree (wide_type
, wi::to_widest (op
));
5269 /* Visit a nary operator RHS, value number it, and return true if the
5270 value number of LHS has changed as a result. */
5273 visit_nary_op (tree lhs
, gassign
*stmt
)
5275 vn_nary_op_t vnresult
;
5276 tree result
= vn_nary_op_lookup_stmt (stmt
, &vnresult
);
5277 if (! result
&& vnresult
)
5278 result
= vn_nary_op_get_predicated_value (vnresult
, gimple_bb (stmt
));
5280 return set_ssa_val_to (lhs
, result
);
5282 /* Do some special pattern matching for redundancies of operations
5283 in different types. */
5284 enum tree_code code
= gimple_assign_rhs_code (stmt
);
5285 tree type
= TREE_TYPE (lhs
);
5286 tree rhs1
= gimple_assign_rhs1 (stmt
);
5290 /* Match arithmetic done in a different type where we can easily
5291 substitute the result from some earlier sign-changed or widened
5293 if (INTEGRAL_TYPE_P (type
)
5294 && TREE_CODE (rhs1
) == SSA_NAME
5295 /* We only handle sign-changes, zero-extension -> & mask or
5296 sign-extension if we know the inner operation doesn't
5298 && (((TYPE_UNSIGNED (TREE_TYPE (rhs1
))
5299 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
5300 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1
))))
5301 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
5302 || TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (rhs1
))))
5304 gassign
*def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (rhs1
));
5306 && (gimple_assign_rhs_code (def
) == PLUS_EXPR
5307 || gimple_assign_rhs_code (def
) == MINUS_EXPR
5308 || gimple_assign_rhs_code (def
) == MULT_EXPR
))
5311 /* When requiring a sign-extension we cannot model a
5312 previous truncation with a single op so don't bother. */
5313 bool allow_truncate
= TYPE_UNSIGNED (TREE_TYPE (rhs1
));
5314 /* Either we have the op widened available. */
5315 ops
[0] = valueized_wider_op (type
, gimple_assign_rhs1 (def
),
5318 ops
[1] = valueized_wider_op (type
, gimple_assign_rhs2 (def
),
5320 if (ops
[0] && ops
[1])
5322 ops
[0] = vn_nary_op_lookup_pieces
5323 (2, gimple_assign_rhs_code (def
), type
, ops
, NULL
);
5324 /* We have wider operation available. */
5326 /* If the leader is a wrapping operation we can
5327 insert it for code hoisting w/o introducing
5328 undefined overflow. If it is not it has to
5329 be available. See PR86554. */
5330 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops
[0]))
5331 || (rpo_avail
&& vn_context_bb
5332 && rpo_avail
->eliminate_avail (vn_context_bb
,
5335 unsigned lhs_prec
= TYPE_PRECISION (type
);
5336 unsigned rhs_prec
= TYPE_PRECISION (TREE_TYPE (rhs1
));
5337 if (lhs_prec
== rhs_prec
5338 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
5339 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1
))))
5341 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5342 NOP_EXPR
, type
, ops
[0]);
5343 result
= vn_nary_build_or_lookup (&match_op
);
5346 bool changed
= set_ssa_val_to (lhs
, result
);
5347 vn_nary_op_insert_stmt (stmt
, result
);
5353 tree mask
= wide_int_to_tree
5354 (type
, wi::mask (rhs_prec
, false, lhs_prec
));
5355 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5359 result
= vn_nary_build_or_lookup (&match_op
);
5362 bool changed
= set_ssa_val_to (lhs
, result
);
5363 vn_nary_op_insert_stmt (stmt
, result
);
5373 if (INTEGRAL_TYPE_P (type
)
5374 && TREE_CODE (rhs1
) == SSA_NAME
5375 && TREE_CODE (gimple_assign_rhs2 (stmt
)) == INTEGER_CST
5376 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
)
5377 && default_vn_walk_kind
!= VN_NOWALK
5379 && BITS_PER_UNIT
== 8
5380 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
5381 && !integer_all_onesp (gimple_assign_rhs2 (stmt
))
5382 && !integer_zerop (gimple_assign_rhs2 (stmt
)))
5384 gassign
*ass
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (rhs1
));
5386 && !gimple_has_volatile_ops (ass
)
5387 && vn_get_stmt_kind (ass
) == VN_REFERENCE
)
5389 tree last_vuse
= gimple_vuse (ass
);
5390 tree op
= gimple_assign_rhs1 (ass
);
5391 tree result
= vn_reference_lookup (op
, gimple_vuse (ass
),
5392 default_vn_walk_kind
,
5393 NULL
, true, &last_vuse
,
5394 gimple_assign_rhs2 (stmt
));
5396 && useless_type_conversion_p (TREE_TYPE (result
),
5398 return set_ssa_val_to (lhs
, result
);
5402 case TRUNC_DIV_EXPR
:
5403 if (TYPE_UNSIGNED (type
))
5408 /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5409 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type
))
5413 rhs
[1] = gimple_assign_rhs2 (stmt
);
5414 for (unsigned i
= 0; i
<= 1; ++i
)
5416 unsigned j
= i
== 0 ? 1 : 0;
5418 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5419 NEGATE_EXPR
, type
, rhs
[i
]);
5420 ops
[i
] = vn_nary_build_or_lookup_1 (&match_op
, false, true);
5423 && (ops
[0] = vn_nary_op_lookup_pieces (2, code
,
5426 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5427 NEGATE_EXPR
, type
, ops
[0]);
5428 result
= vn_nary_build_or_lookup_1 (&match_op
, true, false);
5431 bool changed
= set_ssa_val_to (lhs
, result
);
5432 vn_nary_op_insert_stmt (stmt
, result
);
5440 /* For X << C, use the value number of X * (1 << C). */
5441 if (INTEGRAL_TYPE_P (type
)
5442 && TYPE_OVERFLOW_WRAPS (type
)
5443 && !TYPE_SATURATING (type
))
5445 tree rhs2
= gimple_assign_rhs2 (stmt
);
5446 if (TREE_CODE (rhs2
) == INTEGER_CST
5447 && tree_fits_uhwi_p (rhs2
)
5448 && tree_to_uhwi (rhs2
) < TYPE_PRECISION (type
))
5450 wide_int w
= wi::set_bit_in_zero (tree_to_uhwi (rhs2
),
5451 TYPE_PRECISION (type
));
5452 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5453 MULT_EXPR
, type
, rhs1
,
5454 wide_int_to_tree (type
, w
));
5455 result
= vn_nary_build_or_lookup (&match_op
);
5458 bool changed
= set_ssa_val_to (lhs
, result
);
5459 if (TREE_CODE (result
) == SSA_NAME
)
5460 vn_nary_op_insert_stmt (stmt
, result
);
5470 bool changed
= set_ssa_val_to (lhs
, lhs
);
5471 vn_nary_op_insert_stmt (stmt
, lhs
);
5475 /* Visit a call STMT storing into LHS. Return true if the value number
5476 of the LHS has changed as a result. */
5479 visit_reference_op_call (tree lhs
, gcall
*stmt
)
5481 bool changed
= false;
5482 struct vn_reference_s vr1
;
5483 vn_reference_t vnresult
= NULL
;
5484 tree vdef
= gimple_vdef (stmt
);
5485 modref_summary
*summary
;
5487 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5488 if (lhs
&& TREE_CODE (lhs
) != SSA_NAME
)
5491 vn_reference_lookup_call (stmt
, &vnresult
, &vr1
);
5493 /* If the lookup did not succeed for pure functions try to use
5494 modref info to find a candidate to CSE to. */
5495 const unsigned accesses_limit
= 8;
5499 && gimple_vuse (stmt
)
5500 && (((summary
= get_modref_function_summary (stmt
, NULL
))
5501 && !summary
->global_memory_read
5502 && summary
->load_accesses
< accesses_limit
)
5503 || gimple_call_flags (stmt
) & ECF_CONST
))
5505 /* First search if we can do someting useful and build a
5506 vector of all loads we have to check. */
5507 bool unknown_memory_access
= false;
5508 auto_vec
<ao_ref
, accesses_limit
> accesses
;
5509 unsigned load_accesses
= summary
? summary
->load_accesses
: 0;
5510 if (!unknown_memory_access
)
5511 /* Add loads done as part of setting up the call arguments.
5512 That's also necessary for CONST functions which will
5513 not have a modref summary. */
5514 for (unsigned i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
5516 tree arg
= gimple_call_arg (stmt
, i
);
5517 if (TREE_CODE (arg
) != SSA_NAME
5518 && !is_gimple_min_invariant (arg
))
5520 if (accesses
.length () >= accesses_limit
- load_accesses
)
5522 unknown_memory_access
= true;
5525 accesses
.quick_grow (accesses
.length () + 1);
5526 ao_ref_init (&accesses
.last (), arg
);
5529 if (summary
&& !unknown_memory_access
)
5531 /* Add loads as analyzed by IPA modref. */
5532 for (auto base_node
: summary
->loads
->bases
)
5533 if (unknown_memory_access
)
5535 else for (auto ref_node
: base_node
->refs
)
5536 if (unknown_memory_access
)
5538 else for (auto access_node
: ref_node
->accesses
)
5540 accesses
.quick_grow (accesses
.length () + 1);
5541 ao_ref
*r
= &accesses
.last ();
5542 if (!access_node
.get_ao_ref (stmt
, r
))
5544 /* Initialize a ref based on the argument and
5545 unknown offset if possible. */
5546 tree arg
= access_node
.get_call_arg (stmt
);
5547 if (arg
&& TREE_CODE (arg
) == SSA_NAME
)
5548 arg
= SSA_VAL (arg
);
5550 && TREE_CODE (arg
) == ADDR_EXPR
5551 && (arg
= get_base_address (arg
))
5554 ao_ref_init (r
, arg
);
5560 unknown_memory_access
= true;
5564 r
->base_alias_set
= base_node
->base
;
5565 r
->ref_alias_set
= ref_node
->ref
;
5569 /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5570 for the call in the hashtable. */
5571 unsigned limit
= (unknown_memory_access
5573 : (param_sccvn_max_alias_queries_per_access
5574 / (accesses
.length () + 1)));
5575 tree saved_vuse
= vr1
.vuse
;
5576 hashval_t saved_hashcode
= vr1
.hashcode
;
5577 while (limit
> 0 && !vnresult
&& !SSA_NAME_IS_DEFAULT_DEF (vr1
.vuse
))
5579 vr1
.hashcode
= vr1
.hashcode
- SSA_NAME_VERSION (vr1
.vuse
);
5580 gimple
*def
= SSA_NAME_DEF_STMT (vr1
.vuse
);
5581 /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5582 do not bother for now. */
5583 if (is_a
<gphi
*> (def
))
5585 vr1
.vuse
= vuse_ssa_val (gimple_vuse (def
));
5586 vr1
.hashcode
= vr1
.hashcode
+ SSA_NAME_VERSION (vr1
.vuse
);
5587 vn_reference_lookup_1 (&vr1
, &vnresult
);
5591 /* If we found a candidate to CSE to verify it is valid. */
5592 if (vnresult
&& !accesses
.is_empty ())
5594 tree vuse
= vuse_ssa_val (gimple_vuse (stmt
));
5595 while (vnresult
&& vuse
!= vr1
.vuse
)
5597 gimple
*def
= SSA_NAME_DEF_STMT (vuse
);
5598 for (auto &ref
: accesses
)
5600 /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5601 analysis overhead that we might be able to cache. */
5602 if (stmt_may_clobber_ref_p_1 (def
, &ref
, true))
5608 vuse
= vuse_ssa_val (gimple_vuse (def
));
5611 vr1
.vuse
= saved_vuse
;
5612 vr1
.hashcode
= saved_hashcode
;
5619 if (vnresult
->result_vdef
)
5620 changed
|= set_ssa_val_to (vdef
, vnresult
->result_vdef
);
5621 else if (!lhs
&& gimple_call_lhs (stmt
))
5622 /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5623 as the call still acts as a lhs store. */
5624 changed
|= set_ssa_val_to (vdef
, vdef
);
5626 /* If the call was discovered to be pure or const reflect
5627 that as far as possible. */
5628 changed
|= set_ssa_val_to (vdef
,
5629 vuse_ssa_val (gimple_vuse (stmt
)));
5632 if (!vnresult
->result
&& lhs
)
5633 vnresult
->result
= lhs
;
5635 if (vnresult
->result
&& lhs
)
5636 changed
|= set_ssa_val_to (lhs
, vnresult
->result
);
5641 vn_reference_s
**slot
;
5642 tree vdef_val
= vdef
;
5645 /* If we value numbered an indirect functions function to
5646 one not clobbering memory value number its VDEF to its
5648 tree fn
= gimple_call_fn (stmt
);
5649 if (fn
&& TREE_CODE (fn
) == SSA_NAME
)
5652 if (TREE_CODE (fn
) == ADDR_EXPR
5653 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
5654 && (flags_from_decl_or_type (TREE_OPERAND (fn
, 0))
5655 & (ECF_CONST
| ECF_PURE
))
5656 /* If stmt has non-SSA_NAME lhs, value number the
5657 vdef to itself, as the call still acts as a lhs
5659 && (lhs
|| gimple_call_lhs (stmt
) == NULL_TREE
))
5660 vdef_val
= vuse_ssa_val (gimple_vuse (stmt
));
5662 changed
|= set_ssa_val_to (vdef
, vdef_val
);
5665 changed
|= set_ssa_val_to (lhs
, lhs
);
5666 vr2
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
5667 vr2
->vuse
= vr1
.vuse
;
5668 /* As we are not walking the virtual operand chain we know the
5669 shared_lookup_references are still original so we can re-use
5671 vr2
->operands
= vr1
.operands
.copy ();
5672 vr2
->type
= vr1
.type
;
5673 vr2
->punned
= vr1
.punned
;
5675 vr2
->base_set
= vr1
.base_set
;
5676 vr2
->hashcode
= vr1
.hashcode
;
5678 vr2
->result_vdef
= vdef_val
;
5680 slot
= valid_info
->references
->find_slot_with_hash (vr2
, vr2
->hashcode
,
5682 gcc_assert (!*slot
);
5684 vr2
->next
= last_inserted_ref
;
5685 last_inserted_ref
= vr2
;
5691 /* Visit a load from a reference operator RHS, part of STMT, value number it,
5692 and return true if the value number of the LHS has changed as a result. */
5695 visit_reference_op_load (tree lhs
, tree op
, gimple
*stmt
)
5697 bool changed
= false;
5701 tree vuse
= gimple_vuse (stmt
);
5702 tree last_vuse
= vuse
;
5703 result
= vn_reference_lookup (op
, vuse
, default_vn_walk_kind
, &res
, true, &last_vuse
);
5705 /* We handle type-punning through unions by value-numbering based
5706 on offset and size of the access. Be prepared to handle a
5707 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5709 && !useless_type_conversion_p (TREE_TYPE (result
), TREE_TYPE (op
)))
5711 /* Avoid the type punning in case the result mode has padding where
5712 the op we lookup has not. */
5713 if (maybe_lt (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (result
))),
5714 GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op
)))))
5718 /* We will be setting the value number of lhs to the value number
5719 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5720 So first simplify and lookup this expression to see if it
5721 is already available. */
5722 gimple_match_op
res_op (gimple_match_cond::UNCOND
,
5723 VIEW_CONVERT_EXPR
, TREE_TYPE (op
), result
);
5724 result
= vn_nary_build_or_lookup (&res_op
);
5726 && TREE_CODE (result
) == SSA_NAME
5727 && VN_INFO (result
)->needs_insertion
)
5728 /* Track whether this is the canonical expression for different
5729 typed loads. We use that as a stopgap measure for code
5730 hoisting when dealing with floating point loads. */
5734 /* When building the conversion fails avoid inserting the reference
5737 return set_ssa_val_to (lhs
, lhs
);
5741 changed
= set_ssa_val_to (lhs
, result
);
5744 changed
= set_ssa_val_to (lhs
, lhs
);
5745 vn_reference_insert (op
, lhs
, last_vuse
, NULL_TREE
);
5746 if (vuse
&& SSA_VAL (last_vuse
) != SSA_VAL (vuse
))
5748 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5750 fprintf (dump_file
, "Using extra use virtual operand ");
5751 print_generic_expr (dump_file
, last_vuse
);
5752 fprintf (dump_file
, "\n");
5754 vn_reference_insert (op
, lhs
, vuse
, NULL_TREE
);
5762 /* Visit a store to a reference operator LHS, part of STMT, value number it,
5763 and return true if the value number of the LHS has changed as a result. */
5766 visit_reference_op_store (tree lhs
, tree op
, gimple
*stmt
)
5768 bool changed
= false;
5769 vn_reference_t vnresult
= NULL
;
5771 bool resultsame
= false;
5772 tree vuse
= gimple_vuse (stmt
);
5773 tree vdef
= gimple_vdef (stmt
);
5775 if (TREE_CODE (op
) == SSA_NAME
)
5778 /* First we want to lookup using the *vuses* from the store and see
5779 if there the last store to this location with the same address
5782 The vuses represent the memory state before the store. If the
5783 memory state, address, and value of the store is the same as the
5784 last store to this location, then this store will produce the
5785 same memory state as that store.
5787 In this case the vdef versions for this store are value numbered to those
5788 vuse versions, since they represent the same memory state after
5791 Otherwise, the vdefs for the store are used when inserting into
5792 the table, since the store generates a new memory state. */
5794 vn_reference_lookup (lhs
, vuse
, VN_NOWALK
, &vnresult
, false);
5796 && vnresult
->result
)
5798 tree result
= vnresult
->result
;
5799 gcc_checking_assert (TREE_CODE (result
) != SSA_NAME
5800 || result
== SSA_VAL (result
));
5801 resultsame
= expressions_equal_p (result
, op
);
5804 /* If the TBAA state isn't compatible for downstream reads
5805 we cannot value-number the VDEFs the same. */
5807 ao_ref_init (&lhs_ref
, lhs
);
5808 alias_set_type set
= ao_ref_alias_set (&lhs_ref
);
5809 alias_set_type base_set
= ao_ref_base_alias_set (&lhs_ref
);
5810 if ((vnresult
->set
!= set
5811 && ! alias_set_subset_of (set
, vnresult
->set
))
5812 || (vnresult
->base_set
!= base_set
5813 && ! alias_set_subset_of (base_set
, vnresult
->base_set
)))
5820 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5822 fprintf (dump_file
, "No store match\n");
5823 fprintf (dump_file
, "Value numbering store ");
5824 print_generic_expr (dump_file
, lhs
);
5825 fprintf (dump_file
, " to ");
5826 print_generic_expr (dump_file
, op
);
5827 fprintf (dump_file
, "\n");
5829 /* Have to set value numbers before insert, since insert is
5830 going to valueize the references in-place. */
5832 changed
|= set_ssa_val_to (vdef
, vdef
);
5834 /* Do not insert structure copies into the tables. */
5835 if (is_gimple_min_invariant (op
)
5836 || is_gimple_reg (op
))
5837 vn_reference_insert (lhs
, op
, vdef
, NULL
);
5839 /* Only perform the following when being called from PRE
5840 which embeds tail merging. */
5841 if (default_vn_walk_kind
== VN_WALK
)
5843 assign
= build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, op
);
5844 vn_reference_lookup (assign
, vuse
, VN_NOWALK
, &vnresult
, false);
5846 vn_reference_insert (assign
, lhs
, vuse
, vdef
);
5851 /* We had a match, so value number the vdef to have the value
5852 number of the vuse it came from. */
5854 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5855 fprintf (dump_file
, "Store matched earlier value, "
5856 "value numbering store vdefs to matching vuses.\n");
5858 changed
|= set_ssa_val_to (vdef
, SSA_VAL (vuse
));
5864 /* Visit and value number PHI, return true if the value number
5865 changed. When BACKEDGES_VARYING_P is true then assume all
5866 backedge values are varying. When INSERTED is not NULL then
5867 this is just a ahead query for a possible iteration, set INSERTED
5868 to true if we'd insert into the hashtable. */
5871 visit_phi (gimple
*phi
, bool *inserted
, bool backedges_varying_p
)
5873 tree result
, sameval
= VN_TOP
, seen_undef
= NULL_TREE
;
5874 tree backedge_val
= NULL_TREE
;
5875 bool seen_non_backedge
= false;
5876 tree sameval_base
= NULL_TREE
;
5877 poly_int64 soff
, doff
;
5878 unsigned n_executable
= 0;
5880 edge e
, sameval_e
= NULL
;
5882 /* TODO: We could check for this in initialization, and replace this
5883 with a gcc_assert. */
5884 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi
)))
5885 return set_ssa_val_to (PHI_RESULT (phi
), PHI_RESULT (phi
));
5887 /* We track whether a PHI was CSEd to to avoid excessive iterations
5888 that would be necessary only because the PHI changed arguments
5891 gimple_set_plf (phi
, GF_PLF_1
, false);
5893 /* See if all non-TOP arguments have the same value. TOP is
5894 equivalent to everything, so we can ignore it. */
5895 basic_block bb
= gimple_bb (phi
);
5896 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5897 if (e
->flags
& EDGE_EXECUTABLE
)
5899 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
5901 if (def
== PHI_RESULT (phi
))
5904 if (TREE_CODE (def
) == SSA_NAME
)
5906 if (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
))
5907 def
= SSA_VAL (def
);
5908 if (e
->flags
& EDGE_DFS_BACK
)
5911 if (!(e
->flags
& EDGE_DFS_BACK
))
5912 seen_non_backedge
= true;
5915 /* Ignore undefined defs for sameval but record one. */
5916 else if (TREE_CODE (def
) == SSA_NAME
5917 && ! virtual_operand_p (def
)
5918 && ssa_undefined_value_p (def
, false))
5920 else if (sameval
== VN_TOP
)
5925 else if (expressions_equal_p (def
, sameval
))
5927 else if (virtual_operand_p (def
))
5929 sameval
= NULL_TREE
;
5934 /* We know we're arriving only with invariant addresses here,
5935 try harder comparing them. We can do some caching here
5936 which we cannot do in expressions_equal_p. */
5937 if (TREE_CODE (def
) == ADDR_EXPR
5938 && TREE_CODE (sameval
) == ADDR_EXPR
5939 && sameval_base
!= (void *)-1)
5942 sameval_base
= get_addr_base_and_unit_offset
5943 (TREE_OPERAND (sameval
, 0), &soff
);
5945 sameval_base
= (tree
)(void *)-1;
5946 else if ((get_addr_base_and_unit_offset
5947 (TREE_OPERAND (def
, 0), &doff
) == sameval_base
)
5948 && known_eq (soff
, doff
))
5951 /* There's also the possibility to use equivalences. */
5952 if (!FLOAT_TYPE_P (TREE_TYPE (def
))
5953 /* But only do this if we didn't force any of sameval or
5954 val to VARYING because of backedge processing rules. */
5955 && (TREE_CODE (sameval
) != SSA_NAME
5956 || SSA_VAL (sameval
) == sameval
)
5957 && (TREE_CODE (def
) != SSA_NAME
|| SSA_VAL (def
) == def
))
5959 vn_nary_op_t vnresult
;
5963 tree val
= vn_nary_op_lookup_pieces (2, EQ_EXPR
,
5966 if (! val
&& vnresult
&& vnresult
->predicated_values
)
5968 val
= vn_nary_op_get_predicated_value (vnresult
, e
);
5969 if (val
&& integer_truep (val
)
5970 && !(sameval_e
&& (sameval_e
->flags
& EDGE_DFS_BACK
)))
5972 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5974 fprintf (dump_file
, "Predication says ");
5975 print_generic_expr (dump_file
, def
, TDF_NONE
);
5976 fprintf (dump_file
, " and ");
5977 print_generic_expr (dump_file
, sameval
, TDF_NONE
);
5978 fprintf (dump_file
, " are equal on edge %d -> %d\n",
5979 e
->src
->index
, e
->dest
->index
);
5983 /* If on all previous edges the value was equal to def
5984 we can change sameval to def. */
5985 if (EDGE_COUNT (bb
->preds
) == 2
5986 && (val
= vn_nary_op_get_predicated_value
5987 (vnresult
, EDGE_PRED (bb
, 0)))
5988 && integer_truep (val
)
5989 && !(e
->flags
& EDGE_DFS_BACK
))
5991 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5993 fprintf (dump_file
, "Predication says ");
5994 print_generic_expr (dump_file
, def
, TDF_NONE
);
5995 fprintf (dump_file
, " and ");
5996 print_generic_expr (dump_file
, sameval
, TDF_NONE
);
5997 fprintf (dump_file
, " are equal on edge %d -> %d\n",
5998 EDGE_PRED (bb
, 0)->src
->index
,
5999 EDGE_PRED (bb
, 0)->dest
->index
);
6006 sameval
= NULL_TREE
;
6011 /* If the value we want to use is flowing over the backedge and we
6012 should take it as VARYING but it has a non-VARYING value drop to
6014 If we value-number a virtual operand never value-number to the
6015 value from the backedge as that confuses the alias-walking code.
6016 See gcc.dg/torture/pr87176.c. If the value is the same on a
6017 non-backedge everything is OK though. */
6020 && !seen_non_backedge
6021 && TREE_CODE (backedge_val
) == SSA_NAME
6022 && sameval
== backedge_val
6023 && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val
)
6024 || SSA_VAL (backedge_val
) != backedge_val
))
6025 /* Do not value-number a virtual operand to sth not visited though
6026 given that allows us to escape a region in alias walking. */
6028 && TREE_CODE (sameval
) == SSA_NAME
6029 && !SSA_NAME_IS_DEFAULT_DEF (sameval
)
6030 && SSA_NAME_IS_VIRTUAL_OPERAND (sameval
)
6031 && (SSA_VAL (sameval
, &visited_p
), !visited_p
)))
6032 /* Note this just drops to VARYING without inserting the PHI into
6034 result
= PHI_RESULT (phi
);
6035 /* If none of the edges was executable keep the value-number at VN_TOP,
6036 if only a single edge is exectuable use its value. */
6037 else if (n_executable
<= 1)
6038 result
= seen_undef
? seen_undef
: sameval
;
6039 /* If we saw only undefined values and VN_TOP use one of the
6040 undefined values. */
6041 else if (sameval
== VN_TOP
)
6042 result
= seen_undef
? seen_undef
: sameval
;
6043 /* First see if it is equivalent to a phi node in this block. We prefer
6044 this as it allows IV elimination - see PRs 66502 and 67167. */
6045 else if ((result
= vn_phi_lookup (phi
, backedges_varying_p
)))
6048 && TREE_CODE (result
) == SSA_NAME
6049 && gimple_code (SSA_NAME_DEF_STMT (result
)) == GIMPLE_PHI
)
6051 gimple_set_plf (SSA_NAME_DEF_STMT (result
), GF_PLF_1
, true);
6052 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6054 fprintf (dump_file
, "Marking CSEd to PHI node ");
6055 print_gimple_expr (dump_file
, SSA_NAME_DEF_STMT (result
),
6057 fprintf (dump_file
, "\n");
6061 /* If all values are the same use that, unless we've seen undefined
6062 values as well and the value isn't constant.
6063 CCP/copyprop have the same restriction to not remove uninit warnings. */
6065 && (! seen_undef
|| is_gimple_min_invariant (sameval
)))
6069 result
= PHI_RESULT (phi
);
6070 /* Only insert PHIs that are varying, for constant value numbers
6071 we mess up equivalences otherwise as we are only comparing
6072 the immediate controlling predicates. */
6073 vn_phi_insert (phi
, result
, backedges_varying_p
);
6078 return set_ssa_val_to (PHI_RESULT (phi
), result
);
6081 /* Try to simplify RHS using equivalences and constant folding. */
6084 try_to_simplify (gassign
*stmt
)
6086 enum tree_code code
= gimple_assign_rhs_code (stmt
);
6089 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6090 in this case, there is no point in doing extra work. */
6091 if (code
== SSA_NAME
)
6094 /* First try constant folding based on our current lattice. */
6095 mprts_hook
= vn_lookup_simplify_result
;
6096 tem
= gimple_fold_stmt_to_constant_1 (stmt
, vn_valueize
, vn_valueize
);
6099 && (TREE_CODE (tem
) == SSA_NAME
6100 || is_gimple_min_invariant (tem
)))
6106 /* Visit and value number STMT, return true if the value number
6110 visit_stmt (gimple
*stmt
, bool backedges_varying_p
= false)
6112 bool changed
= false;
6114 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6116 fprintf (dump_file
, "Value numbering stmt = ");
6117 print_gimple_stmt (dump_file
, stmt
, 0);
6120 if (gimple_code (stmt
) == GIMPLE_PHI
)
6121 changed
= visit_phi (stmt
, NULL
, backedges_varying_p
);
6122 else if (gimple_has_volatile_ops (stmt
))
6123 changed
= defs_to_varying (stmt
);
6124 else if (gassign
*ass
= dyn_cast
<gassign
*> (stmt
))
6126 enum tree_code code
= gimple_assign_rhs_code (ass
);
6127 tree lhs
= gimple_assign_lhs (ass
);
6128 tree rhs1
= gimple_assign_rhs1 (ass
);
6131 /* Shortcut for copies. Simplifying copies is pointless,
6132 since we copy the expression and value they represent. */
6133 if (code
== SSA_NAME
6134 && TREE_CODE (lhs
) == SSA_NAME
)
6136 changed
= visit_copy (lhs
, rhs1
);
6139 simplified
= try_to_simplify (ass
);
6142 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6144 fprintf (dump_file
, "RHS ");
6145 print_gimple_expr (dump_file
, ass
, 0);
6146 fprintf (dump_file
, " simplified to ");
6147 print_generic_expr (dump_file
, simplified
);
6148 fprintf (dump_file
, "\n");
6151 /* Setting value numbers to constants will occasionally
6152 screw up phi congruence because constants are not
6153 uniquely associated with a single ssa name that can be
6156 && is_gimple_min_invariant (simplified
)
6157 && TREE_CODE (lhs
) == SSA_NAME
)
6159 changed
= set_ssa_val_to (lhs
, simplified
);
6163 && TREE_CODE (simplified
) == SSA_NAME
6164 && TREE_CODE (lhs
) == SSA_NAME
)
6166 changed
= visit_copy (lhs
, simplified
);
6170 if ((TREE_CODE (lhs
) == SSA_NAME
6171 /* We can substitute SSA_NAMEs that are live over
6172 abnormal edges with their constant value. */
6173 && !(gimple_assign_copy_p (ass
)
6174 && is_gimple_min_invariant (rhs1
))
6176 && is_gimple_min_invariant (simplified
))
6177 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
6178 /* Stores or copies from SSA_NAMEs that are live over
6179 abnormal edges are a problem. */
6180 || (code
== SSA_NAME
6181 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
)))
6182 changed
= defs_to_varying (ass
);
6183 else if (REFERENCE_CLASS_P (lhs
)
6185 changed
= visit_reference_op_store (lhs
, rhs1
, ass
);
6186 else if (TREE_CODE (lhs
) == SSA_NAME
)
6188 if ((gimple_assign_copy_p (ass
)
6189 && is_gimple_min_invariant (rhs1
))
6191 && is_gimple_min_invariant (simplified
)))
6194 changed
= set_ssa_val_to (lhs
, simplified
);
6196 changed
= set_ssa_val_to (lhs
, rhs1
);
6200 /* Visit the original statement. */
6201 switch (vn_get_stmt_kind (ass
))
6204 changed
= visit_nary_op (lhs
, ass
);
6207 changed
= visit_reference_op_load (lhs
, rhs1
, ass
);
6210 changed
= defs_to_varying (ass
);
6216 changed
= defs_to_varying (ass
);
6218 else if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
6220 tree lhs
= gimple_call_lhs (call_stmt
);
6221 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6223 /* Try constant folding based on our current lattice. */
6224 tree simplified
= gimple_fold_stmt_to_constant_1 (call_stmt
,
6228 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6230 fprintf (dump_file
, "call ");
6231 print_gimple_expr (dump_file
, call_stmt
, 0);
6232 fprintf (dump_file
, " simplified to ");
6233 print_generic_expr (dump_file
, simplified
);
6234 fprintf (dump_file
, "\n");
6237 /* Setting value numbers to constants will occasionally
6238 screw up phi congruence because constants are not
6239 uniquely associated with a single ssa name that can be
6242 && is_gimple_min_invariant (simplified
))
6244 changed
= set_ssa_val_to (lhs
, simplified
);
6245 if (gimple_vdef (call_stmt
))
6246 changed
|= set_ssa_val_to (gimple_vdef (call_stmt
),
6247 SSA_VAL (gimple_vuse (call_stmt
)));
6251 && TREE_CODE (simplified
) == SSA_NAME
)
6253 changed
= visit_copy (lhs
, simplified
);
6254 if (gimple_vdef (call_stmt
))
6255 changed
|= set_ssa_val_to (gimple_vdef (call_stmt
),
6256 SSA_VAL (gimple_vuse (call_stmt
)));
6259 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
6261 changed
= defs_to_varying (call_stmt
);
6266 /* Pick up flags from a devirtualization target. */
6267 tree fn
= gimple_call_fn (stmt
);
6268 int extra_fnflags
= 0;
6269 if (fn
&& TREE_CODE (fn
) == SSA_NAME
)
6272 if (TREE_CODE (fn
) == ADDR_EXPR
6273 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
)
6274 extra_fnflags
= flags_from_decl_or_type (TREE_OPERAND (fn
, 0));
6276 if ((/* Calls to the same function with the same vuse
6277 and the same operands do not necessarily return the same
6278 value, unless they're pure or const. */
6279 ((gimple_call_flags (call_stmt
) | extra_fnflags
)
6280 & (ECF_PURE
| ECF_CONST
))
6281 /* If calls have a vdef, subsequent calls won't have
6282 the same incoming vuse. So, if 2 calls with vdef have the
6283 same vuse, we know they're not subsequent.
6284 We can value number 2 calls to the same function with the
6285 same vuse and the same operands which are not subsequent
6286 the same, because there is no code in the program that can
6287 compare the 2 values... */
6288 || (gimple_vdef (call_stmt
)
6289 /* ... unless the call returns a pointer which does
6290 not alias with anything else. In which case the
6291 information that the values are distinct are encoded
6293 && !(gimple_call_return_flags (call_stmt
) & ERF_NOALIAS
)
6294 /* Only perform the following when being called from PRE
6295 which embeds tail merging. */
6296 && default_vn_walk_kind
== VN_WALK
))
6297 /* Do not process .DEFERRED_INIT since that confuses uninit
6299 && !gimple_call_internal_p (call_stmt
, IFN_DEFERRED_INIT
))
6300 changed
= visit_reference_op_call (lhs
, call_stmt
);
6302 changed
= defs_to_varying (call_stmt
);
6305 changed
= defs_to_varying (stmt
);
6311 /* Allocate a value number table. */
6314 allocate_vn_table (vn_tables_t table
, unsigned size
)
6316 table
->phis
= new vn_phi_table_type (size
);
6317 table
->nary
= new vn_nary_op_table_type (size
);
6318 table
->references
= new vn_reference_table_type (size
);
6321 /* Free a value number table. */
6324 free_vn_table (vn_tables_t table
)
6326 /* Walk over elements and release vectors. */
6327 vn_reference_iterator_type hir
;
6329 FOR_EACH_HASH_TABLE_ELEMENT (*table
->references
, vr
, vn_reference_t
, hir
)
6330 vr
->operands
.release ();
6335 delete table
->references
;
6336 table
->references
= NULL
;
6339 /* Set *ID according to RESULT. */
6342 set_value_id_for_result (tree result
, unsigned int *id
)
6344 if (result
&& TREE_CODE (result
) == SSA_NAME
)
6345 *id
= VN_INFO (result
)->value_id
;
6346 else if (result
&& is_gimple_min_invariant (result
))
6347 *id
= get_or_alloc_constant_value_id (result
);
6349 *id
= get_next_value_id ();
6352 /* Set the value ids in the valid hash tables. */
6355 set_hashtable_value_ids (void)
6357 vn_nary_op_iterator_type hin
;
6358 vn_phi_iterator_type hip
;
6359 vn_reference_iterator_type hir
;
6364 /* Now set the value ids of the things we had put in the hash
6367 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->nary
, vno
, vn_nary_op_t
, hin
)
6368 if (! vno
->predicated_values
)
6369 set_value_id_for_result (vno
->u
.result
, &vno
->value_id
);
6371 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->phis
, vp
, vn_phi_t
, hip
)
6372 set_value_id_for_result (vp
->result
, &vp
->value_id
);
6374 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->references
, vr
, vn_reference_t
,
6376 set_value_id_for_result (vr
->result
, &vr
->value_id
);
6379 /* Return the maximum value id we have ever seen. */
6382 get_max_value_id (void)
6384 return next_value_id
;
6387 /* Return the maximum constant value id we have ever seen. */
6390 get_max_constant_value_id (void)
6392 return -next_constant_value_id
;
6395 /* Return the next unique value id. */
6398 get_next_value_id (void)
6400 gcc_checking_assert ((int)next_value_id
> 0);
6401 return next_value_id
++;
6404 /* Return the next unique value id for constants. */
6407 get_next_constant_value_id (void)
6409 gcc_checking_assert (next_constant_value_id
< 0);
6410 return next_constant_value_id
--;
6414 /* Compare two expressions E1 and E2 and return true if they are equal.
6415 If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6416 otherwise VN_TOP only matches VN_TOP. */
6419 expressions_equal_p (tree e1
, tree e2
, bool match_vn_top_optimistically
)
6421 /* The obvious case. */
6425 /* If either one is VN_TOP consider them equal. */
6426 if (match_vn_top_optimistically
6427 && (e1
== VN_TOP
|| e2
== VN_TOP
))
6430 /* If only one of them is null, they cannot be equal. While in general
6431 this should not happen for operations like TARGET_MEM_REF some
6432 operands are optional and an identity value we could substitute
6433 has differing semantics. */
6437 /* SSA_NAME compare pointer equal. */
6438 if (TREE_CODE (e1
) == SSA_NAME
|| TREE_CODE (e2
) == SSA_NAME
)
6441 /* Now perform the actual comparison. */
6442 if (TREE_CODE (e1
) == TREE_CODE (e2
)
6443 && operand_equal_p (e1
, e2
, OEP_PURE_SAME
))
6450 /* Return true if the nary operation NARY may trap. This is a copy
6451 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6454 vn_nary_may_trap (vn_nary_op_t nary
)
6457 tree rhs2
= NULL_TREE
;
6458 bool honor_nans
= false;
6459 bool honor_snans
= false;
6460 bool fp_operation
= false;
6461 bool honor_trapv
= false;
6465 if (TREE_CODE_CLASS (nary
->opcode
) == tcc_comparison
6466 || TREE_CODE_CLASS (nary
->opcode
) == tcc_unary
6467 || TREE_CODE_CLASS (nary
->opcode
) == tcc_binary
)
6470 fp_operation
= FLOAT_TYPE_P (type
);
6473 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
6474 honor_snans
= flag_signaling_nans
!= 0;
6476 else if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_TRAPS (type
))
6479 if (nary
->length
>= 2)
6481 ret
= operation_could_trap_helper_p (nary
->opcode
, fp_operation
,
6482 honor_trapv
, honor_nans
, honor_snans
,
6487 for (i
= 0; i
< nary
->length
; ++i
)
6488 if (tree_could_trap_p (nary
->op
[i
]))
6494 /* Return true if the reference operation REF may trap. */
6497 vn_reference_may_trap (vn_reference_t ref
)
6499 switch (ref
->operands
[0].opcode
)
6503 /* We do not handle calls. */
6506 /* And toplevel address computations never trap. */
6511 vn_reference_op_t op
;
6513 FOR_EACH_VEC_ELT (ref
->operands
, i
, op
)
6517 case WITH_SIZE_EXPR
:
6518 case TARGET_MEM_REF
:
6519 /* Always variable. */
6522 if (op
->op1
&& TREE_CODE (op
->op1
) == SSA_NAME
)
6525 case ARRAY_RANGE_REF
:
6526 if (TREE_CODE (op
->op0
) == SSA_NAME
)
6531 if (TREE_CODE (op
->op0
) != INTEGER_CST
)
6534 /* !in_array_bounds */
6535 tree domain_type
= TYPE_DOMAIN (ref
->operands
[i
+1].type
);
6540 tree max
= TYPE_MAX_VALUE (domain_type
);
6543 || TREE_CODE (min
) != INTEGER_CST
6544 || TREE_CODE (max
) != INTEGER_CST
)
6547 if (tree_int_cst_lt (op
->op0
, min
)
6548 || tree_int_cst_lt (max
, op
->op0
))
6554 /* Nothing interesting in itself, the base is separate. */
6556 /* The following are the address bases. */
6561 return tree_could_trap_p (TREE_OPERAND (op
->op0
, 0));
6569 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction
,
6570 bitmap inserted_exprs_
)
6571 : dom_walker (direction
), do_pre (inserted_exprs_
!= NULL
),
6572 el_todo (0), eliminations (0), insertions (0),
6573 inserted_exprs (inserted_exprs_
)
6575 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
6576 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
6579 eliminate_dom_walker::~eliminate_dom_walker ()
6581 BITMAP_FREE (need_eh_cleanup
);
6582 BITMAP_FREE (need_ab_cleanup
);
6585 /* Return a leader for OP that is available at the current point of the
6586 eliminate domwalk. */
6589 eliminate_dom_walker::eliminate_avail (basic_block
, tree op
)
6591 tree valnum
= VN_INFO (op
)->valnum
;
6592 if (TREE_CODE (valnum
) == SSA_NAME
)
6594 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
6596 if (avail
.length () > SSA_NAME_VERSION (valnum
))
6597 return avail
[SSA_NAME_VERSION (valnum
)];
6599 else if (is_gimple_min_invariant (valnum
))
6604 /* At the current point of the eliminate domwalk make OP available. */
6607 eliminate_dom_walker::eliminate_push_avail (basic_block
, tree op
)
6609 tree valnum
= VN_INFO (op
)->valnum
;
6610 if (TREE_CODE (valnum
) == SSA_NAME
)
6612 if (avail
.length () <= SSA_NAME_VERSION (valnum
))
6613 avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1, true);
6615 if (avail
[SSA_NAME_VERSION (valnum
)])
6616 pushop
= avail
[SSA_NAME_VERSION (valnum
)];
6617 avail_stack
.safe_push (pushop
);
6618 avail
[SSA_NAME_VERSION (valnum
)] = op
;
6622 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
6623 the leader for the expression if insertion was successful. */
6626 eliminate_dom_walker::eliminate_insert (basic_block bb
,
6627 gimple_stmt_iterator
*gsi
, tree val
)
6629 /* We can insert a sequence with a single assignment only. */
6630 gimple_seq stmts
= VN_INFO (val
)->expr
;
6631 if (!gimple_seq_singleton_p (stmts
))
6633 gassign
*stmt
= dyn_cast
<gassign
*> (gimple_seq_first_stmt (stmts
));
6635 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
6636 && gimple_assign_rhs_code (stmt
) != VIEW_CONVERT_EXPR
6637 && gimple_assign_rhs_code (stmt
) != NEGATE_EXPR
6638 && gimple_assign_rhs_code (stmt
) != BIT_FIELD_REF
6639 && (gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6640 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)))
6643 tree op
= gimple_assign_rhs1 (stmt
);
6644 if (gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
6645 || gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
6646 op
= TREE_OPERAND (op
, 0);
6647 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (bb
, op
) : op
;
6653 if (gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
6654 res
= gimple_build (&stmts
, BIT_FIELD_REF
,
6655 TREE_TYPE (val
), leader
,
6656 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1),
6657 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2));
6658 else if (gimple_assign_rhs_code (stmt
) == BIT_AND_EXPR
)
6659 res
= gimple_build (&stmts
, BIT_AND_EXPR
,
6660 TREE_TYPE (val
), leader
, gimple_assign_rhs2 (stmt
));
6662 res
= gimple_build (&stmts
, gimple_assign_rhs_code (stmt
),
6663 TREE_TYPE (val
), leader
);
6664 if (TREE_CODE (res
) != SSA_NAME
6665 || SSA_NAME_IS_DEFAULT_DEF (res
)
6666 || gimple_bb (SSA_NAME_DEF_STMT (res
)))
6668 gimple_seq_discard (stmts
);
6670 /* During propagation we have to treat SSA info conservatively
6671 and thus we can end up simplifying the inserted expression
6672 at elimination time to sth not defined in stmts. */
6673 /* But then this is a redundancy we failed to detect. Which means
6674 res now has two values. That doesn't play well with how
6675 we track availability here, so give up. */
6676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6678 if (TREE_CODE (res
) == SSA_NAME
)
6679 res
= eliminate_avail (bb
, res
);
6682 fprintf (dump_file
, "Failed to insert expression for value ");
6683 print_generic_expr (dump_file
, val
);
6684 fprintf (dump_file
, " which is really fully redundant to ");
6685 print_generic_expr (dump_file
, res
);
6686 fprintf (dump_file
, "\n");
6694 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
6695 vn_ssa_aux_t vn_info
= VN_INFO (res
);
6696 vn_info
->valnum
= val
;
6697 vn_info
->visited
= true;
6701 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6703 fprintf (dump_file
, "Inserted ");
6704 print_gimple_stmt (dump_file
, SSA_NAME_DEF_STMT (res
), 0);
6711 eliminate_dom_walker::eliminate_stmt (basic_block b
, gimple_stmt_iterator
*gsi
)
6713 tree sprime
= NULL_TREE
;
6714 gimple
*stmt
= gsi_stmt (*gsi
);
6715 tree lhs
= gimple_get_lhs (stmt
);
6716 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6717 && !gimple_has_volatile_ops (stmt
)
6718 /* See PR43491. Do not replace a global register variable when
6719 it is a the RHS of an assignment. Do replace local register
6720 variables since gcc does not guarantee a local variable will
6721 be allocated in register.
6722 ??? The fix isn't effective here. This should instead
6723 be ensured by not value-numbering them the same but treating
6724 them like volatiles? */
6725 && !(gimple_assign_single_p (stmt
)
6726 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
6727 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
6728 && is_global_var (gimple_assign_rhs1 (stmt
)))))
6730 sprime
= eliminate_avail (b
, lhs
);
6733 /* If there is no existing usable leader but SCCVN thinks
6734 it has an expression it wants to use as replacement,
6736 tree val
= VN_INFO (lhs
)->valnum
;
6737 vn_ssa_aux_t vn_info
;
6739 && TREE_CODE (val
) == SSA_NAME
6740 && (vn_info
= VN_INFO (val
), true)
6741 && vn_info
->needs_insertion
6742 && vn_info
->expr
!= NULL
6743 && (sprime
= eliminate_insert (b
, gsi
, val
)) != NULL_TREE
)
6744 eliminate_push_avail (b
, sprime
);
6747 /* If this now constitutes a copy duplicate points-to
6748 and range info appropriately. This is especially
6749 important for inserted code. See tree-ssa-copy.cc
6750 for similar code. */
6752 && TREE_CODE (sprime
) == SSA_NAME
)
6754 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
6755 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
6756 && SSA_NAME_PTR_INFO (lhs
)
6757 && ! SSA_NAME_PTR_INFO (sprime
))
6759 duplicate_ssa_name_ptr_info (sprime
,
6760 SSA_NAME_PTR_INFO (lhs
));
6762 reset_flow_sensitive_info (sprime
);
6764 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6765 && SSA_NAME_RANGE_INFO (lhs
)
6766 && ! SSA_NAME_RANGE_INFO (sprime
)
6768 duplicate_ssa_name_range_info (sprime
, lhs
);
6771 /* Inhibit the use of an inserted PHI on a loop header when
6772 the address of the memory reference is a simple induction
6773 variable. In other cases the vectorizer won't do anything
6774 anyway (either it's loop invariant or a complicated
6777 && TREE_CODE (sprime
) == SSA_NAME
6779 && (flag_tree_loop_vectorize
|| flag_tree_parallelize_loops
> 1)
6780 && loop_outer (b
->loop_father
)
6781 && has_zero_uses (sprime
)
6782 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
6783 && gimple_assign_load_p (stmt
))
6785 gimple
*def_stmt
= SSA_NAME_DEF_STMT (sprime
);
6786 basic_block def_bb
= gimple_bb (def_stmt
);
6787 if (gimple_code (def_stmt
) == GIMPLE_PHI
6788 && def_bb
->loop_father
->header
== def_bb
)
6790 loop_p loop
= def_bb
->loop_father
;
6794 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
6797 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
6799 && flow_bb_inside_loop_p (loop
, def_bb
)
6800 && simple_iv (loop
, loop
, op
, &iv
, true))
6808 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6810 fprintf (dump_file
, "Not replacing ");
6811 print_gimple_expr (dump_file
, stmt
, 0);
6812 fprintf (dump_file
, " with ");
6813 print_generic_expr (dump_file
, sprime
);
6814 fprintf (dump_file
, " which would add a loop"
6815 " carried dependence to loop %d\n",
6818 /* Don't keep sprime available. */
6826 /* If we can propagate the value computed for LHS into
6827 all uses don't bother doing anything with this stmt. */
6828 if (may_propagate_copy (lhs
, sprime
))
6830 /* Mark it for removal. */
6831 to_remove
.safe_push (stmt
);
6833 /* ??? Don't count copy/constant propagations. */
6834 if (gimple_assign_single_p (stmt
)
6835 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
6836 || gimple_assign_rhs1 (stmt
) == sprime
))
6839 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6841 fprintf (dump_file
, "Replaced ");
6842 print_gimple_expr (dump_file
, stmt
, 0);
6843 fprintf (dump_file
, " with ");
6844 print_generic_expr (dump_file
, sprime
);
6845 fprintf (dump_file
, " in all uses of ");
6846 print_gimple_stmt (dump_file
, stmt
, 0);
6853 /* If this is an assignment from our leader (which
6854 happens in the case the value-number is a constant)
6855 then there is nothing to do. Likewise if we run into
6856 inserted code that needed a conversion because of
6857 our type-agnostic value-numbering of loads. */
6858 if ((gimple_assign_single_p (stmt
)
6859 || (is_gimple_assign (stmt
)
6860 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
6861 || gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
)))
6862 && sprime
== gimple_assign_rhs1 (stmt
))
6865 /* Else replace its RHS. */
6866 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6868 fprintf (dump_file
, "Replaced ");
6869 print_gimple_expr (dump_file
, stmt
, 0);
6870 fprintf (dump_file
, " with ");
6871 print_generic_expr (dump_file
, sprime
);
6872 fprintf (dump_file
, " in ");
6873 print_gimple_stmt (dump_file
, stmt
, 0);
6877 bool can_make_abnormal_goto
= (is_gimple_call (stmt
)
6878 && stmt_can_make_abnormal_goto (stmt
));
6879 gimple
*orig_stmt
= stmt
;
6880 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
6881 TREE_TYPE (sprime
)))
6883 /* We preserve conversions to but not from function or method
6884 types. This asymmetry makes it necessary to re-instantiate
6885 conversions here. */
6886 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
6887 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs
))))
6888 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
6892 tree vdef
= gimple_vdef (stmt
);
6893 tree vuse
= gimple_vuse (stmt
);
6894 propagate_tree_value_into_stmt (gsi
, sprime
);
6895 stmt
= gsi_stmt (*gsi
);
6897 /* In case the VDEF on the original stmt was released, value-number
6898 it to the VUSE. This is to make vuse_ssa_val able to skip
6899 released virtual operands. */
6900 if (vdef
!= gimple_vdef (stmt
))
6902 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef
));
6903 VN_INFO (vdef
)->valnum
= vuse
;
6906 /* If we removed EH side-effects from the statement, clean
6907 its EH information. */
6908 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
6910 bitmap_set_bit (need_eh_cleanup
,
6911 gimple_bb (stmt
)->index
);
6912 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6913 fprintf (dump_file
, " Removed EH side-effects.\n");
6916 /* Likewise for AB side-effects. */
6917 if (can_make_abnormal_goto
6918 && !stmt_can_make_abnormal_goto (stmt
))
6920 bitmap_set_bit (need_ab_cleanup
,
6921 gimple_bb (stmt
)->index
);
6922 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6923 fprintf (dump_file
, " Removed AB side-effects.\n");
6930 /* If the statement is a scalar store, see if the expression
6931 has the same value number as its rhs. If so, the store is
6933 if (gimple_assign_single_p (stmt
)
6934 && !gimple_has_volatile_ops (stmt
)
6935 && !is_gimple_reg (gimple_assign_lhs (stmt
))
6936 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
6937 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
6939 tree rhs
= gimple_assign_rhs1 (stmt
);
6940 vn_reference_t vnresult
;
6941 /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
6942 typed load of a byte known to be 0x11 as 1 so a store of
6943 a boolean 1 is detected as redundant. Because of this we
6944 have to make sure to lookup with a ref where its size
6945 matches the precision. */
6946 tree lookup_lhs
= lhs
;
6947 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6948 && (TREE_CODE (lhs
) != COMPONENT_REF
6949 || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs
, 1)))
6950 && !type_has_mode_precision_p (TREE_TYPE (lhs
)))
6952 if (TREE_CODE (lhs
) == COMPONENT_REF
6953 || TREE_CODE (lhs
) == MEM_REF
)
6955 tree ltype
= build_nonstandard_integer_type
6956 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs
))),
6957 TYPE_UNSIGNED (TREE_TYPE (lhs
)));
6958 if (TREE_CODE (lhs
) == COMPONENT_REF
)
6960 tree foff
= component_ref_field_offset (lhs
);
6961 tree f
= TREE_OPERAND (lhs
, 1);
6962 if (!poly_int_tree_p (foff
))
6963 lookup_lhs
= NULL_TREE
;
6965 lookup_lhs
= build3 (BIT_FIELD_REF
, ltype
,
6966 TREE_OPERAND (lhs
, 0),
6967 TYPE_SIZE (TREE_TYPE (lhs
)),
6969 (foff
, DECL_FIELD_BIT_OFFSET (f
)));
6972 lookup_lhs
= build2 (MEM_REF
, ltype
,
6973 TREE_OPERAND (lhs
, 0),
6974 TREE_OPERAND (lhs
, 1));
6977 lookup_lhs
= NULL_TREE
;
6979 tree val
= NULL_TREE
;
6981 val
= vn_reference_lookup (lookup_lhs
, gimple_vuse (stmt
),
6982 VN_WALKREWRITE
, &vnresult
, false,
6983 NULL
, NULL_TREE
, true);
6984 if (TREE_CODE (rhs
) == SSA_NAME
)
6985 rhs
= VN_INFO (rhs
)->valnum
;
6987 && (operand_equal_p (val
, rhs
, 0)
6988 /* Due to the bitfield lookups above we can get bit
6989 interpretations of the same RHS as values here. Those
6990 are redundant as well. */
6991 || (TREE_CODE (val
) == SSA_NAME
6992 && gimple_assign_single_p (SSA_NAME_DEF_STMT (val
))
6993 && (val
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val
)))
6994 && TREE_CODE (val
) == VIEW_CONVERT_EXPR
6995 && TREE_OPERAND (val
, 0) == rhs
)))
6997 /* We can only remove the later store if the former aliases
6998 at least all accesses the later one does or if the store
6999 was to readonly memory storing the same value. */
7001 ao_ref_init (&lhs_ref
, lhs
);
7002 alias_set_type set
= ao_ref_alias_set (&lhs_ref
);
7003 alias_set_type base_set
= ao_ref_base_alias_set (&lhs_ref
);
7005 || ((vnresult
->set
== set
7006 || alias_set_subset_of (set
, vnresult
->set
))
7007 && (vnresult
->base_set
== base_set
7008 || alias_set_subset_of (base_set
, vnresult
->base_set
))))
7010 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7012 fprintf (dump_file
, "Deleted redundant store ");
7013 print_gimple_stmt (dump_file
, stmt
, 0);
7016 /* Queue stmt for removal. */
7017 to_remove
.safe_push (stmt
);
7023 /* If this is a control statement value numbering left edges
7024 unexecuted on force the condition in a way consistent with
7026 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
7028 if ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
)
7029 ^ (EDGE_SUCC (b
, 1)->flags
& EDGE_EXECUTABLE
))
7031 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7033 fprintf (dump_file
, "Removing unexecutable edge from ");
7034 print_gimple_stmt (dump_file
, stmt
, 0);
7036 if (((EDGE_SUCC (b
, 0)->flags
& EDGE_TRUE_VALUE
) != 0)
7037 == ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
) != 0))
7038 gimple_cond_make_true (cond
);
7040 gimple_cond_make_false (cond
);
7042 el_todo
|= TODO_cleanup_cfg
;
7047 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
7048 bool was_noreturn
= (is_gimple_call (stmt
)
7049 && gimple_call_noreturn_p (stmt
));
7050 tree vdef
= gimple_vdef (stmt
);
7051 tree vuse
= gimple_vuse (stmt
);
7053 /* If we didn't replace the whole stmt (or propagate the result
7054 into all uses), replace all uses on this stmt with their
7056 bool modified
= false;
7057 use_operand_p use_p
;
7059 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
7061 tree use
= USE_FROM_PTR (use_p
);
7062 /* ??? The call code above leaves stmt operands un-updated. */
7063 if (TREE_CODE (use
) != SSA_NAME
)
7066 if (SSA_NAME_IS_DEFAULT_DEF (use
))
7067 /* ??? For default defs BB shouldn't matter, but we have to
7068 solve the inconsistency between rpo eliminate and
7069 dom eliminate avail valueization first. */
7070 sprime
= eliminate_avail (b
, use
);
7072 /* Look for sth available at the definition block of the argument.
7073 This avoids inconsistencies between availability there which
7074 decides if the stmt can be removed and availability at the
7075 use site. The SSA property ensures that things available
7076 at the definition are also available at uses. */
7077 sprime
= eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use
)), use
);
7078 if (sprime
&& sprime
!= use
7079 && may_propagate_copy (use
, sprime
, true)
7080 /* We substitute into debug stmts to avoid excessive
7081 debug temporaries created by removed stmts, but we need
7082 to avoid doing so for inserted sprimes as we never want
7083 to create debug temporaries for them. */
7085 || TREE_CODE (sprime
) != SSA_NAME
7086 || !is_gimple_debug (stmt
)
7087 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
7089 propagate_value (use_p
, sprime
);
7094 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7095 into which is a requirement for the IPA devirt machinery. */
7096 gimple
*old_stmt
= stmt
;
7099 /* If a formerly non-invariant ADDR_EXPR is turned into an
7100 invariant one it was on a separate stmt. */
7101 if (gimple_assign_single_p (stmt
)
7102 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
7103 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
7104 gimple_stmt_iterator prev
= *gsi
;
7106 if (fold_stmt (gsi
, follow_all_ssa_edges
))
7108 /* fold_stmt may have created new stmts inbetween
7109 the previous stmt and the folded stmt. Mark
7110 all defs created there as varying to not confuse
7111 the SCCVN machinery as we're using that even during
7113 if (gsi_end_p (prev
))
7114 prev
= gsi_start_bb (b
);
7117 if (gsi_stmt (prev
) != gsi_stmt (*gsi
))
7122 FOR_EACH_SSA_TREE_OPERAND (def
, gsi_stmt (prev
),
7123 dit
, SSA_OP_ALL_DEFS
)
7124 /* As existing DEFs may move between stmts
7125 only process new ones. */
7126 if (! has_VN_INFO (def
))
7128 vn_ssa_aux_t vn_info
= VN_INFO (def
);
7129 vn_info
->valnum
= def
;
7130 vn_info
->visited
= true;
7132 if (gsi_stmt (prev
) == gsi_stmt (*gsi
))
7138 stmt
= gsi_stmt (*gsi
);
7139 /* In case we folded the stmt away schedule the NOP for removal. */
7140 if (gimple_nop_p (stmt
))
7141 to_remove
.safe_push (stmt
);
7144 /* Visit indirect calls and turn them into direct calls if
7145 possible using the devirtualization machinery. Do this before
7146 checking for required EH/abnormal/noreturn cleanup as devird
7147 may expose more of those. */
7148 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
7150 tree fn
= gimple_call_fn (call_stmt
);
7152 && flag_devirtualize
7153 && virtual_method_call_p (fn
))
7155 tree otr_type
= obj_type_ref_class (fn
);
7156 unsigned HOST_WIDE_INT otr_tok
7157 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn
));
7159 ipa_polymorphic_call_context
context (current_function_decl
,
7160 fn
, stmt
, &instance
);
7161 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
),
7162 otr_type
, stmt
, NULL
);
7164 vec
<cgraph_node
*> targets
7165 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
7166 otr_tok
, context
, &final
);
7168 dump_possible_polymorphic_call_targets (dump_file
,
7169 obj_type_ref_class (fn
),
7171 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
7174 if (targets
.length () == 1)
7175 fn
= targets
[0]->decl
;
7177 fn
= builtin_decl_unreachable ();
7178 if (dump_enabled_p ())
7180 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
7181 "converting indirect call to "
7183 lang_hooks
.decl_printable_name (fn
, 2));
7185 gimple_call_set_fndecl (call_stmt
, fn
);
7186 /* If changing the call to __builtin_unreachable
7187 or similar noreturn function, adjust gimple_call_fntype
7189 if (gimple_call_noreturn_p (call_stmt
)
7190 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn
)))
7191 && TYPE_ARG_TYPES (TREE_TYPE (fn
))
7192 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn
)))
7194 gimple_call_set_fntype (call_stmt
, TREE_TYPE (fn
));
7195 maybe_remove_unused_call_args (cfun
, call_stmt
);
7203 /* When changing a call into a noreturn call, cfg cleanup
7204 is needed to fix up the noreturn call. */
7206 && is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
))
7207 to_fixup
.safe_push (stmt
);
7208 /* When changing a condition or switch into one we know what
7209 edge will be executed, schedule a cfg cleanup. */
7210 if ((gimple_code (stmt
) == GIMPLE_COND
7211 && (gimple_cond_true_p (as_a
<gcond
*> (stmt
))
7212 || gimple_cond_false_p (as_a
<gcond
*> (stmt
))))
7213 || (gimple_code (stmt
) == GIMPLE_SWITCH
7214 && TREE_CODE (gimple_switch_index
7215 (as_a
<gswitch
*> (stmt
))) == INTEGER_CST
))
7216 el_todo
|= TODO_cleanup_cfg
;
7217 /* If we removed EH side-effects from the statement, clean
7218 its EH information. */
7219 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
7221 bitmap_set_bit (need_eh_cleanup
,
7222 gimple_bb (stmt
)->index
);
7223 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7224 fprintf (dump_file
, " Removed EH side-effects.\n");
7226 /* Likewise for AB side-effects. */
7227 if (can_make_abnormal_goto
7228 && !stmt_can_make_abnormal_goto (stmt
))
7230 bitmap_set_bit (need_ab_cleanup
,
7231 gimple_bb (stmt
)->index
);
7232 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7233 fprintf (dump_file
, " Removed AB side-effects.\n");
7236 /* In case the VDEF on the original stmt was released, value-number
7237 it to the VUSE. This is to make vuse_ssa_val able to skip
7238 released virtual operands. */
7239 if (vdef
&& SSA_NAME_IN_FREE_LIST (vdef
))
7240 VN_INFO (vdef
)->valnum
= vuse
;
7243 /* Make new values available - for fully redundant LHS we
7244 continue with the next stmt above and skip this.
7245 But avoid picking up dead defs. */
7247 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7248 if (! has_zero_uses (def
)
7250 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (def
))))
7251 eliminate_push_avail (b
, def
);
7254 /* Perform elimination for the basic-block B during the domwalk. */
7257 eliminate_dom_walker::before_dom_children (basic_block b
)
7260 avail_stack
.safe_push (NULL_TREE
);
7262 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7263 if (!(b
->flags
& BB_EXECUTABLE
))
7268 for (gphi_iterator gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
7270 gphi
*phi
= gsi
.phi ();
7271 tree res
= PHI_RESULT (phi
);
7273 if (virtual_operand_p (res
))
7279 tree sprime
= eliminate_avail (b
, res
);
7283 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7285 fprintf (dump_file
, "Replaced redundant PHI node defining ");
7286 print_generic_expr (dump_file
, res
);
7287 fprintf (dump_file
, " with ");
7288 print_generic_expr (dump_file
, sprime
);
7289 fprintf (dump_file
, "\n");
7292 /* If we inserted this PHI node ourself, it's not an elimination. */
7293 if (! inserted_exprs
7294 || ! bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
7297 /* If we will propagate into all uses don't bother to do
7299 if (may_propagate_copy (res
, sprime
))
7301 /* Mark the PHI for removal. */
7302 to_remove
.safe_push (phi
);
7307 remove_phi_node (&gsi
, false);
7309 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
7310 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
7311 gimple
*stmt
= gimple_build_assign (res
, sprime
);
7312 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
7313 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
7317 eliminate_push_avail (b
, res
);
7321 for (gimple_stmt_iterator gsi
= gsi_start_bb (b
);
7324 eliminate_stmt (b
, &gsi
);
7326 /* Replace destination PHI arguments. */
7329 FOR_EACH_EDGE (e
, ei
, b
->succs
)
7330 if (e
->flags
& EDGE_EXECUTABLE
)
7331 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
7335 gphi
*phi
= gsi
.phi ();
7336 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
7337 tree arg
= USE_FROM_PTR (use_p
);
7338 if (TREE_CODE (arg
) != SSA_NAME
7339 || virtual_operand_p (arg
))
7341 tree sprime
= eliminate_avail (b
, arg
);
7342 if (sprime
&& may_propagate_copy (arg
, sprime
))
7343 propagate_value (use_p
, sprime
);
7346 vn_context_bb
= NULL
;
7351 /* Make no longer available leaders no longer available. */
7354 eliminate_dom_walker::after_dom_children (basic_block
)
7357 while ((entry
= avail_stack
.pop ()) != NULL_TREE
)
7359 tree valnum
= VN_INFO (entry
)->valnum
;
7360 tree old
= avail
[SSA_NAME_VERSION (valnum
)];
7362 avail
[SSA_NAME_VERSION (valnum
)] = NULL_TREE
;
7364 avail
[SSA_NAME_VERSION (valnum
)] = entry
;
7368 /* Remove queued stmts and perform delayed cleanups. */
7371 eliminate_dom_walker::eliminate_cleanup (bool region_p
)
7373 statistics_counter_event (cfun
, "Eliminated", eliminations
);
7374 statistics_counter_event (cfun
, "Insertions", insertions
);
7376 /* We cannot remove stmts during BB walk, especially not release SSA
7377 names there as this confuses the VN machinery. The stmts ending
7378 up in to_remove are either stores or simple copies.
7379 Remove stmts in reverse order to make debug stmt creation possible. */
7380 while (!to_remove
.is_empty ())
7382 bool do_release_defs
= true;
7383 gimple
*stmt
= to_remove
.pop ();
7385 /* When we are value-numbering a region we do not require exit PHIs to
7386 be present so we have to make sure to deal with uses outside of the
7387 region of stmts that we thought are eliminated.
7388 ??? Note we may be confused by uses in dead regions we didn't run
7389 elimination on. Rather than checking individual uses we accept
7390 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7391 contains such example). */
7394 if (gphi
*phi
= dyn_cast
<gphi
*> (stmt
))
7396 tree lhs
= gimple_phi_result (phi
);
7397 if (!has_zero_uses (lhs
))
7399 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7400 fprintf (dump_file
, "Keeping eliminated stmt live "
7401 "as copy because of out-of-region uses\n");
7402 tree sprime
= eliminate_avail (gimple_bb (stmt
), lhs
);
7403 gimple
*copy
= gimple_build_assign (lhs
, sprime
);
7404 gimple_stmt_iterator gsi
7405 = gsi_after_labels (gimple_bb (stmt
));
7406 gsi_insert_before (&gsi
, copy
, GSI_SAME_STMT
);
7407 do_release_defs
= false;
7410 else if (tree lhs
= gimple_get_lhs (stmt
))
7411 if (TREE_CODE (lhs
) == SSA_NAME
7412 && !has_zero_uses (lhs
))
7414 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7415 fprintf (dump_file
, "Keeping eliminated stmt live "
7416 "as copy because of out-of-region uses\n");
7417 tree sprime
= eliminate_avail (gimple_bb (stmt
), lhs
);
7418 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
7419 if (is_gimple_assign (stmt
))
7421 gimple_assign_set_rhs_from_tree (&gsi
, sprime
);
7422 stmt
= gsi_stmt (gsi
);
7424 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
))
7425 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
7430 gimple
*copy
= gimple_build_assign (lhs
, sprime
);
7431 gsi_insert_before (&gsi
, copy
, GSI_SAME_STMT
);
7432 do_release_defs
= false;
7437 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7439 fprintf (dump_file
, "Removing dead stmt ");
7440 print_gimple_stmt (dump_file
, stmt
, 0, TDF_NONE
);
7443 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
7444 if (gimple_code (stmt
) == GIMPLE_PHI
)
7445 remove_phi_node (&gsi
, do_release_defs
);
7448 basic_block bb
= gimple_bb (stmt
);
7449 unlink_stmt_vdef (stmt
);
7450 if (gsi_remove (&gsi
, true))
7451 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
7452 if (is_gimple_call (stmt
) && stmt_can_make_abnormal_goto (stmt
))
7453 bitmap_set_bit (need_ab_cleanup
, bb
->index
);
7454 if (do_release_defs
)
7455 release_defs (stmt
);
7458 /* Removing a stmt may expose a forwarder block. */
7459 el_todo
|= TODO_cleanup_cfg
;
7462 /* Fixup stmts that became noreturn calls. This may require splitting
7463 blocks and thus isn't possible during the dominator walk. Do this
7464 in reverse order so we don't inadvertedly remove a stmt we want to
7465 fixup by visiting a dominating now noreturn call first. */
7466 while (!to_fixup
.is_empty ())
7468 gimple
*stmt
= to_fixup
.pop ();
7470 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7472 fprintf (dump_file
, "Fixing up noreturn call ");
7473 print_gimple_stmt (dump_file
, stmt
, 0);
7476 if (fixup_noreturn_call (stmt
))
7477 el_todo
|= TODO_cleanup_cfg
;
7480 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
7481 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
7484 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
7487 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
7489 if (do_eh_cleanup
|| do_ab_cleanup
)
7490 el_todo
|= TODO_cleanup_cfg
;
7495 /* Eliminate fully redundant computations. */
7498 eliminate_with_rpo_vn (bitmap inserted_exprs
)
7500 eliminate_dom_walker
walker (CDI_DOMINATORS
, inserted_exprs
);
7502 eliminate_dom_walker
*saved_rpo_avail
= rpo_avail
;
7503 rpo_avail
= &walker
;
7504 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
7505 rpo_avail
= saved_rpo_avail
;
7507 return walker
.eliminate_cleanup ();
7511 do_rpo_vn_1 (function
*fn
, edge entry
, bitmap exit_bbs
,
7512 bool iterate
, bool eliminate
, vn_lookup_kind kind
);
7515 run_rpo_vn (vn_lookup_kind kind
)
7517 do_rpo_vn_1 (cfun
, NULL
, NULL
, true, false, kind
);
7519 /* ??? Prune requirement of these. */
7520 constant_to_value_id
= new hash_table
<vn_constant_hasher
> (23);
7522 /* Initialize the value ids and prune out remaining VN_TOPs
7526 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7528 vn_ssa_aux_t info
= VN_INFO (name
);
7530 || info
->valnum
== VN_TOP
)
7531 info
->valnum
= name
;
7532 if (info
->valnum
== name
)
7533 info
->value_id
= get_next_value_id ();
7534 else if (is_gimple_min_invariant (info
->valnum
))
7535 info
->value_id
= get_or_alloc_constant_value_id (info
->valnum
);
7539 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7541 vn_ssa_aux_t info
= VN_INFO (name
);
7542 if (TREE_CODE (info
->valnum
) == SSA_NAME
7543 && info
->valnum
!= name
7544 && info
->value_id
!= VN_INFO (info
->valnum
)->value_id
)
7545 info
->value_id
= VN_INFO (info
->valnum
)->value_id
;
7548 set_hashtable_value_ids ();
7550 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7552 fprintf (dump_file
, "Value numbers:\n");
7553 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7555 if (VN_INFO (name
)->visited
7556 && SSA_VAL (name
) != name
)
7558 print_generic_expr (dump_file
, name
);
7559 fprintf (dump_file
, " = ");
7560 print_generic_expr (dump_file
, SSA_VAL (name
));
7561 fprintf (dump_file
, " (%04d)\n", VN_INFO (name
)->value_id
);
7567 /* Free VN associated data structures. */
7572 free_vn_table (valid_info
);
7573 XDELETE (valid_info
);
7574 obstack_free (&vn_tables_obstack
, NULL
);
7575 obstack_free (&vn_tables_insert_obstack
, NULL
);
7577 vn_ssa_aux_iterator_type it
;
7579 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash
, info
, vn_ssa_aux_t
, it
)
7580 if (info
->needs_insertion
)
7581 release_ssa_name (info
->name
);
7582 obstack_free (&vn_ssa_aux_obstack
, NULL
);
7583 delete vn_ssa_aux_hash
;
7585 delete constant_to_value_id
;
7586 constant_to_value_id
= NULL
;
7589 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7592 vn_lookup_simplify_result (gimple_match_op
*res_op
)
7594 if (!res_op
->code
.is_tree_code ())
7596 tree
*ops
= res_op
->ops
;
7597 unsigned int length
= res_op
->num_ops
;
7598 if (res_op
->code
== CONSTRUCTOR
7599 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7600 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7601 && TREE_CODE (res_op
->ops
[0]) == CONSTRUCTOR
)
7603 length
= CONSTRUCTOR_NELTS (res_op
->ops
[0]);
7604 ops
= XALLOCAVEC (tree
, length
);
7605 for (unsigned i
= 0; i
< length
; ++i
)
7606 ops
[i
] = CONSTRUCTOR_ELT (res_op
->ops
[0], i
)->value
;
7608 vn_nary_op_t vnresult
= NULL
;
7609 tree res
= vn_nary_op_lookup_pieces (length
, (tree_code
) res_op
->code
,
7610 res_op
->type
, ops
, &vnresult
);
7611 /* If this is used from expression simplification make sure to
7612 return an available expression. */
7613 if (res
&& TREE_CODE (res
) == SSA_NAME
&& mprts_hook
&& rpo_avail
)
7614 res
= rpo_avail
->eliminate_avail (vn_context_bb
, res
);
7618 /* Return a leader for OPs value that is valid at BB. */
7621 rpo_elim::eliminate_avail (basic_block bb
, tree op
)
7624 tree valnum
= SSA_VAL (op
, &visited
);
7625 /* If we didn't visit OP then it must be defined outside of the
7626 region we process and also dominate it. So it is available. */
7629 if (TREE_CODE (valnum
) == SSA_NAME
)
7631 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
7633 vn_avail
*av
= VN_INFO (valnum
)->avail
;
7636 if (av
->location
== bb
->index
)
7637 /* On tramp3d 90% of the cases are here. */
7638 return ssa_name (av
->leader
);
7641 basic_block abb
= BASIC_BLOCK_FOR_FN (cfun
, av
->location
);
7642 /* ??? During elimination we have to use availability at the
7643 definition site of a use we try to replace. This
7644 is required to not run into inconsistencies because
7645 of dominated_by_p_w_unex behavior and removing a definition
7646 while not replacing all uses.
7647 ??? We could try to consistently walk dominators
7648 ignoring non-executable regions. The nearest common
7649 dominator of bb and abb is where we can stop walking. We
7650 may also be able to "pre-compute" (bits of) the next immediate
7651 (non-)dominator during the RPO walk when marking edges as
7653 if (dominated_by_p_w_unex (bb
, abb
, true))
7655 tree leader
= ssa_name (av
->leader
);
7656 /* Prevent eliminations that break loop-closed SSA. */
7657 if (loops_state_satisfies_p (LOOP_CLOSED_SSA
)
7658 && ! SSA_NAME_IS_DEFAULT_DEF (leader
)
7659 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7660 (leader
))->loop_father
,
7663 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7665 print_generic_expr (dump_file
, leader
);
7666 fprintf (dump_file
, " is available for ");
7667 print_generic_expr (dump_file
, valnum
);
7668 fprintf (dump_file
, "\n");
7670 /* On tramp3d 99% of the _remaining_ cases succeed at
7674 /* ??? Can we somehow skip to the immediate dominator
7675 RPO index (bb_to_rpo)? Again, maybe not worth, on
7676 tramp3d the worst number of elements in the vector is 9. */
7681 else if (valnum
!= VN_TOP
)
7682 /* valnum is is_gimple_min_invariant. */
7687 /* Make LEADER a leader for its value at BB. */
7690 rpo_elim::eliminate_push_avail (basic_block bb
, tree leader
)
7692 tree valnum
= VN_INFO (leader
)->valnum
;
7693 if (valnum
== VN_TOP
7694 || is_gimple_min_invariant (valnum
))
7696 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7698 fprintf (dump_file
, "Making available beyond BB%d ", bb
->index
);
7699 print_generic_expr (dump_file
, leader
);
7700 fprintf (dump_file
, " for value ");
7701 print_generic_expr (dump_file
, valnum
);
7702 fprintf (dump_file
, "\n");
7704 vn_ssa_aux_t value
= VN_INFO (valnum
);
7706 if (m_avail_freelist
)
7708 av
= m_avail_freelist
;
7709 m_avail_freelist
= m_avail_freelist
->next
;
7712 av
= XOBNEW (&vn_ssa_aux_obstack
, vn_avail
);
7713 av
->location
= bb
->index
;
7714 av
->leader
= SSA_NAME_VERSION (leader
);
7715 av
->next
= value
->avail
;
7716 av
->next_undo
= last_pushed_avail
;
7717 last_pushed_avail
= value
;
7721 /* Valueization hook for RPO VN plus required state. */
7724 rpo_vn_valueize (tree name
)
7726 if (TREE_CODE (name
) == SSA_NAME
)
7728 vn_ssa_aux_t val
= VN_INFO (name
);
7731 tree tem
= val
->valnum
;
7732 if (tem
!= VN_TOP
&& tem
!= name
)
7734 if (TREE_CODE (tem
) != SSA_NAME
)
7736 /* For all values we only valueize to an available leader
7737 which means we can use SSA name info without restriction. */
7738 tem
= rpo_avail
->eliminate_avail (vn_context_bb
, tem
);
7747 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7748 inverted condition. */
7751 insert_related_predicates_on_edge (enum tree_code code
, tree
*ops
, edge pred_e
)
7756 /* a < b -> a {!,<}= b */
7757 vn_nary_op_insert_pieces_predicated (2, NE_EXPR
, boolean_type_node
,
7758 ops
, boolean_true_node
, 0, pred_e
);
7759 vn_nary_op_insert_pieces_predicated (2, LE_EXPR
, boolean_type_node
,
7760 ops
, boolean_true_node
, 0, pred_e
);
7761 /* a < b -> ! a {>,=} b */
7762 vn_nary_op_insert_pieces_predicated (2, GT_EXPR
, boolean_type_node
,
7763 ops
, boolean_false_node
, 0, pred_e
);
7764 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR
, boolean_type_node
,
7765 ops
, boolean_false_node
, 0, pred_e
);
7768 /* a > b -> a {!,>}= b */
7769 vn_nary_op_insert_pieces_predicated (2, NE_EXPR
, boolean_type_node
,
7770 ops
, boolean_true_node
, 0, pred_e
);
7771 vn_nary_op_insert_pieces_predicated (2, GE_EXPR
, boolean_type_node
,
7772 ops
, boolean_true_node
, 0, pred_e
);
7773 /* a > b -> ! a {<,=} b */
7774 vn_nary_op_insert_pieces_predicated (2, LT_EXPR
, boolean_type_node
,
7775 ops
, boolean_false_node
, 0, pred_e
);
7776 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR
, boolean_type_node
,
7777 ops
, boolean_false_node
, 0, pred_e
);
7780 /* a == b -> ! a {<,>} b */
7781 vn_nary_op_insert_pieces_predicated (2, LT_EXPR
, boolean_type_node
,
7782 ops
, boolean_false_node
, 0, pred_e
);
7783 vn_nary_op_insert_pieces_predicated (2, GT_EXPR
, boolean_type_node
,
7784 ops
, boolean_false_node
, 0, pred_e
);
7789 /* Nothing besides inverted condition. */
7795 /* Main stmt worker for RPO VN, process BB. */
7798 process_bb (rpo_elim
&avail
, basic_block bb
,
7799 bool bb_visited
, bool iterate_phis
, bool iterate
, bool eliminate
,
7800 bool do_region
, bitmap exit_bbs
, bool skip_phis
)
7808 /* If we are in loop-closed SSA preserve this state. This is
7809 relevant when called on regions from outside of FRE/PRE. */
7810 bool lc_phi_nodes
= false;
7812 && loops_state_satisfies_p (LOOP_CLOSED_SSA
))
7813 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
7814 if (e
->src
->loop_father
!= e
->dest
->loop_father
7815 && flow_loop_nested_p (e
->dest
->loop_father
,
7816 e
->src
->loop_father
))
7818 lc_phi_nodes
= true;
7822 /* When we visit a loop header substitute into loop info. */
7823 if (!iterate
&& eliminate
&& bb
->loop_father
->header
== bb
)
7825 /* Keep fields in sync with substitute_in_loop_info. */
7826 if (bb
->loop_father
->nb_iterations
)
7827 bb
->loop_father
->nb_iterations
7828 = simplify_replace_tree (bb
->loop_father
->nb_iterations
,
7829 NULL_TREE
, NULL_TREE
, &vn_valueize_for_srt
);
7832 /* Value-number all defs in the basic-block. */
7834 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7837 gphi
*phi
= gsi
.phi ();
7838 tree res
= PHI_RESULT (phi
);
7839 vn_ssa_aux_t res_info
= VN_INFO (res
);
7842 gcc_assert (!res_info
->visited
);
7843 res_info
->valnum
= VN_TOP
;
7844 res_info
->visited
= true;
7847 /* When not iterating force backedge values to varying. */
7848 visit_stmt (phi
, !iterate_phis
);
7849 if (virtual_operand_p (res
))
7853 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
7854 how we handle backedges and availability.
7855 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
7856 tree val
= res_info
->valnum
;
7857 if (res
!= val
&& !iterate
&& eliminate
)
7859 if (tree leader
= avail
.eliminate_avail (bb
, res
))
7862 /* Preserve loop-closed SSA form. */
7864 || is_gimple_min_invariant (leader
)))
7866 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7868 fprintf (dump_file
, "Replaced redundant PHI node "
7870 print_generic_expr (dump_file
, res
);
7871 fprintf (dump_file
, " with ");
7872 print_generic_expr (dump_file
, leader
);
7873 fprintf (dump_file
, "\n");
7875 avail
.eliminations
++;
7877 if (may_propagate_copy (res
, leader
))
7879 /* Schedule for removal. */
7880 avail
.to_remove
.safe_push (phi
);
7883 /* ??? Else generate a copy stmt. */
7887 /* Only make defs available that not already are. But make
7888 sure loop-closed SSA PHI node defs are picked up for
7892 || ! avail
.eliminate_avail (bb
, res
))
7893 avail
.eliminate_push_avail (bb
, res
);
7896 /* For empty BBs mark outgoing edges executable. For non-empty BBs
7897 we do this when processing the last stmt as we have to do this
7898 before elimination which otherwise forces GIMPLE_CONDs to
7899 if (1 != 0) style when seeing non-executable edges. */
7900 if (gsi_end_p (gsi_start_bb (bb
)))
7902 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
7904 if (!(e
->flags
& EDGE_EXECUTABLE
))
7906 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7908 "marking outgoing edge %d -> %d executable\n",
7909 e
->src
->index
, e
->dest
->index
);
7910 e
->flags
|= EDGE_EXECUTABLE
;
7911 e
->dest
->flags
|= BB_EXECUTABLE
;
7913 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
7915 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7917 "marking destination block %d reachable\n",
7919 e
->dest
->flags
|= BB_EXECUTABLE
;
7923 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
7924 !gsi_end_p (gsi
); gsi_next (&gsi
))
7930 FOR_EACH_SSA_TREE_OPERAND (op
, gsi_stmt (gsi
), i
, SSA_OP_ALL_DEFS
)
7932 vn_ssa_aux_t op_info
= VN_INFO (op
);
7933 gcc_assert (!op_info
->visited
);
7934 op_info
->valnum
= VN_TOP
;
7935 op_info
->visited
= true;
7938 /* We somehow have to deal with uses that are not defined
7939 in the processed region. Forcing unvisited uses to
7940 varying here doesn't play well with def-use following during
7941 expression simplification, so we deal with this by checking
7942 the visited flag in SSA_VAL. */
7945 visit_stmt (gsi_stmt (gsi
));
7947 gimple
*last
= gsi_stmt (gsi
);
7949 switch (gimple_code (last
))
7952 e
= find_taken_edge (bb
, vn_valueize (gimple_switch_index
7953 (as_a
<gswitch
*> (last
))));
7957 tree lhs
= vn_valueize (gimple_cond_lhs (last
));
7958 tree rhs
= vn_valueize (gimple_cond_rhs (last
));
7959 tree val
= gimple_simplify (gimple_cond_code (last
),
7960 boolean_type_node
, lhs
, rhs
,
7962 /* If the condition didn't simplfy see if we have recorded
7963 an expression from sofar taken edges. */
7964 if (! val
|| TREE_CODE (val
) != INTEGER_CST
)
7966 vn_nary_op_t vnresult
;
7970 val
= vn_nary_op_lookup_pieces (2, gimple_cond_code (last
),
7971 boolean_type_node
, ops
,
7973 /* Did we get a predicated value? */
7974 if (! val
&& vnresult
&& vnresult
->predicated_values
)
7976 val
= vn_nary_op_get_predicated_value (vnresult
, bb
);
7977 if (val
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
7979 fprintf (dump_file
, "Got predicated value ");
7980 print_generic_expr (dump_file
, val
, TDF_NONE
);
7981 fprintf (dump_file
, " for ");
7982 print_gimple_stmt (dump_file
, last
, TDF_SLIM
);
7987 e
= find_taken_edge (bb
, val
);
7990 /* If we didn't manage to compute the taken edge then
7991 push predicated expressions for the condition itself
7992 and related conditions to the hashtables. This allows
7993 simplification of redundant conditions which is
7994 important as early cleanup. */
7995 edge true_e
, false_e
;
7996 extract_true_false_edges_from_block (bb
, &true_e
, &false_e
);
7997 enum tree_code code
= gimple_cond_code (last
);
7998 enum tree_code icode
7999 = invert_tree_comparison (code
, HONOR_NANS (lhs
));
8003 if ((do_region
&& bitmap_bit_p (exit_bbs
, true_e
->dest
->index
))
8004 || !can_track_predicate_on_edge (true_e
))
8006 if ((do_region
&& bitmap_bit_p (exit_bbs
, false_e
->dest
->index
))
8007 || !can_track_predicate_on_edge (false_e
))
8010 vn_nary_op_insert_pieces_predicated
8011 (2, code
, boolean_type_node
, ops
,
8012 boolean_true_node
, 0, true_e
);
8014 vn_nary_op_insert_pieces_predicated
8015 (2, code
, boolean_type_node
, ops
,
8016 boolean_false_node
, 0, false_e
);
8017 if (icode
!= ERROR_MARK
)
8020 vn_nary_op_insert_pieces_predicated
8021 (2, icode
, boolean_type_node
, ops
,
8022 boolean_false_node
, 0, true_e
);
8024 vn_nary_op_insert_pieces_predicated
8025 (2, icode
, boolean_type_node
, ops
,
8026 boolean_true_node
, 0, false_e
);
8028 /* Relax for non-integers, inverted condition handled
8030 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
8033 insert_related_predicates_on_edge (code
, ops
, true_e
);
8035 insert_related_predicates_on_edge (icode
, ops
, false_e
);
8041 e
= find_taken_edge (bb
, vn_valueize (gimple_goto_dest (last
)));
8048 todo
= TODO_cleanup_cfg
;
8049 if (!(e
->flags
& EDGE_EXECUTABLE
))
8051 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8053 "marking known outgoing %sedge %d -> %d executable\n",
8054 e
->flags
& EDGE_DFS_BACK
? "back-" : "",
8055 e
->src
->index
, e
->dest
->index
);
8056 e
->flags
|= EDGE_EXECUTABLE
;
8057 e
->dest
->flags
|= BB_EXECUTABLE
;
8059 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
8061 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8063 "marking destination block %d reachable\n",
8065 e
->dest
->flags
|= BB_EXECUTABLE
;
8068 else if (gsi_one_before_end_p (gsi
))
8070 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8072 if (!(e
->flags
& EDGE_EXECUTABLE
))
8074 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8076 "marking outgoing edge %d -> %d executable\n",
8077 e
->src
->index
, e
->dest
->index
);
8078 e
->flags
|= EDGE_EXECUTABLE
;
8079 e
->dest
->flags
|= BB_EXECUTABLE
;
8081 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
8083 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8085 "marking destination block %d reachable\n",
8087 e
->dest
->flags
|= BB_EXECUTABLE
;
8092 /* Eliminate. That also pushes to avail. */
8093 if (eliminate
&& ! iterate
)
8094 avail
.eliminate_stmt (bb
, &gsi
);
8096 /* If not eliminating, make all not already available defs
8097 available. But avoid picking up dead defs. */
8098 FOR_EACH_SSA_TREE_OPERAND (op
, gsi_stmt (gsi
), i
, SSA_OP_DEF
)
8099 if (! has_zero_uses (op
)
8100 && ! avail
.eliminate_avail (bb
, op
))
8101 avail
.eliminate_push_avail (bb
, op
);
8104 /* Eliminate in destination PHI arguments. Always substitute in dest
8105 PHIs, even for non-executable edges. This handles region
8107 if (!iterate
&& eliminate
)
8108 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8109 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
8110 !gsi_end_p (gsi
); gsi_next (&gsi
))
8112 gphi
*phi
= gsi
.phi ();
8113 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
8114 tree arg
= USE_FROM_PTR (use_p
);
8115 if (TREE_CODE (arg
) != SSA_NAME
8116 || virtual_operand_p (arg
))
8119 if (SSA_NAME_IS_DEFAULT_DEF (arg
))
8121 sprime
= SSA_VAL (arg
);
8122 gcc_assert (TREE_CODE (sprime
) != SSA_NAME
8123 || SSA_NAME_IS_DEFAULT_DEF (sprime
));
8126 /* Look for sth available at the definition block of the argument.
8127 This avoids inconsistencies between availability there which
8128 decides if the stmt can be removed and availability at the
8129 use site. The SSA property ensures that things available
8130 at the definition are also available at uses. */
8131 sprime
= avail
.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg
)),
8135 && may_propagate_copy (arg
, sprime
))
8136 propagate_value (use_p
, sprime
);
8139 vn_context_bb
= NULL
;
8143 /* Unwind state per basic-block. */
8147 /* Times this block has been visited. */
8149 /* Whether to handle this as iteration point or whether to treat
8150 incoming backedge PHI values as varying. */
8152 /* Maximum RPO index this block is reachable from. */
8156 vn_reference_t ref_top
;
8158 vn_nary_op_t nary_top
;
8159 vn_avail
*avail_top
;
8162 /* Unwind the RPO VN state for iteration. */
8165 do_unwind (unwind_state
*to
, rpo_elim
&avail
)
8167 gcc_assert (to
->iterate
);
8168 for (; last_inserted_nary
!= to
->nary_top
;
8169 last_inserted_nary
= last_inserted_nary
->next
)
8172 slot
= valid_info
->nary
->find_slot_with_hash
8173 (last_inserted_nary
, last_inserted_nary
->hashcode
, NO_INSERT
);
8174 /* Predication causes the need to restore previous state. */
8175 if ((*slot
)->unwind_to
)
8176 *slot
= (*slot
)->unwind_to
;
8178 valid_info
->nary
->clear_slot (slot
);
8180 for (; last_inserted_phi
!= to
->phi_top
;
8181 last_inserted_phi
= last_inserted_phi
->next
)
8184 slot
= valid_info
->phis
->find_slot_with_hash
8185 (last_inserted_phi
, last_inserted_phi
->hashcode
, NO_INSERT
);
8186 valid_info
->phis
->clear_slot (slot
);
8188 for (; last_inserted_ref
!= to
->ref_top
;
8189 last_inserted_ref
= last_inserted_ref
->next
)
8191 vn_reference_t
*slot
;
8192 slot
= valid_info
->references
->find_slot_with_hash
8193 (last_inserted_ref
, last_inserted_ref
->hashcode
, NO_INSERT
);
8194 (*slot
)->operands
.release ();
8195 valid_info
->references
->clear_slot (slot
);
8197 obstack_free (&vn_tables_obstack
, to
->ob_top
);
8199 /* Prune [rpo_idx, ] from avail. */
8200 for (; last_pushed_avail
&& last_pushed_avail
->avail
!= to
->avail_top
;)
8202 vn_ssa_aux_t val
= last_pushed_avail
;
8203 vn_avail
*av
= val
->avail
;
8204 val
->avail
= av
->next
;
8205 last_pushed_avail
= av
->next_undo
;
8206 av
->next
= avail
.m_avail_freelist
;
8207 avail
.m_avail_freelist
= av
;
8211 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8212 If ITERATE is true then treat backedges optimistically as not
8213 executed and iterate. If ELIMINATE is true then perform
8214 elimination, otherwise leave that to the caller. */
8217 do_rpo_vn_1 (function
*fn
, edge entry
, bitmap exit_bbs
,
8218 bool iterate
, bool eliminate
, vn_lookup_kind kind
)
8221 default_vn_walk_kind
= kind
;
8223 /* We currently do not support region-based iteration when
8224 elimination is requested. */
8225 gcc_assert (!entry
|| !iterate
|| !eliminate
);
8226 /* When iterating we need loop info up-to-date. */
8227 gcc_assert (!iterate
|| !loops_state_satisfies_p (LOOPS_NEED_FIXUP
));
8229 bool do_region
= entry
!= NULL
;
8232 entry
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn
));
8233 exit_bbs
= BITMAP_ALLOC (NULL
);
8234 bitmap_set_bit (exit_bbs
, EXIT_BLOCK
);
8237 /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8238 re-mark those that are contained in the region. */
8241 FOR_EACH_EDGE (e
, ei
, entry
->dest
->preds
)
8242 e
->flags
&= ~EDGE_DFS_BACK
;
8244 int *rpo
= XNEWVEC (int, n_basic_blocks_for_fn (fn
) - NUM_FIXED_BLOCKS
);
8245 auto_vec
<std::pair
<int, int> > toplevel_scc_extents
;
8246 int n
= rev_post_order_and_mark_dfs_back_seme
8247 (fn
, entry
, exit_bbs
, true, rpo
, !iterate
? &toplevel_scc_extents
: NULL
);
8250 BITMAP_FREE (exit_bbs
);
8252 /* If there are any non-DFS_BACK edges into entry->dest skip
8253 processing PHI nodes for that block. This supports
8254 value-numbering loop bodies w/o the actual loop. */
8255 FOR_EACH_EDGE (e
, ei
, entry
->dest
->preds
)
8257 && !(e
->flags
& EDGE_DFS_BACK
))
8259 bool skip_entry_phis
= e
!= NULL
;
8260 if (skip_entry_phis
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
8261 fprintf (dump_file
, "Region does not contain all edges into "
8262 "the entry block, skipping its PHIs.\n");
8264 int *bb_to_rpo
= XNEWVEC (int, last_basic_block_for_fn (fn
));
8265 for (int i
= 0; i
< n
; ++i
)
8266 bb_to_rpo
[rpo
[i
]] = i
;
8268 unwind_state
*rpo_state
= XNEWVEC (unwind_state
, n
);
8270 rpo_elim
avail (entry
->dest
);
8273 /* Verify we have no extra entries into the region. */
8274 if (flag_checking
&& do_region
)
8276 auto_bb_flag
bb_in_region (fn
);
8277 for (int i
= 0; i
< n
; ++i
)
8279 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8280 bb
->flags
|= bb_in_region
;
8282 /* We can't merge the first two loops because we cannot rely
8283 on EDGE_DFS_BACK for edges not within the region. But if
8284 we decide to always have the bb_in_region flag we can
8285 do the checking during the RPO walk itself (but then it's
8286 also easy to handle MEME conservatively). */
8287 for (int i
= 0; i
< n
; ++i
)
8289 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8292 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8293 gcc_assert (e
== entry
8294 || (skip_entry_phis
&& bb
== entry
->dest
)
8295 || (e
->src
->flags
& bb_in_region
));
8297 for (int i
= 0; i
< n
; ++i
)
8299 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8300 bb
->flags
&= ~bb_in_region
;
8304 /* Create the VN state. For the initial size of the various hashtables
8305 use a heuristic based on region size and number of SSA names. */
8306 unsigned region_size
= (((unsigned HOST_WIDE_INT
)n
* num_ssa_names
)
8307 / (n_basic_blocks_for_fn (fn
) - NUM_FIXED_BLOCKS
));
8308 VN_TOP
= create_tmp_var_raw (void_type_node
, "vn_top");
8310 next_constant_value_id
= -1;
8312 vn_ssa_aux_hash
= new hash_table
<vn_ssa_aux_hasher
> (region_size
* 2);
8313 gcc_obstack_init (&vn_ssa_aux_obstack
);
8315 gcc_obstack_init (&vn_tables_obstack
);
8316 gcc_obstack_init (&vn_tables_insert_obstack
);
8317 valid_info
= XCNEW (struct vn_tables_s
);
8318 allocate_vn_table (valid_info
, region_size
);
8319 last_inserted_ref
= NULL
;
8320 last_inserted_phi
= NULL
;
8321 last_inserted_nary
= NULL
;
8322 last_pushed_avail
= NULL
;
8324 vn_valueize
= rpo_vn_valueize
;
8326 /* Initialize the unwind state and edge/BB executable state. */
8327 unsigned curr_scc
= 0;
8328 for (int i
= 0; i
< n
; ++i
)
8330 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8331 rpo_state
[i
].visited
= 0;
8332 rpo_state
[i
].max_rpo
= i
;
8333 if (!iterate
&& curr_scc
< toplevel_scc_extents
.length ())
8335 if (i
>= toplevel_scc_extents
[curr_scc
].first
8336 && i
<= toplevel_scc_extents
[curr_scc
].second
)
8337 rpo_state
[i
].max_rpo
= toplevel_scc_extents
[curr_scc
].second
;
8338 if (i
== toplevel_scc_extents
[curr_scc
].second
)
8341 bb
->flags
&= ~BB_EXECUTABLE
;
8342 bool has_backedges
= false;
8345 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8347 if (e
->flags
& EDGE_DFS_BACK
)
8348 has_backedges
= true;
8349 e
->flags
&= ~EDGE_EXECUTABLE
;
8350 if (iterate
|| e
== entry
|| (skip_entry_phis
&& bb
== entry
->dest
))
8353 rpo_state
[i
].iterate
= iterate
&& has_backedges
;
8355 entry
->flags
|= EDGE_EXECUTABLE
;
8356 entry
->dest
->flags
|= BB_EXECUTABLE
;
8358 /* As heuristic to improve compile-time we handle only the N innermost
8359 loops and the outermost one optimistically. */
8362 unsigned max_depth
= param_rpo_vn_max_loop_depth
;
8363 for (auto loop
: loops_list (cfun
, LI_ONLY_INNERMOST
))
8364 if (loop_depth (loop
) > max_depth
)
8365 for (unsigned i
= 2;
8366 i
< loop_depth (loop
) - max_depth
; ++i
)
8368 basic_block header
= superloop_at_depth (loop
, i
)->header
;
8369 bool non_latch_backedge
= false;
8372 FOR_EACH_EDGE (e
, ei
, header
->preds
)
8373 if (e
->flags
& EDGE_DFS_BACK
)
8375 /* There can be a non-latch backedge into the header
8376 which is part of an outer irreducible region. We
8377 cannot avoid iterating this block then. */
8378 if (!dominated_by_p (CDI_DOMINATORS
,
8381 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8382 fprintf (dump_file
, "non-latch backedge %d -> %d "
8383 "forces iteration of loop %d\n",
8384 e
->src
->index
, e
->dest
->index
, loop
->num
);
8385 non_latch_backedge
= true;
8388 e
->flags
|= EDGE_EXECUTABLE
;
8390 rpo_state
[bb_to_rpo
[header
->index
]].iterate
= non_latch_backedge
;
8397 /* Go and process all blocks, iterating as necessary. */
8400 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[idx
]);
8402 /* If the block has incoming backedges remember unwind state. This
8403 is required even for non-executable blocks since in irreducible
8404 regions we might reach them via the backedge and re-start iterating
8406 Note we can individually mark blocks with incoming backedges to
8407 not iterate where we then handle PHIs conservatively. We do that
8408 heuristically to reduce compile-time for degenerate cases. */
8409 if (rpo_state
[idx
].iterate
)
8411 rpo_state
[idx
].ob_top
= obstack_alloc (&vn_tables_obstack
, 0);
8412 rpo_state
[idx
].ref_top
= last_inserted_ref
;
8413 rpo_state
[idx
].phi_top
= last_inserted_phi
;
8414 rpo_state
[idx
].nary_top
= last_inserted_nary
;
8415 rpo_state
[idx
].avail_top
8416 = last_pushed_avail
? last_pushed_avail
->avail
: NULL
;
8419 if (!(bb
->flags
& BB_EXECUTABLE
))
8421 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8422 fprintf (dump_file
, "Block %d: BB%d found not executable\n",
8428 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8429 fprintf (dump_file
, "Processing block %d: BB%d\n", idx
, bb
->index
);
8431 todo
|= process_bb (avail
, bb
,
8432 rpo_state
[idx
].visited
!= 0,
8433 rpo_state
[idx
].iterate
,
8434 iterate
, eliminate
, do_region
, exit_bbs
, false);
8435 rpo_state
[idx
].visited
++;
8437 /* Verify if changed values flow over executable outgoing backedges
8438 and those change destination PHI values (that's the thing we
8439 can easily verify). Reduce over all such edges to the farthest
8441 int iterate_to
= -1;
8444 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8445 if ((e
->flags
& (EDGE_DFS_BACK
|EDGE_EXECUTABLE
))
8446 == (EDGE_DFS_BACK
|EDGE_EXECUTABLE
)
8447 && rpo_state
[bb_to_rpo
[e
->dest
->index
]].iterate
)
8449 int destidx
= bb_to_rpo
[e
->dest
->index
];
8450 if (!rpo_state
[destidx
].visited
)
8452 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8453 fprintf (dump_file
, "Unvisited destination %d\n",
8455 if (iterate_to
== -1 || destidx
< iterate_to
)
8456 iterate_to
= destidx
;
8459 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8460 fprintf (dump_file
, "Looking for changed values of backedge"
8461 " %d->%d destination PHIs\n",
8462 e
->src
->index
, e
->dest
->index
);
8463 vn_context_bb
= e
->dest
;
8465 for (gsi
= gsi_start_phis (e
->dest
);
8466 !gsi_end_p (gsi
); gsi_next (&gsi
))
8468 bool inserted
= false;
8469 /* While we'd ideally just iterate on value changes
8470 we CSE PHIs and do that even across basic-block
8471 boundaries. So even hashtable state changes can
8472 be important (which is roughly equivalent to
8473 PHI argument value changes). To not excessively
8474 iterate because of that we track whether a PHI
8475 was CSEd to with GF_PLF_1. */
8476 bool phival_changed
;
8477 if ((phival_changed
= visit_phi (gsi
.phi (),
8479 || (inserted
&& gimple_plf (gsi
.phi (), GF_PLF_1
)))
8482 && dump_file
&& (dump_flags
& TDF_DETAILS
))
8483 fprintf (dump_file
, "PHI was CSEd and hashtable "
8484 "state (changed)\n");
8485 if (iterate_to
== -1 || destidx
< iterate_to
)
8486 iterate_to
= destidx
;
8490 vn_context_bb
= NULL
;
8492 if (iterate_to
!= -1)
8494 do_unwind (&rpo_state
[iterate_to
], avail
);
8496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8497 fprintf (dump_file
, "Iterating to %d BB%d\n",
8498 iterate_to
, rpo
[iterate_to
]);
8508 /* Process all blocks greedily with a worklist that enforces RPO
8509 processing of reachable blocks. */
8510 auto_bitmap worklist
;
8511 bitmap_set_bit (worklist
, 0);
8512 while (!bitmap_empty_p (worklist
))
8514 int idx
= bitmap_clear_first_set_bit (worklist
);
8515 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[idx
]);
8516 gcc_assert ((bb
->flags
& BB_EXECUTABLE
)
8517 && !rpo_state
[idx
].visited
);
8519 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8520 fprintf (dump_file
, "Processing block %d: BB%d\n", idx
, bb
->index
);
8522 /* When we run into predecessor edges where we cannot trust its
8523 executable state mark them executable so PHI processing will
8525 ??? Do we need to force arguments flowing over that edge
8526 to be varying or will they even always be? */
8529 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8530 if (!(e
->flags
& EDGE_EXECUTABLE
)
8531 && (bb
== entry
->dest
8532 || (!rpo_state
[bb_to_rpo
[e
->src
->index
]].visited
8533 && (rpo_state
[bb_to_rpo
[e
->src
->index
]].max_rpo
8536 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8537 fprintf (dump_file
, "Cannot trust state of predecessor "
8538 "edge %d -> %d, marking executable\n",
8539 e
->src
->index
, e
->dest
->index
);
8540 e
->flags
|= EDGE_EXECUTABLE
;
8544 todo
|= process_bb (avail
, bb
, false, false, false, eliminate
,
8545 do_region
, exit_bbs
,
8546 skip_entry_phis
&& bb
== entry
->dest
);
8547 rpo_state
[idx
].visited
++;
8549 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8550 if ((e
->flags
& EDGE_EXECUTABLE
)
8551 && e
->dest
->index
!= EXIT_BLOCK
8552 && (!do_region
|| !bitmap_bit_p (exit_bbs
, e
->dest
->index
))
8553 && !rpo_state
[bb_to_rpo
[e
->dest
->index
]].visited
)
8554 bitmap_set_bit (worklist
, bb_to_rpo
[e
->dest
->index
]);
8558 /* If statistics or dump file active. */
8560 unsigned max_visited
= 1;
8561 for (int i
= 0; i
< n
; ++i
)
8563 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8564 if (bb
->flags
& BB_EXECUTABLE
)
8566 statistics_histogram_event (cfun
, "RPO block visited times",
8567 rpo_state
[i
].visited
);
8568 if (rpo_state
[i
].visited
> max_visited
)
8569 max_visited
= rpo_state
[i
].visited
;
8571 unsigned nvalues
= 0, navail
= 0;
8572 for (hash_table
<vn_ssa_aux_hasher
>::iterator i
= vn_ssa_aux_hash
->begin ();
8573 i
!= vn_ssa_aux_hash
->end (); ++i
)
8576 vn_avail
*av
= (*i
)->avail
;
8583 statistics_counter_event (cfun
, "RPO blocks", n
);
8584 statistics_counter_event (cfun
, "RPO blocks visited", nblk
);
8585 statistics_counter_event (cfun
, "RPO blocks executable", nex
);
8586 statistics_histogram_event (cfun
, "RPO iterations", 10*nblk
/ nex
);
8587 statistics_histogram_event (cfun
, "RPO num values", nvalues
);
8588 statistics_histogram_event (cfun
, "RPO num avail", navail
);
8589 statistics_histogram_event (cfun
, "RPO num lattice",
8590 vn_ssa_aux_hash
->elements ());
8591 if (dump_file
&& (dump_flags
& (TDF_DETAILS
|TDF_STATS
)))
8593 fprintf (dump_file
, "RPO iteration over %d blocks visited %" PRIu64
8594 " blocks in total discovering %d executable blocks iterating "
8595 "%d.%d times, a block was visited max. %u times\n",
8597 (int)((10*nblk
/ nex
)/10), (int)((10*nblk
/ nex
)%10),
8599 fprintf (dump_file
, "RPO tracked %d values available at %d locations "
8600 "and %" PRIu64
" lattice elements\n",
8601 nvalues
, navail
, (uint64_t) vn_ssa_aux_hash
->elements ());
8606 /* When !iterate we already performed elimination during the RPO
8610 /* Elimination for region-based VN needs to be done within the
8612 gcc_assert (! do_region
);
8613 /* Note we can't use avail.walk here because that gets confused
8614 by the existing availability and it will be less efficient
8616 todo
|= eliminate_with_rpo_vn (NULL
);
8619 todo
|= avail
.eliminate_cleanup (do_region
);
8625 XDELETEVEC (bb_to_rpo
);
8627 XDELETEVEC (rpo_state
);
8632 /* Region-based entry for RPO VN. Performs value-numbering and elimination
8633 on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
8634 the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
8636 If ITERATE is true then treat backedges optimistically as not
8637 executed and iterate. If ELIMINATE is true then perform
8638 elimination, otherwise leave that to the caller.
8639 KIND specifies the amount of work done for handling memory operations. */
8642 do_rpo_vn (function
*fn
, edge entry
, bitmap exit_bbs
,
8643 bool iterate
, bool eliminate
, vn_lookup_kind kind
)
8645 auto_timevar
tv (TV_TREE_RPO_VN
);
8646 unsigned todo
= do_rpo_vn_1 (fn
, entry
, exit_bbs
, iterate
, eliminate
, kind
);
8654 const pass_data pass_data_fre
=
8656 GIMPLE_PASS
, /* type */
8658 OPTGROUP_NONE
, /* optinfo_flags */
8659 TV_TREE_FRE
, /* tv_id */
8660 ( PROP_cfg
| PROP_ssa
), /* properties_required */
8661 0, /* properties_provided */
8662 0, /* properties_destroyed */
8663 0, /* todo_flags_start */
8664 0, /* todo_flags_finish */
8667 class pass_fre
: public gimple_opt_pass
8670 pass_fre (gcc::context
*ctxt
)
8671 : gimple_opt_pass (pass_data_fre
, ctxt
), may_iterate (true)
8674 /* opt_pass methods: */
8675 opt_pass
* clone () final override
{ return new pass_fre (m_ctxt
); }
8676 void set_pass_param (unsigned int n
, bool param
) final override
8678 gcc_assert (n
== 0);
8679 may_iterate
= param
;
8681 bool gate (function
*) final override
8683 return flag_tree_fre
!= 0 && (may_iterate
|| optimize
> 1);
8685 unsigned int execute (function
*) final override
;
8689 }; // class pass_fre
8692 pass_fre::execute (function
*fun
)
8696 /* At -O[1g] use the cheap non-iterating mode. */
8697 bool iterate_p
= may_iterate
&& (optimize
> 1);
8698 calculate_dominance_info (CDI_DOMINATORS
);
8700 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
8702 todo
= do_rpo_vn_1 (fun
, NULL
, NULL
, iterate_p
, true, VN_WALKREWRITE
);
8706 loop_optimizer_finalize ();
8708 if (scev_initialized_p ())
8711 /* For late FRE after IVOPTs and unrolling, see if we can
8712 remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8714 todo
|= TODO_update_address_taken
;
8722 make_pass_fre (gcc::context
*ctxt
)
8724 return new pass_fre (ctxt
);
8727 #undef BB_EXECUTABLE