1 /* SCC value numbering for trees
2 Copyright (C) 2006-2022 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
))))
807 poly_int64 off1
= 0, off2
= 0;
808 vn_reference_op_t vro1
, vro2
;
809 vn_reference_op_s tem1
, tem2
;
810 bool deref1
= false, deref2
= false;
811 bool reverse1
= false, reverse2
= false;
812 for (; vr1
->operands
.iterate (i
, &vro1
); i
++)
814 if (vro1
->opcode
== MEM_REF
)
816 /* Do not look through a storage order barrier. */
817 else if (vro1
->opcode
== VIEW_CONVERT_EXPR
&& vro1
->reverse
)
819 reverse1
|= vro1
->reverse
;
820 if (known_eq (vro1
->off
, -1))
824 for (; vr2
->operands
.iterate (j
, &vro2
); j
++)
826 if (vro2
->opcode
== MEM_REF
)
828 /* Do not look through a storage order barrier. */
829 else if (vro2
->opcode
== VIEW_CONVERT_EXPR
&& vro2
->reverse
)
831 reverse2
|= vro2
->reverse
;
832 if (known_eq (vro2
->off
, -1))
836 if (maybe_ne (off1
, off2
) || reverse1
!= reverse2
)
838 if (deref1
&& vro1
->opcode
== ADDR_EXPR
)
840 memset (&tem1
, 0, sizeof (tem1
));
841 tem1
.op0
= TREE_OPERAND (vro1
->op0
, 0);
842 tem1
.type
= TREE_TYPE (tem1
.op0
);
843 tem1
.opcode
= TREE_CODE (tem1
.op0
);
847 if (deref2
&& vro2
->opcode
== ADDR_EXPR
)
849 memset (&tem2
, 0, sizeof (tem2
));
850 tem2
.op0
= TREE_OPERAND (vro2
->op0
, 0);
851 tem2
.type
= TREE_TYPE (tem2
.op0
);
852 tem2
.opcode
= TREE_CODE (tem2
.op0
);
856 if (deref1
!= deref2
)
858 if (!vn_reference_op_eq (vro1
, vro2
))
863 while (vr1
->operands
.length () != i
864 || vr2
->operands
.length () != j
);
869 /* Copy the operations present in load/store REF into RESULT, a vector of
870 vn_reference_op_s's. */
873 copy_reference_ops_from_ref (tree ref
, vec
<vn_reference_op_s
> *result
)
875 /* For non-calls, store the information that makes up the address. */
879 vn_reference_op_s temp
;
881 memset (&temp
, 0, sizeof (temp
));
882 temp
.type
= TREE_TYPE (ref
);
883 temp
.opcode
= TREE_CODE (ref
);
889 temp
.op0
= TREE_OPERAND (ref
, 1);
892 temp
.op0
= TREE_OPERAND (ref
, 1);
896 /* The base address gets its own vn_reference_op_s structure. */
897 temp
.op0
= TREE_OPERAND (ref
, 1);
898 if (!mem_ref_offset (ref
).to_shwi (&temp
.off
))
900 temp
.clique
= MR_DEPENDENCE_CLIQUE (ref
);
901 temp
.base
= MR_DEPENDENCE_BASE (ref
);
902 temp
.reverse
= REF_REVERSE_STORAGE_ORDER (ref
);
905 /* The base address gets its own vn_reference_op_s structure. */
906 temp
.op0
= TMR_INDEX (ref
);
907 temp
.op1
= TMR_STEP (ref
);
908 temp
.op2
= TMR_OFFSET (ref
);
909 temp
.clique
= MR_DEPENDENCE_CLIQUE (ref
);
910 temp
.base
= MR_DEPENDENCE_BASE (ref
);
911 result
->safe_push (temp
);
912 memset (&temp
, 0, sizeof (temp
));
913 temp
.type
= NULL_TREE
;
914 temp
.opcode
= ERROR_MARK
;
915 temp
.op0
= TMR_INDEX2 (ref
);
919 /* Record bits, position and storage order. */
920 temp
.op0
= TREE_OPERAND (ref
, 1);
921 temp
.op1
= TREE_OPERAND (ref
, 2);
922 if (!multiple_p (bit_field_offset (ref
), BITS_PER_UNIT
, &temp
.off
))
924 temp
.reverse
= REF_REVERSE_STORAGE_ORDER (ref
);
927 /* The field decl is enough to unambiguously specify the field,
928 so use its type here. */
929 temp
.type
= TREE_TYPE (TREE_OPERAND (ref
, 1));
930 temp
.op0
= TREE_OPERAND (ref
, 1);
931 temp
.op1
= TREE_OPERAND (ref
, 2);
932 temp
.reverse
= (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref
, 0)))
933 && TYPE_REVERSE_STORAGE_ORDER
934 (TREE_TYPE (TREE_OPERAND (ref
, 0))));
936 tree this_offset
= component_ref_field_offset (ref
);
938 && poly_int_tree_p (this_offset
))
940 tree bit_offset
= DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref
, 1));
941 if (TREE_INT_CST_LOW (bit_offset
) % BITS_PER_UNIT
== 0)
944 = (wi::to_poly_offset (this_offset
)
945 + (wi::to_offset (bit_offset
) >> LOG2_BITS_PER_UNIT
));
946 /* Probibit value-numbering zero offset components
947 of addresses the same before the pass folding
948 __builtin_object_size had a chance to run. */
949 if (TREE_CODE (orig
) != ADDR_EXPR
951 || (cfun
->curr_properties
& PROP_objsz
))
952 off
.to_shwi (&temp
.off
);
957 case ARRAY_RANGE_REF
:
960 tree eltype
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref
, 0)));
961 /* Record index as operand. */
962 temp
.op0
= TREE_OPERAND (ref
, 1);
963 /* Always record lower bounds and element size. */
964 temp
.op1
= array_ref_low_bound (ref
);
965 /* But record element size in units of the type alignment. */
966 temp
.op2
= TREE_OPERAND (ref
, 3);
967 temp
.align
= eltype
->type_common
.align
;
969 temp
.op2
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (eltype
),
970 size_int (TYPE_ALIGN_UNIT (eltype
)));
971 if (poly_int_tree_p (temp
.op0
)
972 && poly_int_tree_p (temp
.op1
)
973 && TREE_CODE (temp
.op2
) == INTEGER_CST
)
975 poly_offset_int off
= ((wi::to_poly_offset (temp
.op0
)
976 - wi::to_poly_offset (temp
.op1
))
977 * wi::to_offset (temp
.op2
)
978 * vn_ref_op_align_unit (&temp
));
979 off
.to_shwi (&temp
.off
);
981 temp
.reverse
= (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref
, 0)))
982 && TYPE_REVERSE_STORAGE_ORDER
983 (TREE_TYPE (TREE_OPERAND (ref
, 0))));
987 if (DECL_HARD_REGISTER (ref
))
996 /* Canonicalize decls to MEM[&decl] which is what we end up with
997 when valueizing MEM[ptr] with ptr = &decl. */
998 temp
.opcode
= MEM_REF
;
999 temp
.op0
= build_int_cst (build_pointer_type (TREE_TYPE (ref
)), 0);
1001 result
->safe_push (temp
);
1002 temp
.opcode
= ADDR_EXPR
;
1003 temp
.op0
= build1 (ADDR_EXPR
, TREE_TYPE (temp
.op0
), ref
);
1004 temp
.type
= TREE_TYPE (temp
.op0
);
1019 if (is_gimple_min_invariant (ref
))
1025 /* These are only interesting for their operands, their
1026 existence, and their type. They will never be the last
1027 ref in the chain of references (IE they require an
1028 operand), so we don't have to put anything
1029 for op* as it will be handled by the iteration */
1033 case VIEW_CONVERT_EXPR
:
1035 temp
.reverse
= storage_order_barrier_p (ref
);
1038 /* This is only interesting for its constant offset. */
1039 temp
.off
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref
)));
1044 result
->safe_push (temp
);
1046 if (REFERENCE_CLASS_P (ref
)
1047 || TREE_CODE (ref
) == MODIFY_EXPR
1048 || TREE_CODE (ref
) == WITH_SIZE_EXPR
1049 || (TREE_CODE (ref
) == ADDR_EXPR
1050 && !is_gimple_min_invariant (ref
)))
1051 ref
= TREE_OPERAND (ref
, 0);
1057 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1058 operands in *OPS, the reference alias set SET and the reference type TYPE.
1059 Return true if something useful was produced. */
1062 ao_ref_init_from_vn_reference (ao_ref
*ref
,
1063 alias_set_type set
, alias_set_type base_set
,
1064 tree type
, const vec
<vn_reference_op_s
> &ops
)
1067 tree base
= NULL_TREE
;
1068 tree
*op0_p
= &base
;
1069 poly_offset_int offset
= 0;
1070 poly_offset_int max_size
;
1071 poly_offset_int size
= -1;
1072 tree size_tree
= NULL_TREE
;
1074 /* We don't handle calls. */
1078 machine_mode mode
= TYPE_MODE (type
);
1079 if (mode
== BLKmode
)
1080 size_tree
= TYPE_SIZE (type
);
1082 size
= GET_MODE_BITSIZE (mode
);
1083 if (size_tree
!= NULL_TREE
1084 && poly_int_tree_p (size_tree
))
1085 size
= wi::to_poly_offset (size_tree
);
1087 /* Lower the final access size from the outermost expression. */
1088 const_vn_reference_op_t cst_op
= &ops
[0];
1089 /* Cast away constness for the sake of the const-unsafe
1090 FOR_EACH_VEC_ELT(). */
1091 vn_reference_op_t op
= const_cast<vn_reference_op_t
>(cst_op
);
1092 size_tree
= NULL_TREE
;
1093 if (op
->opcode
== COMPONENT_REF
)
1094 size_tree
= DECL_SIZE (op
->op0
);
1095 else if (op
->opcode
== BIT_FIELD_REF
)
1096 size_tree
= op
->op0
;
1097 if (size_tree
!= NULL_TREE
1098 && poly_int_tree_p (size_tree
)
1099 && (!known_size_p (size
)
1100 || known_lt (wi::to_poly_offset (size_tree
), size
)))
1101 size
= wi::to_poly_offset (size_tree
);
1103 /* Initially, maxsize is the same as the accessed element size.
1104 In the following it will only grow (or become -1). */
1107 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1108 and find the ultimate containing object. */
1109 FOR_EACH_VEC_ELT (ops
, i
, op
)
1113 /* These may be in the reference ops, but we cannot do anything
1114 sensible with them here. */
1116 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1117 if (base
!= NULL_TREE
1118 && TREE_CODE (base
) == MEM_REF
1120 && DECL_P (TREE_OPERAND (op
->op0
, 0)))
1122 const_vn_reference_op_t pop
= &ops
[i
-1];
1123 base
= TREE_OPERAND (op
->op0
, 0);
1124 if (known_eq (pop
->off
, -1))
1130 offset
+= pop
->off
* BITS_PER_UNIT
;
1138 /* Record the base objects. */
1140 *op0_p
= build2 (MEM_REF
, op
->type
,
1141 NULL_TREE
, op
->op0
);
1142 MR_DEPENDENCE_CLIQUE (*op0_p
) = op
->clique
;
1143 MR_DEPENDENCE_BASE (*op0_p
) = op
->base
;
1144 op0_p
= &TREE_OPERAND (*op0_p
, 0);
1155 /* And now the usual component-reference style ops. */
1157 offset
+= wi::to_poly_offset (op
->op1
);
1162 tree field
= op
->op0
;
1163 /* We do not have a complete COMPONENT_REF tree here so we
1164 cannot use component_ref_field_offset. Do the interesting
1166 tree this_offset
= DECL_FIELD_OFFSET (field
);
1168 if (op
->op1
|| !poly_int_tree_p (this_offset
))
1172 poly_offset_int woffset
= (wi::to_poly_offset (this_offset
)
1173 << LOG2_BITS_PER_UNIT
);
1174 woffset
+= wi::to_offset (DECL_FIELD_BIT_OFFSET (field
));
1180 case ARRAY_RANGE_REF
:
1182 /* We recorded the lower bound and the element size. */
1183 if (!poly_int_tree_p (op
->op0
)
1184 || !poly_int_tree_p (op
->op1
)
1185 || TREE_CODE (op
->op2
) != INTEGER_CST
)
1189 poly_offset_int woffset
1190 = wi::sext (wi::to_poly_offset (op
->op0
)
1191 - wi::to_poly_offset (op
->op1
),
1192 TYPE_PRECISION (sizetype
));
1193 woffset
*= wi::to_offset (op
->op2
) * vn_ref_op_align_unit (op
);
1194 woffset
<<= LOG2_BITS_PER_UNIT
;
1206 case VIEW_CONVERT_EXPR
:
1223 if (base
== NULL_TREE
)
1226 ref
->ref
= NULL_TREE
;
1228 ref
->ref_alias_set
= set
;
1229 ref
->base_alias_set
= base_set
;
1230 /* We discount volatiles from value-numbering elsewhere. */
1231 ref
->volatile_p
= false;
1233 if (!size
.to_shwi (&ref
->size
) || maybe_lt (ref
->size
, 0))
1241 if (!offset
.to_shwi (&ref
->offset
))
1248 if (!max_size
.to_shwi (&ref
->max_size
) || maybe_lt (ref
->max_size
, 0))
1254 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1255 vn_reference_op_s's. */
1258 copy_reference_ops_from_call (gcall
*call
,
1259 vec
<vn_reference_op_s
> *result
)
1261 vn_reference_op_s temp
;
1263 tree lhs
= gimple_call_lhs (call
);
1266 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1267 different. By adding the lhs here in the vector, we ensure that the
1268 hashcode is different, guaranteeing a different value number. */
1269 if (lhs
&& TREE_CODE (lhs
) != SSA_NAME
)
1271 memset (&temp
, 0, sizeof (temp
));
1272 temp
.opcode
= MODIFY_EXPR
;
1273 temp
.type
= TREE_TYPE (lhs
);
1276 result
->safe_push (temp
);
1279 /* Copy the type, opcode, function, static chain and EH region, if any. */
1280 memset (&temp
, 0, sizeof (temp
));
1281 temp
.type
= gimple_call_fntype (call
);
1282 temp
.opcode
= CALL_EXPR
;
1283 temp
.op0
= gimple_call_fn (call
);
1284 if (gimple_call_internal_p (call
))
1285 temp
.clique
= gimple_call_internal_fn (call
);
1286 temp
.op1
= gimple_call_chain (call
);
1287 if (stmt_could_throw_p (cfun
, call
) && (lr
= lookup_stmt_eh_lp (call
)) > 0)
1288 temp
.op2
= size_int (lr
);
1290 result
->safe_push (temp
);
1292 /* Copy the call arguments. As they can be references as well,
1293 just chain them together. */
1294 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
1296 tree callarg
= gimple_call_arg (call
, i
);
1297 copy_reference_ops_from_ref (callarg
, result
);
1301 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1302 *I_P to point to the last element of the replacement. */
1304 vn_reference_fold_indirect (vec
<vn_reference_op_s
> *ops
,
1307 unsigned int i
= *i_p
;
1308 vn_reference_op_t op
= &(*ops
)[i
];
1309 vn_reference_op_t mem_op
= &(*ops
)[i
- 1];
1311 poly_int64 addr_offset
= 0;
1313 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1314 from .foo.bar to the preceding MEM_REF offset and replace the
1315 address with &OBJ. */
1316 addr_base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (op
->op0
, 0),
1317 &addr_offset
, vn_valueize
);
1318 gcc_checking_assert (addr_base
&& TREE_CODE (addr_base
) != MEM_REF
);
1319 if (addr_base
!= TREE_OPERAND (op
->op0
, 0))
1322 = (poly_offset_int::from (wi::to_poly_wide (mem_op
->op0
),
1325 mem_op
->op0
= wide_int_to_tree (TREE_TYPE (mem_op
->op0
), off
);
1326 op
->op0
= build_fold_addr_expr (addr_base
);
1327 if (tree_fits_shwi_p (mem_op
->op0
))
1328 mem_op
->off
= tree_to_shwi (mem_op
->op0
);
1336 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1337 *I_P to point to the last element of the replacement. */
1339 vn_reference_maybe_forwprop_address (vec
<vn_reference_op_s
> *ops
,
1342 bool changed
= false;
1343 vn_reference_op_t op
;
1347 unsigned int i
= *i_p
;
1349 vn_reference_op_t mem_op
= &(*ops
)[i
- 1];
1351 enum tree_code code
;
1352 poly_offset_int off
;
1354 def_stmt
= SSA_NAME_DEF_STMT (op
->op0
);
1355 if (!is_gimple_assign (def_stmt
))
1358 code
= gimple_assign_rhs_code (def_stmt
);
1359 if (code
!= ADDR_EXPR
1360 && code
!= POINTER_PLUS_EXPR
)
1363 off
= poly_offset_int::from (wi::to_poly_wide (mem_op
->op0
), SIGNED
);
1365 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1366 from .foo.bar to the preceding MEM_REF offset and replace the
1367 address with &OBJ. */
1368 if (code
== ADDR_EXPR
)
1370 tree addr
, addr_base
;
1371 poly_int64 addr_offset
;
1373 addr
= gimple_assign_rhs1 (def_stmt
);
1374 addr_base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr
, 0),
1377 /* If that didn't work because the address isn't invariant propagate
1378 the reference tree from the address operation in case the current
1379 dereference isn't offsetted. */
1381 && *i_p
== ops
->length () - 1
1382 && known_eq (off
, 0)
1383 /* This makes us disable this transform for PRE where the
1384 reference ops might be also used for code insertion which
1386 && default_vn_walk_kind
== VN_WALKREWRITE
)
1388 auto_vec
<vn_reference_op_s
, 32> tem
;
1389 copy_reference_ops_from_ref (TREE_OPERAND (addr
, 0), &tem
);
1390 /* Make sure to preserve TBAA info. The only objects not
1391 wrapped in MEM_REFs that can have their address taken are
1393 if (tem
.length () >= 2
1394 && tem
[tem
.length () - 2].opcode
== MEM_REF
)
1396 vn_reference_op_t new_mem_op
= &tem
[tem
.length () - 2];
1398 = wide_int_to_tree (TREE_TYPE (mem_op
->op0
),
1399 wi::to_poly_wide (new_mem_op
->op0
));
1402 gcc_assert (tem
.last ().opcode
== STRING_CST
);
1405 ops
->safe_splice (tem
);
1410 || TREE_CODE (addr_base
) != MEM_REF
1411 || (TREE_CODE (TREE_OPERAND (addr_base
, 0)) == SSA_NAME
1412 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base
,
1417 off
+= mem_ref_offset (addr_base
);
1418 op
->op0
= TREE_OPERAND (addr_base
, 0);
1423 ptr
= gimple_assign_rhs1 (def_stmt
);
1424 ptroff
= gimple_assign_rhs2 (def_stmt
);
1425 if (TREE_CODE (ptr
) != SSA_NAME
1426 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr
)
1427 /* Make sure to not endlessly recurse.
1428 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1429 happen when we value-number a PHI to its backedge value. */
1430 || SSA_VAL (ptr
) == op
->op0
1431 || !poly_int_tree_p (ptroff
))
1434 off
+= wi::to_poly_offset (ptroff
);
1438 mem_op
->op0
= wide_int_to_tree (TREE_TYPE (mem_op
->op0
), off
);
1439 if (tree_fits_shwi_p (mem_op
->op0
))
1440 mem_op
->off
= tree_to_shwi (mem_op
->op0
);
1443 /* ??? Can end up with endless recursion here!?
1444 gcc.c-torture/execute/strcmp-1.c */
1445 if (TREE_CODE (op
->op0
) == SSA_NAME
)
1446 op
->op0
= SSA_VAL (op
->op0
);
1447 if (TREE_CODE (op
->op0
) != SSA_NAME
)
1448 op
->opcode
= TREE_CODE (op
->op0
);
1453 while (TREE_CODE (op
->op0
) == SSA_NAME
);
1455 /* Fold a remaining *&. */
1456 if (TREE_CODE (op
->op0
) == ADDR_EXPR
)
1457 vn_reference_fold_indirect (ops
, i_p
);
1462 /* Optimize the reference REF to a constant if possible or return
1463 NULL_TREE if not. */
1466 fully_constant_vn_reference_p (vn_reference_t ref
)
1468 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1469 vn_reference_op_t op
;
1471 /* Try to simplify the translated expression if it is
1472 a call to a builtin function with at most two arguments. */
1474 if (op
->opcode
== CALL_EXPR
1476 || (TREE_CODE (op
->op0
) == ADDR_EXPR
1477 && TREE_CODE (TREE_OPERAND (op
->op0
, 0)) == FUNCTION_DECL
1478 && fndecl_built_in_p (TREE_OPERAND (op
->op0
, 0),
1480 && operands
.length () >= 2
1481 && operands
.length () <= 3)
1483 vn_reference_op_t arg0
, arg1
= NULL
;
1484 bool anyconst
= false;
1485 arg0
= &operands
[1];
1486 if (operands
.length () > 2)
1487 arg1
= &operands
[2];
1488 if (TREE_CODE_CLASS (arg0
->opcode
) == tcc_constant
1489 || (arg0
->opcode
== ADDR_EXPR
1490 && is_gimple_min_invariant (arg0
->op0
)))
1493 && (TREE_CODE_CLASS (arg1
->opcode
) == tcc_constant
1494 || (arg1
->opcode
== ADDR_EXPR
1495 && is_gimple_min_invariant (arg1
->op0
))))
1501 fn
= as_combined_fn (DECL_FUNCTION_CODE
1502 (TREE_OPERAND (op
->op0
, 0)));
1504 fn
= as_combined_fn ((internal_fn
) op
->clique
);
1507 folded
= fold_const_call (fn
, ref
->type
, arg0
->op0
, arg1
->op0
);
1509 folded
= fold_const_call (fn
, ref
->type
, arg0
->op0
);
1511 && is_gimple_min_invariant (folded
))
1516 /* Simplify reads from constants or constant initializers. */
1517 else if (BITS_PER_UNIT
== 8
1519 && COMPLETE_TYPE_P (ref
->type
)
1520 && is_gimple_reg_type (ref
->type
))
1524 if (INTEGRAL_TYPE_P (ref
->type
))
1525 size
= TYPE_PRECISION (ref
->type
);
1526 else if (tree_fits_shwi_p (TYPE_SIZE (ref
->type
)))
1527 size
= tree_to_shwi (TYPE_SIZE (ref
->type
));
1530 if (size
% BITS_PER_UNIT
!= 0
1531 || size
> MAX_BITSIZE_MODE_ANY_MODE
)
1533 size
/= BITS_PER_UNIT
;
1535 for (i
= 0; i
< operands
.length (); ++i
)
1537 if (TREE_CODE_CLASS (operands
[i
].opcode
) == tcc_constant
)
1542 if (known_eq (operands
[i
].off
, -1))
1544 off
+= operands
[i
].off
;
1545 if (operands
[i
].opcode
== MEM_REF
)
1551 vn_reference_op_t base
= &operands
[--i
];
1552 tree ctor
= error_mark_node
;
1553 tree decl
= NULL_TREE
;
1554 if (TREE_CODE_CLASS (base
->opcode
) == tcc_constant
)
1556 else if (base
->opcode
== MEM_REF
1557 && base
[1].opcode
== ADDR_EXPR
1558 && (TREE_CODE (TREE_OPERAND (base
[1].op0
, 0)) == VAR_DECL
1559 || TREE_CODE (TREE_OPERAND (base
[1].op0
, 0)) == CONST_DECL
1560 || TREE_CODE (TREE_OPERAND (base
[1].op0
, 0)) == STRING_CST
))
1562 decl
= TREE_OPERAND (base
[1].op0
, 0);
1563 if (TREE_CODE (decl
) == STRING_CST
)
1566 ctor
= ctor_for_folding (decl
);
1568 if (ctor
== NULL_TREE
)
1569 return build_zero_cst (ref
->type
);
1570 else if (ctor
!= error_mark_node
)
1572 HOST_WIDE_INT const_off
;
1575 tree res
= fold_ctor_reference (ref
->type
, ctor
,
1576 off
* BITS_PER_UNIT
,
1577 size
* BITS_PER_UNIT
, decl
);
1580 STRIP_USELESS_TYPE_CONVERSION (res
);
1581 if (is_gimple_min_invariant (res
))
1585 else if (off
.is_constant (&const_off
))
1587 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
1588 int len
= native_encode_expr (ctor
, buf
, size
, const_off
);
1590 return native_interpret_expr (ref
->type
, buf
, len
);
1598 /* Return true if OPS contain a storage order barrier. */
1601 contains_storage_order_barrier_p (vec
<vn_reference_op_s
> ops
)
1603 vn_reference_op_t op
;
1606 FOR_EACH_VEC_ELT (ops
, i
, op
)
1607 if (op
->opcode
== VIEW_CONVERT_EXPR
&& op
->reverse
)
1613 /* Return true if OPS represent an access with reverse storage order. */
1616 reverse_storage_order_for_component_p (vec
<vn_reference_op_s
> ops
)
1619 if (ops
[i
].opcode
== REALPART_EXPR
|| ops
[i
].opcode
== IMAGPART_EXPR
)
1621 switch (ops
[i
].opcode
)
1627 return ops
[i
].reverse
;
1633 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1634 structures into their value numbers. This is done in-place, and
1635 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1636 whether any operands were valueized. */
1639 valueize_refs_1 (vec
<vn_reference_op_s
> *orig
, bool *valueized_anything
,
1640 bool with_avail
= false)
1642 *valueized_anything
= false;
1644 for (unsigned i
= 0; i
< orig
->length (); ++i
)
1647 vn_reference_op_t vro
= &(*orig
)[i
];
1648 if (vro
->opcode
== SSA_NAME
1649 || (vro
->op0
&& TREE_CODE (vro
->op0
) == SSA_NAME
))
1651 tree tem
= with_avail
? vn_valueize (vro
->op0
) : SSA_VAL (vro
->op0
);
1652 if (tem
!= vro
->op0
)
1654 *valueized_anything
= true;
1657 /* If it transforms from an SSA_NAME to a constant, update
1659 if (TREE_CODE (vro
->op0
) != SSA_NAME
&& vro
->opcode
== SSA_NAME
)
1660 vro
->opcode
= TREE_CODE (vro
->op0
);
1662 if (vro
->op1
&& TREE_CODE (vro
->op1
) == SSA_NAME
)
1664 tree tem
= with_avail
? vn_valueize (vro
->op1
) : SSA_VAL (vro
->op1
);
1665 if (tem
!= vro
->op1
)
1667 *valueized_anything
= true;
1671 if (vro
->op2
&& TREE_CODE (vro
->op2
) == SSA_NAME
)
1673 tree tem
= with_avail
? vn_valueize (vro
->op2
) : SSA_VAL (vro
->op2
);
1674 if (tem
!= vro
->op2
)
1676 *valueized_anything
= true;
1680 /* If it transforms from an SSA_NAME to an address, fold with
1681 a preceding indirect reference. */
1684 && TREE_CODE (vro
->op0
) == ADDR_EXPR
1685 && (*orig
)[i
- 1].opcode
== MEM_REF
)
1687 if (vn_reference_fold_indirect (orig
, &i
))
1688 *valueized_anything
= true;
1691 && vro
->opcode
== SSA_NAME
1692 && (*orig
)[i
- 1].opcode
== MEM_REF
)
1694 if (vn_reference_maybe_forwprop_address (orig
, &i
))
1696 *valueized_anything
= true;
1697 /* Re-valueize the current operand. */
1701 /* If it transforms a non-constant ARRAY_REF into a constant
1702 one, adjust the constant offset. */
1703 else if (vro
->opcode
== ARRAY_REF
1704 && known_eq (vro
->off
, -1)
1705 && poly_int_tree_p (vro
->op0
)
1706 && poly_int_tree_p (vro
->op1
)
1707 && TREE_CODE (vro
->op2
) == INTEGER_CST
)
1709 poly_offset_int off
= ((wi::to_poly_offset (vro
->op0
)
1710 - wi::to_poly_offset (vro
->op1
))
1711 * wi::to_offset (vro
->op2
)
1712 * vn_ref_op_align_unit (vro
));
1713 off
.to_shwi (&vro
->off
);
1719 valueize_refs (vec
<vn_reference_op_s
> *orig
)
1722 valueize_refs_1 (orig
, &tem
);
1725 static vec
<vn_reference_op_s
> shared_lookup_references
;
1727 /* Create a vector of vn_reference_op_s structures from REF, a
1728 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1729 this function. *VALUEIZED_ANYTHING will specify whether any
1730 operands were valueized. */
1732 static vec
<vn_reference_op_s
>
1733 valueize_shared_reference_ops_from_ref (tree ref
, bool *valueized_anything
)
1737 shared_lookup_references
.truncate (0);
1738 copy_reference_ops_from_ref (ref
, &shared_lookup_references
);
1739 valueize_refs_1 (&shared_lookup_references
, valueized_anything
);
1740 return shared_lookup_references
;
1743 /* Create a vector of vn_reference_op_s structures from CALL, a
1744 call statement. The vector is shared among all callers of
1747 static vec
<vn_reference_op_s
>
1748 valueize_shared_reference_ops_from_call (gcall
*call
)
1752 shared_lookup_references
.truncate (0);
1753 copy_reference_ops_from_call (call
, &shared_lookup_references
);
1754 valueize_refs (&shared_lookup_references
);
1755 return shared_lookup_references
;
1758 /* Lookup a SCCVN reference operation VR in the current hash table.
1759 Returns the resulting value number if it exists in the hash table,
1760 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1761 vn_reference_t stored in the hashtable if something is found. */
1764 vn_reference_lookup_1 (vn_reference_t vr
, vn_reference_t
*vnresult
)
1766 vn_reference_s
**slot
;
1769 hash
= vr
->hashcode
;
1770 slot
= valid_info
->references
->find_slot_with_hash (vr
, hash
, NO_INSERT
);
1774 *vnresult
= (vn_reference_t
)*slot
;
1775 return ((vn_reference_t
)*slot
)->result
;
1782 /* Partial definition tracking support. */
1786 HOST_WIDE_INT offset
;
1793 HOST_WIDE_INT rhs_off
;
1794 HOST_WIDE_INT offset
;
1798 /* Context for alias walking. */
1800 struct vn_walk_cb_data
1802 vn_walk_cb_data (vn_reference_t vr_
, tree orig_ref_
, tree
*last_vuse_ptr_
,
1803 vn_lookup_kind vn_walk_kind_
, bool tbaa_p_
, tree mask_
,
1804 bool redundant_store_removal_p_
)
1805 : vr (vr_
), last_vuse_ptr (last_vuse_ptr_
), last_vuse (NULL_TREE
),
1806 mask (mask_
), masked_result (NULL_TREE
), vn_walk_kind (vn_walk_kind_
),
1807 tbaa_p (tbaa_p_
), redundant_store_removal_p (redundant_store_removal_p_
),
1808 saved_operands (vNULL
), first_set (-2), first_base_set (-2),
1812 last_vuse_ptr
= &last_vuse
;
1813 ao_ref_init (&orig_ref
, orig_ref_
);
1816 wide_int w
= wi::to_wide (mask
);
1817 unsigned int pos
= 0, prec
= w
.get_precision ();
1819 pd
.rhs
= build_constructor (NULL_TREE
, NULL
);
1821 /* When bitwise and with a constant is done on a memory load,
1822 we don't really need all the bits to be defined or defined
1823 to constants, we don't really care what is in the position
1824 corresponding to 0 bits in the mask.
1825 So, push the ranges of those 0 bits in the mask as artificial
1826 zero stores and let the partial def handling code do the
1830 int tz
= wi::ctz (w
);
1831 if (pos
+ tz
> prec
)
1835 if (BYTES_BIG_ENDIAN
)
1836 pd
.offset
= prec
- pos
- tz
;
1840 void *r
= push_partial_def (pd
, 0, 0, 0, prec
);
1841 gcc_assert (r
== NULL_TREE
);
1846 w
= wi::lrshift (w
, tz
);
1847 tz
= wi::ctz (wi::bit_not (w
));
1848 if (pos
+ tz
> prec
)
1851 w
= wi::lrshift (w
, tz
);
1855 ~vn_walk_cb_data ();
1856 void *finish (alias_set_type
, alias_set_type
, tree
);
1857 void *push_partial_def (pd_data pd
,
1858 alias_set_type
, alias_set_type
, HOST_WIDE_INT
,
1863 tree
*last_vuse_ptr
;
1867 vn_lookup_kind vn_walk_kind
;
1869 bool redundant_store_removal_p
;
1870 vec
<vn_reference_op_s
> saved_operands
;
1872 /* The VDEFs of partial defs we come along. */
1873 auto_vec
<pd_data
, 2> partial_defs
;
1874 /* The first defs range to avoid splay tree setup in most cases. */
1875 pd_range first_range
;
1876 alias_set_type first_set
;
1877 alias_set_type first_base_set
;
1878 splay_tree known_ranges
;
1879 obstack ranges_obstack
;
1882 vn_walk_cb_data::~vn_walk_cb_data ()
1886 splay_tree_delete (known_ranges
);
1887 obstack_free (&ranges_obstack
, NULL
);
1889 saved_operands
.release ();
1893 vn_walk_cb_data::finish (alias_set_type set
, alias_set_type base_set
, tree val
)
1895 if (first_set
!= -2)
1898 base_set
= first_base_set
;
1902 masked_result
= val
;
1905 vec
<vn_reference_op_s
> &operands
1906 = saved_operands
.exists () ? saved_operands
: vr
->operands
;
1907 return vn_reference_lookup_or_insert_for_pieces (last_vuse
, set
, base_set
,
1908 vr
->type
, operands
, val
);
1911 /* pd_range splay-tree helpers. */
1914 pd_range_compare (splay_tree_key offset1p
, splay_tree_key offset2p
)
1916 HOST_WIDE_INT offset1
= *(HOST_WIDE_INT
*)offset1p
;
1917 HOST_WIDE_INT offset2
= *(HOST_WIDE_INT
*)offset2p
;
1918 if (offset1
< offset2
)
1920 else if (offset1
> offset2
)
1926 pd_tree_alloc (int size
, void *data_
)
1928 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
1929 return obstack_alloc (&data
->ranges_obstack
, size
);
1933 pd_tree_dealloc (void *, void *)
1937 /* Push PD to the vector of partial definitions returning a
1938 value when we are ready to combine things with VUSE, SET and MAXSIZEI,
1939 NULL when we want to continue looking for partial defs or -1
1943 vn_walk_cb_data::push_partial_def (pd_data pd
,
1944 alias_set_type set
, alias_set_type base_set
,
1945 HOST_WIDE_INT offseti
,
1946 HOST_WIDE_INT maxsizei
)
1948 const HOST_WIDE_INT bufsize
= 64;
1949 /* We're using a fixed buffer for encoding so fail early if the object
1950 we want to interpret is bigger. */
1951 if (maxsizei
> bufsize
* BITS_PER_UNIT
1953 || BITS_PER_UNIT
!= 8
1954 /* Not prepared to handle PDP endian. */
1955 || BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
1958 /* Turn too large constant stores into non-constant stores. */
1959 if (CONSTANT_CLASS_P (pd
.rhs
) && pd
.size
> bufsize
* BITS_PER_UNIT
)
1960 pd
.rhs
= error_mark_node
;
1962 /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
1963 most a partial byte before and/or after the region. */
1964 if (!CONSTANT_CLASS_P (pd
.rhs
))
1966 if (pd
.offset
< offseti
)
1968 HOST_WIDE_INT o
= ROUND_DOWN (offseti
- pd
.offset
, BITS_PER_UNIT
);
1969 gcc_assert (pd
.size
> o
);
1973 if (pd
.size
> maxsizei
)
1974 pd
.size
= maxsizei
+ ((pd
.size
- maxsizei
) % BITS_PER_UNIT
);
1977 pd
.offset
-= offseti
;
1979 bool pd_constant_p
= (TREE_CODE (pd
.rhs
) == CONSTRUCTOR
1980 || CONSTANT_CLASS_P (pd
.rhs
));
1982 if (partial_defs
.is_empty ())
1984 /* If we get a clobber upfront, fail. */
1985 if (TREE_CLOBBER_P (pd
.rhs
))
1989 partial_defs
.safe_push (pd
);
1990 first_range
.offset
= pd
.offset
;
1991 first_range
.size
= pd
.size
;
1993 first_base_set
= base_set
;
1994 last_vuse_ptr
= NULL
;
1996 /* Go check if the first partial definition was a full one in case
1997 the caller didn't optimize for this. */
2003 /* ??? Optimize the case where the 2nd partial def completes
2005 gcc_obstack_init (&ranges_obstack
);
2006 known_ranges
= splay_tree_new_with_allocator (pd_range_compare
, 0, 0,
2008 pd_tree_dealloc
, this);
2009 splay_tree_insert (known_ranges
,
2010 (splay_tree_key
)&first_range
.offset
,
2011 (splay_tree_value
)&first_range
);
2014 pd_range newr
= { pd
.offset
, pd
.size
};
2016 /* Lookup the predecessor of offset + 1 and see if we need to merge. */
2017 HOST_WIDE_INT loffset
= newr
.offset
+ 1;
2018 if ((n
= splay_tree_predecessor (known_ranges
, (splay_tree_key
)&loffset
))
2019 && ((r
= (pd_range
*)n
->value
), true)
2020 && ranges_known_overlap_p (r
->offset
, r
->size
+ 1,
2021 newr
.offset
, newr
.size
))
2023 /* Ignore partial defs already covered. Here we also drop shadowed
2024 clobbers arriving here at the floor. */
2025 if (known_subrange_p (newr
.offset
, newr
.size
, r
->offset
, r
->size
))
2028 = MAX (r
->offset
+ r
->size
, newr
.offset
+ newr
.size
) - r
->offset
;
2032 /* newr.offset wasn't covered yet, insert the range. */
2033 r
= XOBNEW (&ranges_obstack
, pd_range
);
2035 splay_tree_insert (known_ranges
, (splay_tree_key
)&r
->offset
,
2036 (splay_tree_value
)r
);
2038 /* Merge r which now contains newr and is a member of the splay tree with
2039 adjacent overlapping ranges. */
2041 while ((n
= splay_tree_successor (known_ranges
,
2042 (splay_tree_key
)&r
->offset
))
2043 && ((rafter
= (pd_range
*)n
->value
), true)
2044 && ranges_known_overlap_p (r
->offset
, r
->size
+ 1,
2045 rafter
->offset
, rafter
->size
))
2047 r
->size
= MAX (r
->offset
+ r
->size
,
2048 rafter
->offset
+ rafter
->size
) - r
->offset
;
2049 splay_tree_remove (known_ranges
, (splay_tree_key
)&rafter
->offset
);
2051 /* If we get a clobber, fail. */
2052 if (TREE_CLOBBER_P (pd
.rhs
))
2054 /* Non-constants are OK as long as they are shadowed by a constant. */
2057 partial_defs
.safe_push (pd
);
2060 /* Now we have merged newr into the range tree. When we have covered
2061 [offseti, sizei] then the tree will contain exactly one node which has
2062 the desired properties and it will be 'r'. */
2063 if (!known_subrange_p (0, maxsizei
, r
->offset
, r
->size
))
2064 /* Continue looking for partial defs. */
2067 /* Now simply native encode all partial defs in reverse order. */
2068 unsigned ndefs
= partial_defs
.length ();
2069 /* We support up to 512-bit values (for V8DFmode). */
2070 unsigned char buffer
[bufsize
+ 1];
2071 unsigned char this_buffer
[bufsize
+ 1];
2074 memset (buffer
, 0, bufsize
+ 1);
2075 unsigned needed_len
= ROUND_UP (maxsizei
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
2076 while (!partial_defs
.is_empty ())
2078 pd_data pd
= partial_defs
.pop ();
2080 if (TREE_CODE (pd
.rhs
) == CONSTRUCTOR
)
2082 /* Empty CONSTRUCTOR. */
2083 if (pd
.size
>= needed_len
* BITS_PER_UNIT
)
2086 len
= ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
2087 memset (this_buffer
, 0, len
);
2091 len
= native_encode_expr (pd
.rhs
, this_buffer
, bufsize
,
2092 (MAX (0, -pd
.offset
)
2093 + pd
.rhs_off
) / BITS_PER_UNIT
);
2095 || len
< (ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
2096 - MAX (0, -pd
.offset
) / BITS_PER_UNIT
))
2098 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2099 fprintf (dump_file
, "Failed to encode %u "
2100 "partial definitions\n", ndefs
);
2105 unsigned char *p
= buffer
;
2106 HOST_WIDE_INT size
= pd
.size
;
2108 size
-= ROUND_DOWN (-pd
.offset
, BITS_PER_UNIT
);
2109 this_buffer
[len
] = 0;
2110 if (BYTES_BIG_ENDIAN
)
2112 /* LSB of this_buffer[len - 1] byte should be at
2113 pd.offset + pd.size - 1 bits in buffer. */
2114 amnt
= ((unsigned HOST_WIDE_INT
) pd
.offset
2115 + pd
.size
) % BITS_PER_UNIT
;
2117 shift_bytes_in_array_right (this_buffer
, len
+ 1, amnt
);
2118 unsigned char *q
= this_buffer
;
2119 unsigned int off
= 0;
2123 off
= pd
.offset
/ BITS_PER_UNIT
;
2124 gcc_assert (off
< needed_len
);
2128 msk
= ((1 << size
) - 1) << (BITS_PER_UNIT
- amnt
);
2129 *p
= (*p
& ~msk
) | (this_buffer
[len
] & msk
);
2134 if (TREE_CODE (pd
.rhs
) != CONSTRUCTOR
)
2135 q
= (this_buffer
+ len
2136 - (ROUND_UP (size
- amnt
, BITS_PER_UNIT
)
2138 if (pd
.offset
% BITS_PER_UNIT
)
2140 msk
= -1U << (BITS_PER_UNIT
2141 - (pd
.offset
% BITS_PER_UNIT
));
2142 *p
= (*p
& msk
) | (*q
& ~msk
);
2146 size
-= BITS_PER_UNIT
- (pd
.offset
% BITS_PER_UNIT
);
2147 gcc_assert (size
>= 0);
2151 else if (TREE_CODE (pd
.rhs
) != CONSTRUCTOR
)
2153 q
= (this_buffer
+ len
2154 - (ROUND_UP (size
- amnt
, BITS_PER_UNIT
)
2156 if (pd
.offset
% BITS_PER_UNIT
)
2159 size
-= BITS_PER_UNIT
- ((unsigned HOST_WIDE_INT
) pd
.offset
2161 gcc_assert (size
>= 0);
2164 if ((unsigned HOST_WIDE_INT
) size
/ BITS_PER_UNIT
+ off
2166 size
= (needed_len
- off
) * BITS_PER_UNIT
;
2167 memcpy (p
, q
, size
/ BITS_PER_UNIT
);
2168 if (size
% BITS_PER_UNIT
)
2171 = -1U << (BITS_PER_UNIT
- (size
% BITS_PER_UNIT
));
2172 p
+= size
/ BITS_PER_UNIT
;
2173 q
+= size
/ BITS_PER_UNIT
;
2174 *p
= (*q
& msk
) | (*p
& ~msk
);
2181 /* LSB of this_buffer[0] byte should be at pd.offset bits
2184 size
= MIN (size
, (HOST_WIDE_INT
) needed_len
* BITS_PER_UNIT
);
2185 amnt
= pd
.offset
% BITS_PER_UNIT
;
2187 shift_bytes_in_array_left (this_buffer
, len
+ 1, amnt
);
2188 unsigned int off
= pd
.offset
/ BITS_PER_UNIT
;
2189 gcc_assert (off
< needed_len
);
2191 (HOST_WIDE_INT
) (needed_len
- off
) * BITS_PER_UNIT
);
2193 if (amnt
+ size
< BITS_PER_UNIT
)
2195 /* Low amnt bits come from *p, then size bits
2196 from this_buffer[0] and the remaining again from
2198 msk
= ((1 << size
) - 1) << amnt
;
2199 *p
= (*p
& ~msk
) | (this_buffer
[0] & msk
);
2205 *p
= (*p
& ~msk
) | (this_buffer
[0] & msk
);
2207 size
-= (BITS_PER_UNIT
- amnt
);
2212 amnt
= (unsigned HOST_WIDE_INT
) pd
.offset
% BITS_PER_UNIT
;
2214 size
-= BITS_PER_UNIT
- amnt
;
2215 size
= MIN (size
, (HOST_WIDE_INT
) needed_len
* BITS_PER_UNIT
);
2217 shift_bytes_in_array_left (this_buffer
, len
+ 1, amnt
);
2219 memcpy (p
, this_buffer
+ (amnt
!= 0), size
/ BITS_PER_UNIT
);
2220 p
+= size
/ BITS_PER_UNIT
;
2221 if (size
% BITS_PER_UNIT
)
2223 unsigned int msk
= -1U << (size
% BITS_PER_UNIT
);
2224 *p
= (this_buffer
[(amnt
!= 0) + size
/ BITS_PER_UNIT
]
2225 & ~msk
) | (*p
& msk
);
2230 tree type
= vr
->type
;
2231 /* Make sure to interpret in a type that has a range covering the whole
2233 if (INTEGRAL_TYPE_P (vr
->type
) && maxsizei
!= TYPE_PRECISION (vr
->type
))
2234 type
= build_nonstandard_integer_type (maxsizei
, TYPE_UNSIGNED (type
));
2236 if (BYTES_BIG_ENDIAN
)
2238 unsigned sz
= needed_len
;
2239 if (maxsizei
% BITS_PER_UNIT
)
2240 shift_bytes_in_array_right (buffer
, needed_len
,
2242 - (maxsizei
% BITS_PER_UNIT
));
2243 if (INTEGRAL_TYPE_P (type
))
2244 sz
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
2245 if (sz
> needed_len
)
2247 memcpy (this_buffer
+ (sz
- needed_len
), buffer
, needed_len
);
2248 val
= native_interpret_expr (type
, this_buffer
, sz
);
2251 val
= native_interpret_expr (type
, buffer
, needed_len
);
2254 val
= native_interpret_expr (type
, buffer
, bufsize
);
2255 /* If we chop off bits because the types precision doesn't match the memory
2256 access size this is ok when optimizing reads but not when called from
2257 the DSE code during elimination. */
2258 if (val
&& type
!= vr
->type
)
2260 if (! int_fits_type_p (val
, vr
->type
))
2263 val
= fold_convert (vr
->type
, val
);
2268 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2270 "Successfully combined %u partial definitions\n", ndefs
);
2271 /* We are using the alias-set of the first store we encounter which
2272 should be appropriate here. */
2273 return finish (first_set
, first_base_set
, val
);
2277 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2279 "Failed to interpret %u encoded partial definitions\n", ndefs
);
2284 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2285 with the current VUSE and performs the expression lookup. */
2288 vn_reference_lookup_2 (ao_ref
*op ATTRIBUTE_UNUSED
, tree vuse
, void *data_
)
2290 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
2291 vn_reference_t vr
= data
->vr
;
2292 vn_reference_s
**slot
;
2295 /* If we have partial definitions recorded we have to go through
2296 vn_reference_lookup_3. */
2297 if (!data
->partial_defs
.is_empty ())
2300 if (data
->last_vuse_ptr
)
2302 *data
->last_vuse_ptr
= vuse
;
2303 data
->last_vuse
= vuse
;
2306 /* Fixup vuse and hash. */
2308 vr
->hashcode
= vr
->hashcode
- SSA_NAME_VERSION (vr
->vuse
);
2309 vr
->vuse
= vuse_ssa_val (vuse
);
2311 vr
->hashcode
= vr
->hashcode
+ SSA_NAME_VERSION (vr
->vuse
);
2313 hash
= vr
->hashcode
;
2314 slot
= valid_info
->references
->find_slot_with_hash (vr
, hash
, NO_INSERT
);
2317 if ((*slot
)->result
&& data
->saved_operands
.exists ())
2318 return data
->finish (vr
->set
, vr
->base_set
, (*slot
)->result
);
2325 /* Lookup an existing or insert a new vn_reference entry into the
2326 value table for the VUSE, SET, TYPE, OPERANDS reference which
2327 has the value VALUE which is either a constant or an SSA name. */
2329 static vn_reference_t
2330 vn_reference_lookup_or_insert_for_pieces (tree vuse
,
2332 alias_set_type base_set
,
2334 vec
<vn_reference_op_s
,
2339 vn_reference_t result
;
2341 vr1
.vuse
= vuse
? SSA_VAL (vuse
) : NULL_TREE
;
2342 vr1
.operands
= operands
;
2345 vr1
.base_set
= base_set
;
2346 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
2347 if (vn_reference_lookup_1 (&vr1
, &result
))
2349 if (TREE_CODE (value
) == SSA_NAME
)
2350 value_id
= VN_INFO (value
)->value_id
;
2352 value_id
= get_or_alloc_constant_value_id (value
);
2353 return vn_reference_insert_pieces (vuse
, set
, base_set
, type
,
2354 operands
.copy (), value
, value_id
);
2357 /* Return a value-number for RCODE OPS... either by looking up an existing
2358 value-number for the possibly simplified result or by inserting the
2359 operation if INSERT is true. If SIMPLIFY is false, return a value
2360 number for the unsimplified expression. */
2363 vn_nary_build_or_lookup_1 (gimple_match_op
*res_op
, bool insert
,
2366 tree result
= NULL_TREE
;
2367 /* We will be creating a value number for
2369 So first simplify and lookup this expression to see if it
2370 is already available. */
2371 /* For simplification valueize. */
2374 for (i
= 0; i
< res_op
->num_ops
; ++i
)
2375 if (TREE_CODE (res_op
->ops
[i
]) == SSA_NAME
)
2377 tree tem
= vn_valueize (res_op
->ops
[i
]);
2380 res_op
->ops
[i
] = tem
;
2382 /* If valueization of an operand fails (it is not available), skip
2385 if (i
== res_op
->num_ops
)
2387 mprts_hook
= vn_lookup_simplify_result
;
2388 res
= res_op
->resimplify (NULL
, vn_valueize
);
2391 gimple
*new_stmt
= NULL
;
2393 && gimple_simplified_result_is_gimple_val (res_op
))
2395 /* The expression is already available. */
2396 result
= res_op
->ops
[0];
2397 /* Valueize it, simplification returns sth in AVAIL only. */
2398 if (TREE_CODE (result
) == SSA_NAME
)
2399 result
= SSA_VAL (result
);
2403 tree val
= vn_lookup_simplify_result (res_op
);
2406 gimple_seq stmts
= NULL
;
2407 result
= maybe_push_res_to_seq (res_op
, &stmts
);
2410 gcc_assert (gimple_seq_singleton_p (stmts
));
2411 new_stmt
= gimple_seq_first_stmt (stmts
);
2415 /* The expression is already available. */
2420 /* The expression is not yet available, value-number lhs to
2421 the new SSA_NAME we created. */
2422 /* Initialize value-number information properly. */
2423 vn_ssa_aux_t result_info
= VN_INFO (result
);
2424 result_info
->valnum
= result
;
2425 result_info
->value_id
= get_next_value_id ();
2426 result_info
->visited
= 1;
2427 gimple_seq_add_stmt_without_update (&VN_INFO (result
)->expr
,
2429 result_info
->needs_insertion
= true;
2430 /* ??? PRE phi-translation inserts NARYs without corresponding
2431 SSA name result. Re-use those but set their result according
2432 to the stmt we just built. */
2433 vn_nary_op_t nary
= NULL
;
2434 vn_nary_op_lookup_stmt (new_stmt
, &nary
);
2437 gcc_assert (! nary
->predicated_values
&& nary
->u
.result
== NULL_TREE
);
2438 nary
->u
.result
= gimple_assign_lhs (new_stmt
);
2440 /* As all "inserted" statements are singleton SCCs, insert
2441 to the valid table. This is strictly needed to
2442 avoid re-generating new value SSA_NAMEs for the same
2443 expression during SCC iteration over and over (the
2444 optimistic table gets cleared after each iteration).
2445 We do not need to insert into the optimistic table, as
2446 lookups there will fall back to the valid table. */
2449 unsigned int length
= vn_nary_length_from_stmt (new_stmt
);
2451 = alloc_vn_nary_op_noinit (length
, &vn_tables_insert_obstack
);
2452 vno1
->value_id
= result_info
->value_id
;
2453 vno1
->length
= length
;
2454 vno1
->predicated_values
= 0;
2455 vno1
->u
.result
= result
;
2456 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (new_stmt
));
2457 vn_nary_op_insert_into (vno1
, valid_info
->nary
);
2458 /* Also do not link it into the undo chain. */
2459 last_inserted_nary
= vno1
->next
;
2460 vno1
->next
= (vn_nary_op_t
)(void *)-1;
2462 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2464 fprintf (dump_file
, "Inserting name ");
2465 print_generic_expr (dump_file
, result
);
2466 fprintf (dump_file
, " for expression ");
2467 print_gimple_expr (dump_file
, new_stmt
, 0, TDF_SLIM
);
2468 fprintf (dump_file
, "\n");
2474 /* Return a value-number for RCODE OPS... either by looking up an existing
2475 value-number for the simplified result or by inserting the operation. */
2478 vn_nary_build_or_lookup (gimple_match_op
*res_op
)
2480 return vn_nary_build_or_lookup_1 (res_op
, true, true);
2483 /* Try to simplify the expression RCODE OPS... of type TYPE and return
2484 its value if present. */
2487 vn_nary_simplify (vn_nary_op_t nary
)
2489 if (nary
->length
> gimple_match_op::MAX_NUM_OPS
)
2491 gimple_match_op
op (gimple_match_cond::UNCOND
, nary
->opcode
,
2492 nary
->type
, nary
->length
);
2493 memcpy (op
.ops
, nary
->op
, sizeof (tree
) * nary
->length
);
2494 return vn_nary_build_or_lookup_1 (&op
, false, true);
2497 /* Elimination engine. */
2499 class eliminate_dom_walker
: public dom_walker
2502 eliminate_dom_walker (cdi_direction
, bitmap
);
2503 ~eliminate_dom_walker ();
2505 edge
before_dom_children (basic_block
) final override
;
2506 void after_dom_children (basic_block
) final override
;
2508 virtual tree
eliminate_avail (basic_block
, tree op
);
2509 virtual void eliminate_push_avail (basic_block
, tree op
);
2510 tree
eliminate_insert (basic_block
, gimple_stmt_iterator
*gsi
, tree val
);
2512 void eliminate_stmt (basic_block
, gimple_stmt_iterator
*);
2514 unsigned eliminate_cleanup (bool region_p
= false);
2517 unsigned int el_todo
;
2518 unsigned int eliminations
;
2519 unsigned int insertions
;
2521 /* SSA names that had their defs inserted by PRE if do_pre. */
2522 bitmap inserted_exprs
;
2524 /* Blocks with statements that have had their EH properties changed. */
2525 bitmap need_eh_cleanup
;
2527 /* Blocks with statements that have had their AB properties changed. */
2528 bitmap need_ab_cleanup
;
2530 /* Local state for the eliminate domwalk. */
2531 auto_vec
<gimple
*> to_remove
;
2532 auto_vec
<gimple
*> to_fixup
;
2533 auto_vec
<tree
> avail
;
2534 auto_vec
<tree
> avail_stack
;
2537 /* Adaptor to the elimination engine using RPO availability. */
2539 class rpo_elim
: public eliminate_dom_walker
2542 rpo_elim(basic_block entry_
)
2543 : eliminate_dom_walker (CDI_DOMINATORS
, NULL
), entry (entry_
),
2544 m_avail_freelist (NULL
) {}
2546 tree
eliminate_avail (basic_block
, tree op
) final override
;
2548 void eliminate_push_avail (basic_block
, tree
) final override
;
2551 /* Freelist of avail entries which are allocated from the vn_ssa_aux
2553 vn_avail
*m_avail_freelist
;
2556 /* Global RPO state for access from hooks. */
2557 static eliminate_dom_walker
*rpo_avail
;
2558 basic_block vn_context_bb
;
2560 /* Return true if BASE1 and BASE2 can be adjusted so they have the
2561 same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2562 Otherwise return false. */
2565 adjust_offsets_for_equal_base_address (tree base1
, poly_int64
*offset1
,
2566 tree base2
, poly_int64
*offset2
)
2569 if (TREE_CODE (base1
) == MEM_REF
2570 && TREE_CODE (base2
) == MEM_REF
)
2572 if (mem_ref_offset (base1
).to_shwi (&soff
))
2574 base1
= TREE_OPERAND (base1
, 0);
2575 *offset1
+= soff
* BITS_PER_UNIT
;
2577 if (mem_ref_offset (base2
).to_shwi (&soff
))
2579 base2
= TREE_OPERAND (base2
, 0);
2580 *offset2
+= soff
* BITS_PER_UNIT
;
2582 return operand_equal_p (base1
, base2
, 0);
2584 return operand_equal_p (base1
, base2
, OEP_ADDRESS_OF
);
2587 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2588 from the statement defining VUSE and if not successful tries to
2589 translate *REFP and VR_ through an aggregate copy at the definition
2590 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2591 of *REF and *VR. If only disambiguation was performed then
2592 *DISAMBIGUATE_ONLY is set to true. */
2595 vn_reference_lookup_3 (ao_ref
*ref
, tree vuse
, void *data_
,
2596 translate_flags
*disambiguate_only
)
2598 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
2599 vn_reference_t vr
= data
->vr
;
2600 gimple
*def_stmt
= SSA_NAME_DEF_STMT (vuse
);
2601 tree base
= ao_ref_base (ref
);
2602 HOST_WIDE_INT offseti
= 0, maxsizei
, sizei
= 0;
2603 static vec
<vn_reference_op_s
> lhs_ops
;
2605 bool lhs_ref_ok
= false;
2606 poly_int64 copy_size
;
2608 /* First try to disambiguate after value-replacing in the definitions LHS. */
2609 if (is_gimple_assign (def_stmt
))
2611 tree lhs
= gimple_assign_lhs (def_stmt
);
2612 bool valueized_anything
= false;
2613 /* Avoid re-allocation overhead. */
2614 lhs_ops
.truncate (0);
2615 basic_block saved_rpo_bb
= vn_context_bb
;
2616 vn_context_bb
= gimple_bb (def_stmt
);
2617 if (*disambiguate_only
<= TR_VALUEIZE_AND_DISAMBIGUATE
)
2619 copy_reference_ops_from_ref (lhs
, &lhs_ops
);
2620 valueize_refs_1 (&lhs_ops
, &valueized_anything
, true);
2622 vn_context_bb
= saved_rpo_bb
;
2623 ao_ref_init (&lhs_ref
, lhs
);
2625 if (valueized_anything
2626 && ao_ref_init_from_vn_reference
2627 (&lhs_ref
, ao_ref_alias_set (&lhs_ref
),
2628 ao_ref_base_alias_set (&lhs_ref
), TREE_TYPE (lhs
), lhs_ops
)
2629 && !refs_may_alias_p_1 (ref
, &lhs_ref
, data
->tbaa_p
))
2631 *disambiguate_only
= TR_VALUEIZE_AND_DISAMBIGUATE
;
2635 /* When the def is a CLOBBER we can optimistically disambiguate
2636 against it since any overlap it would be undefined behavior.
2637 Avoid this for obvious must aliases to save compile-time though.
2638 We also may not do this when the query is used for redundant
2640 if (!data
->redundant_store_removal_p
2641 && gimple_clobber_p (def_stmt
)
2642 && !operand_equal_p (ao_ref_base (&lhs_ref
), base
, OEP_ADDRESS_OF
))
2644 *disambiguate_only
= TR_DISAMBIGUATE
;
2648 /* Besides valueizing the LHS we can also use access-path based
2649 disambiguation on the original non-valueized ref. */
2652 && data
->orig_ref
.ref
)
2654 /* We want to use the non-valueized LHS for this, but avoid redundant
2656 ao_ref
*lref
= &lhs_ref
;
2658 if (valueized_anything
)
2660 ao_ref_init (&lref_alt
, lhs
);
2663 if (!refs_may_alias_p_1 (&data
->orig_ref
, lref
, data
->tbaa_p
))
2665 *disambiguate_only
= (valueized_anything
2666 ? TR_VALUEIZE_AND_DISAMBIGUATE
2672 /* If we reach a clobbering statement try to skip it and see if
2673 we find a VN result with exactly the same value as the
2674 possible clobber. In this case we can ignore the clobber
2675 and return the found value. */
2676 if (is_gimple_reg_type (TREE_TYPE (lhs
))
2677 && types_compatible_p (TREE_TYPE (lhs
), vr
->type
)
2678 && (ref
->ref
|| data
->orig_ref
.ref
))
2680 tree
*saved_last_vuse_ptr
= data
->last_vuse_ptr
;
2681 /* Do not update last_vuse_ptr in vn_reference_lookup_2. */
2682 data
->last_vuse_ptr
= NULL
;
2683 tree saved_vuse
= vr
->vuse
;
2684 hashval_t saved_hashcode
= vr
->hashcode
;
2685 void *res
= vn_reference_lookup_2 (ref
, gimple_vuse (def_stmt
), data
);
2686 /* Need to restore vr->vuse and vr->hashcode. */
2687 vr
->vuse
= saved_vuse
;
2688 vr
->hashcode
= saved_hashcode
;
2689 data
->last_vuse_ptr
= saved_last_vuse_ptr
;
2690 if (res
&& res
!= (void *)-1)
2692 vn_reference_t vnresult
= (vn_reference_t
) res
;
2693 tree rhs
= gimple_assign_rhs1 (def_stmt
);
2694 if (TREE_CODE (rhs
) == SSA_NAME
)
2695 rhs
= SSA_VAL (rhs
);
2696 if (vnresult
->result
2697 && operand_equal_p (vnresult
->result
, rhs
, 0)
2698 /* We have to honor our promise about union type punning
2699 and also support arbitrary overlaps with
2700 -fno-strict-aliasing. So simply resort to alignment to
2701 rule out overlaps. Do this check last because it is
2702 quite expensive compared to the hash-lookup above. */
2703 && multiple_p (get_object_alignment
2704 (ref
->ref
? ref
->ref
: data
->orig_ref
.ref
),
2706 && multiple_p (get_object_alignment (lhs
), ref
->size
))
2711 else if (*disambiguate_only
<= TR_VALUEIZE_AND_DISAMBIGUATE
2712 && gimple_call_builtin_p (def_stmt
, BUILT_IN_NORMAL
)
2713 && gimple_call_num_args (def_stmt
) <= 4)
2715 /* For builtin calls valueize its arguments and call the
2716 alias oracle again. Valueization may improve points-to
2717 info of pointers and constify size and position arguments.
2718 Originally this was motivated by PR61034 which has
2719 conditional calls to free falsely clobbering ref because
2720 of imprecise points-to info of the argument. */
2722 bool valueized_anything
= false;
2723 for (unsigned i
= 0; i
< gimple_call_num_args (def_stmt
); ++i
)
2725 oldargs
[i
] = gimple_call_arg (def_stmt
, i
);
2726 tree val
= vn_valueize (oldargs
[i
]);
2727 if (val
!= oldargs
[i
])
2729 gimple_call_set_arg (def_stmt
, i
, val
);
2730 valueized_anything
= true;
2733 if (valueized_anything
)
2735 bool res
= call_may_clobber_ref_p_1 (as_a
<gcall
*> (def_stmt
),
2737 for (unsigned i
= 0; i
< gimple_call_num_args (def_stmt
); ++i
)
2738 gimple_call_set_arg (def_stmt
, i
, oldargs
[i
]);
2741 *disambiguate_only
= TR_VALUEIZE_AND_DISAMBIGUATE
;
2747 if (*disambiguate_only
> TR_TRANSLATE
)
2750 /* If we cannot constrain the size of the reference we cannot
2751 test if anything kills it. */
2752 if (!ref
->max_size_known_p ())
2755 poly_int64 offset
= ref
->offset
;
2756 poly_int64 maxsize
= ref
->max_size
;
2758 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2759 from that definition.
2761 if (is_gimple_reg_type (vr
->type
)
2762 && (gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMSET
)
2763 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMSET_CHK
))
2764 && (integer_zerop (gimple_call_arg (def_stmt
, 1))
2765 || ((TREE_CODE (gimple_call_arg (def_stmt
, 1)) == INTEGER_CST
2766 || (INTEGRAL_TYPE_P (vr
->type
) && known_eq (ref
->size
, 8)))
2768 && BITS_PER_UNIT
== 8
2769 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
2770 && offset
.is_constant (&offseti
)
2771 && ref
->size
.is_constant (&sizei
)
2772 && (offseti
% BITS_PER_UNIT
== 0
2773 || TREE_CODE (gimple_call_arg (def_stmt
, 1)) == INTEGER_CST
)))
2774 && (poly_int_tree_p (gimple_call_arg (def_stmt
, 2))
2775 || (TREE_CODE (gimple_call_arg (def_stmt
, 2)) == SSA_NAME
2776 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt
, 2)))))
2777 && (TREE_CODE (gimple_call_arg (def_stmt
, 0)) == ADDR_EXPR
2778 || TREE_CODE (gimple_call_arg (def_stmt
, 0)) == SSA_NAME
))
2781 poly_int64 offset2
, size2
, maxsize2
;
2783 tree ref2
= gimple_call_arg (def_stmt
, 0);
2784 if (TREE_CODE (ref2
) == SSA_NAME
)
2786 ref2
= SSA_VAL (ref2
);
2787 if (TREE_CODE (ref2
) == SSA_NAME
2788 && (TREE_CODE (base
) != MEM_REF
2789 || TREE_OPERAND (base
, 0) != ref2
))
2791 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref2
);
2792 if (gimple_assign_single_p (def_stmt
)
2793 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
2794 ref2
= gimple_assign_rhs1 (def_stmt
);
2797 if (TREE_CODE (ref2
) == ADDR_EXPR
)
2799 ref2
= TREE_OPERAND (ref2
, 0);
2800 base2
= get_ref_base_and_extent (ref2
, &offset2
, &size2
, &maxsize2
,
2802 if (!known_size_p (maxsize2
)
2803 || !known_eq (maxsize2
, size2
)
2804 || !operand_equal_p (base
, base2
, OEP_ADDRESS_OF
))
2807 else if (TREE_CODE (ref2
) == SSA_NAME
)
2810 if (TREE_CODE (base
) != MEM_REF
2811 || !(mem_ref_offset (base
)
2812 << LOG2_BITS_PER_UNIT
).to_shwi (&soff
))
2816 if (TREE_OPERAND (base
, 0) != ref2
)
2818 gimple
*def
= SSA_NAME_DEF_STMT (ref2
);
2819 if (is_gimple_assign (def
)
2820 && gimple_assign_rhs_code (def
) == POINTER_PLUS_EXPR
2821 && gimple_assign_rhs1 (def
) == TREE_OPERAND (base
, 0)
2822 && poly_int_tree_p (gimple_assign_rhs2 (def
)))
2824 tree rhs2
= gimple_assign_rhs2 (def
);
2825 if (!(poly_offset_int::from (wi::to_poly_wide (rhs2
),
2827 << LOG2_BITS_PER_UNIT
).to_shwi (&offset2
))
2829 ref2
= gimple_assign_rhs1 (def
);
2830 if (TREE_CODE (ref2
) == SSA_NAME
)
2831 ref2
= SSA_VAL (ref2
);
2839 tree len
= gimple_call_arg (def_stmt
, 2);
2840 HOST_WIDE_INT leni
, offset2i
;
2841 if (TREE_CODE (len
) == SSA_NAME
)
2842 len
= SSA_VAL (len
);
2843 /* Sometimes the above trickery is smarter than alias analysis. Take
2844 advantage of that. */
2845 if (!ranges_maybe_overlap_p (offset
, maxsize
, offset2
,
2846 (wi::to_poly_offset (len
)
2847 << LOG2_BITS_PER_UNIT
)))
2849 if (data
->partial_defs
.is_empty ()
2850 && known_subrange_p (offset
, maxsize
, offset2
,
2851 wi::to_poly_offset (len
) << LOG2_BITS_PER_UNIT
))
2854 if (integer_zerop (gimple_call_arg (def_stmt
, 1)))
2855 val
= build_zero_cst (vr
->type
);
2856 else if (INTEGRAL_TYPE_P (vr
->type
)
2857 && known_eq (ref
->size
, 8)
2858 && offseti
% BITS_PER_UNIT
== 0)
2860 gimple_match_op
res_op (gimple_match_cond::UNCOND
, NOP_EXPR
,
2861 vr
->type
, gimple_call_arg (def_stmt
, 1));
2862 val
= vn_nary_build_or_lookup (&res_op
);
2864 || (TREE_CODE (val
) == SSA_NAME
2865 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
)))
2870 unsigned buflen
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr
->type
)) + 1;
2871 if (INTEGRAL_TYPE_P (vr
->type
))
2872 buflen
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr
->type
)) + 1;
2873 unsigned char *buf
= XALLOCAVEC (unsigned char, buflen
);
2874 memset (buf
, TREE_INT_CST_LOW (gimple_call_arg (def_stmt
, 1)),
2876 if (BYTES_BIG_ENDIAN
)
2879 = (((unsigned HOST_WIDE_INT
) offseti
+ sizei
)
2883 shift_bytes_in_array_right (buf
, buflen
,
2884 BITS_PER_UNIT
- amnt
);
2889 else if (offseti
% BITS_PER_UNIT
!= 0)
2892 = BITS_PER_UNIT
- ((unsigned HOST_WIDE_INT
) offseti
2894 shift_bytes_in_array_left (buf
, buflen
, amnt
);
2898 val
= native_interpret_expr (vr
->type
, buf
, buflen
);
2902 return data
->finish (0, 0, val
);
2904 /* For now handle clearing memory with partial defs. */
2905 else if (known_eq (ref
->size
, maxsize
)
2906 && integer_zerop (gimple_call_arg (def_stmt
, 1))
2907 && tree_fits_poly_int64_p (len
)
2908 && tree_to_poly_int64 (len
).is_constant (&leni
)
2909 && leni
<= INTTYPE_MAXIMUM (HOST_WIDE_INT
) / BITS_PER_UNIT
2910 && offset
.is_constant (&offseti
)
2911 && offset2
.is_constant (&offset2i
)
2912 && maxsize
.is_constant (&maxsizei
)
2913 && ranges_known_overlap_p (offseti
, maxsizei
, offset2i
,
2914 leni
<< LOG2_BITS_PER_UNIT
))
2917 pd
.rhs
= build_constructor (NULL_TREE
, NULL
);
2919 pd
.offset
= offset2i
;
2920 pd
.size
= leni
<< LOG2_BITS_PER_UNIT
;
2921 return data
->push_partial_def (pd
, 0, 0, offseti
, maxsizei
);
2925 /* 2) Assignment from an empty CONSTRUCTOR. */
2926 else if (is_gimple_reg_type (vr
->type
)
2927 && gimple_assign_single_p (def_stmt
)
2928 && gimple_assign_rhs_code (def_stmt
) == CONSTRUCTOR
2929 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt
)) == 0)
2932 poly_int64 offset2
, size2
, maxsize2
;
2933 HOST_WIDE_INT offset2i
, size2i
;
2934 gcc_assert (lhs_ref_ok
);
2935 base2
= ao_ref_base (&lhs_ref
);
2936 offset2
= lhs_ref
.offset
;
2937 size2
= lhs_ref
.size
;
2938 maxsize2
= lhs_ref
.max_size
;
2939 if (known_size_p (maxsize2
)
2940 && known_eq (maxsize2
, size2
)
2941 && adjust_offsets_for_equal_base_address (base
, &offset
,
2944 if (data
->partial_defs
.is_empty ()
2945 && known_subrange_p (offset
, maxsize
, offset2
, size2
))
2947 /* While technically undefined behavior do not optimize
2948 a full read from a clobber. */
2949 if (gimple_clobber_p (def_stmt
))
2951 tree val
= build_zero_cst (vr
->type
);
2952 return data
->finish (ao_ref_alias_set (&lhs_ref
),
2953 ao_ref_base_alias_set (&lhs_ref
), val
);
2955 else if (known_eq (ref
->size
, maxsize
)
2956 && maxsize
.is_constant (&maxsizei
)
2957 && offset
.is_constant (&offseti
)
2958 && offset2
.is_constant (&offset2i
)
2959 && size2
.is_constant (&size2i
)
2960 && ranges_known_overlap_p (offseti
, maxsizei
,
2963 /* Let clobbers be consumed by the partial-def tracker
2964 which can choose to ignore them if they are shadowed
2967 pd
.rhs
= gimple_assign_rhs1 (def_stmt
);
2969 pd
.offset
= offset2i
;
2971 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
2972 ao_ref_base_alias_set (&lhs_ref
),
2978 /* 3) Assignment from a constant. We can use folds native encode/interpret
2979 routines to extract the assigned bits. */
2980 else if (known_eq (ref
->size
, maxsize
)
2981 && is_gimple_reg_type (vr
->type
)
2982 && !reverse_storage_order_for_component_p (vr
->operands
)
2983 && !contains_storage_order_barrier_p (vr
->operands
)
2984 && gimple_assign_single_p (def_stmt
)
2986 && BITS_PER_UNIT
== 8
2987 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
2988 /* native_encode and native_decode operate on arrays of bytes
2989 and so fundamentally need a compile-time size and offset. */
2990 && maxsize
.is_constant (&maxsizei
)
2991 && offset
.is_constant (&offseti
)
2992 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
))
2993 || (TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
2994 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt
))))))
2996 tree lhs
= gimple_assign_lhs (def_stmt
);
2998 poly_int64 offset2
, size2
, maxsize2
;
2999 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 reverse
= reverse_storage_order_for_component_p (lhs
);
3009 && !storage_order_barrier_p (lhs
)
3010 && known_eq (maxsize2
, size2
)
3011 && adjust_offsets_for_equal_base_address (base
, &offset
,
3013 && offset
.is_constant (&offseti
)
3014 && offset2
.is_constant (&offset2i
)
3015 && size2
.is_constant (&size2i
))
3017 if (data
->partial_defs
.is_empty ()
3018 && known_subrange_p (offseti
, maxsizei
, offset2
, size2
))
3020 /* We support up to 512-bit values (for V8DFmode). */
3021 unsigned char buffer
[65];
3024 tree rhs
= gimple_assign_rhs1 (def_stmt
);
3025 if (TREE_CODE (rhs
) == SSA_NAME
)
3026 rhs
= SSA_VAL (rhs
);
3027 len
= native_encode_expr (rhs
,
3028 buffer
, sizeof (buffer
) - 1,
3029 (offseti
- offset2i
) / BITS_PER_UNIT
);
3030 if (len
> 0 && len
* BITS_PER_UNIT
>= maxsizei
)
3032 tree type
= vr
->type
;
3033 unsigned char *buf
= buffer
;
3034 unsigned int amnt
= 0;
3035 /* Make sure to interpret in a type that has a range
3036 covering the whole access size. */
3037 if (INTEGRAL_TYPE_P (vr
->type
)
3038 && maxsizei
!= TYPE_PRECISION (vr
->type
))
3039 type
= build_nonstandard_integer_type (maxsizei
,
3040 TYPE_UNSIGNED (type
));
3041 if (BYTES_BIG_ENDIAN
)
3043 /* For big-endian native_encode_expr stored the rhs
3044 such that the LSB of it is the LSB of buffer[len - 1].
3045 That bit is stored into memory at position
3046 offset2 + size2 - 1, i.e. in byte
3047 base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3048 E.g. for offset2 1 and size2 14, rhs -1 and memory
3049 previously cleared that is:
3052 Now, if we want to extract offset 2 and size 12 from
3053 it using native_interpret_expr (which actually works
3054 for integral bitfield types in terms of byte size of
3055 the mode), the native_encode_expr stored the value
3058 and returned len 2 (the X bits are outside of
3060 Let sz be maxsize / BITS_PER_UNIT if not extracting
3061 a bitfield, and GET_MODE_SIZE otherwise.
3062 We need to align the LSB of the value we want to
3063 extract as the LSB of buf[sz - 1].
3064 The LSB from memory we need to read is at position
3065 offset + maxsize - 1. */
3066 HOST_WIDE_INT sz
= maxsizei
/ BITS_PER_UNIT
;
3067 if (INTEGRAL_TYPE_P (type
))
3068 sz
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
3069 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
+ size2i
3070 - offseti
- maxsizei
) % BITS_PER_UNIT
;
3072 shift_bytes_in_array_right (buffer
, len
, amnt
);
3073 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
+ size2i
3074 - offseti
- maxsizei
- amnt
) / BITS_PER_UNIT
;
3075 if ((unsigned HOST_WIDE_INT
) sz
+ amnt
> (unsigned) len
)
3079 buf
= buffer
+ len
- sz
- amnt
;
3080 len
-= (buf
- buffer
);
3085 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
3086 - offseti
) % BITS_PER_UNIT
;
3090 shift_bytes_in_array_left (buffer
, len
+ 1, amnt
);
3094 tree val
= native_interpret_expr (type
, buf
, len
);
3095 /* If we chop off bits because the types precision doesn't
3096 match the memory access size this is ok when optimizing
3097 reads but not when called from the DSE code during
3100 && type
!= vr
->type
)
3102 if (! int_fits_type_p (val
, vr
->type
))
3105 val
= fold_convert (vr
->type
, val
);
3109 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3110 ao_ref_base_alias_set (&lhs_ref
), val
);
3113 else if (ranges_known_overlap_p (offseti
, maxsizei
, offset2i
,
3117 tree rhs
= gimple_assign_rhs1 (def_stmt
);
3118 if (TREE_CODE (rhs
) == SSA_NAME
)
3119 rhs
= SSA_VAL (rhs
);
3122 pd
.offset
= offset2i
;
3124 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3125 ao_ref_base_alias_set (&lhs_ref
),
3131 /* 4) Assignment from an SSA name which definition we may be able
3132 to access pieces from or we can combine to a larger entity. */
3133 else if (known_eq (ref
->size
, maxsize
)
3134 && is_gimple_reg_type (vr
->type
)
3135 && !reverse_storage_order_for_component_p (vr
->operands
)
3136 && !contains_storage_order_barrier_p (vr
->operands
)
3137 && gimple_assign_single_p (def_stmt
)
3138 && TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
)
3140 tree lhs
= gimple_assign_lhs (def_stmt
);
3142 poly_int64 offset2
, size2
, maxsize2
;
3143 HOST_WIDE_INT offset2i
, size2i
, offseti
;
3145 gcc_assert (lhs_ref_ok
);
3146 base2
= ao_ref_base (&lhs_ref
);
3147 offset2
= lhs_ref
.offset
;
3148 size2
= lhs_ref
.size
;
3149 maxsize2
= lhs_ref
.max_size
;
3150 reverse
= reverse_storage_order_for_component_p (lhs
);
3151 tree def_rhs
= gimple_assign_rhs1 (def_stmt
);
3153 && !storage_order_barrier_p (lhs
)
3154 && known_size_p (maxsize2
)
3155 && known_eq (maxsize2
, size2
)
3156 && adjust_offsets_for_equal_base_address (base
, &offset
,
3159 if (data
->partial_defs
.is_empty ()
3160 && known_subrange_p (offset
, maxsize
, offset2
, size2
)
3161 /* ??? We can't handle bitfield precision extracts without
3162 either using an alternate type for the BIT_FIELD_REF and
3163 then doing a conversion or possibly adjusting the offset
3164 according to endianness. */
3165 && (! INTEGRAL_TYPE_P (vr
->type
)
3166 || known_eq (ref
->size
, TYPE_PRECISION (vr
->type
)))
3167 && multiple_p (ref
->size
, BITS_PER_UNIT
))
3169 tree val
= NULL_TREE
;
3170 if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs
))
3171 || type_has_mode_precision_p (TREE_TYPE (def_rhs
)))
3173 gimple_match_op
op (gimple_match_cond::UNCOND
,
3174 BIT_FIELD_REF
, vr
->type
,
3176 bitsize_int (ref
->size
),
3177 bitsize_int (offset
- offset2
));
3178 val
= vn_nary_build_or_lookup (&op
);
3180 else if (known_eq (ref
->size
, size2
))
3182 gimple_match_op
op (gimple_match_cond::UNCOND
,
3183 VIEW_CONVERT_EXPR
, vr
->type
,
3185 val
= vn_nary_build_or_lookup (&op
);
3188 && (TREE_CODE (val
) != SSA_NAME
3189 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
)))
3190 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3191 ao_ref_base_alias_set (&lhs_ref
), val
);
3193 else if (maxsize
.is_constant (&maxsizei
)
3194 && offset
.is_constant (&offseti
)
3195 && offset2
.is_constant (&offset2i
)
3196 && size2
.is_constant (&size2i
)
3197 && ranges_known_overlap_p (offset
, maxsize
, offset2
, size2
))
3200 pd
.rhs
= SSA_VAL (def_rhs
);
3202 pd
.offset
= offset2i
;
3204 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3205 ao_ref_base_alias_set (&lhs_ref
),
3211 /* 4b) Assignment done via one of the vectorizer internal store
3212 functions where we may be able to access pieces from or we can
3213 combine to a larger entity. */
3214 else if (known_eq (ref
->size
, maxsize
)
3215 && is_gimple_reg_type (vr
->type
)
3216 && !reverse_storage_order_for_component_p (vr
->operands
)
3217 && !contains_storage_order_barrier_p (vr
->operands
)
3218 && is_gimple_call (def_stmt
)
3219 && gimple_call_internal_p (def_stmt
)
3220 && internal_store_fn_p (gimple_call_internal_fn (def_stmt
)))
3222 gcall
*call
= as_a
<gcall
*> (def_stmt
);
3223 internal_fn fn
= gimple_call_internal_fn (call
);
3225 tree mask
= NULL_TREE
, len
= NULL_TREE
, bias
= NULL_TREE
;
3228 case IFN_MASK_STORE
:
3229 mask
= gimple_call_arg (call
, internal_fn_mask_index (fn
));
3230 mask
= vn_valueize (mask
);
3231 if (TREE_CODE (mask
) != VECTOR_CST
)
3235 len
= gimple_call_arg (call
, 2);
3236 bias
= gimple_call_arg (call
, 4);
3237 if (!tree_fits_uhwi_p (len
) || !tree_fits_shwi_p (bias
))
3243 tree def_rhs
= gimple_call_arg (call
,
3244 internal_fn_stored_value_index (fn
));
3245 def_rhs
= vn_valueize (def_rhs
);
3246 if (TREE_CODE (def_rhs
) != VECTOR_CST
)
3249 ao_ref_init_from_ptr_and_size (&lhs_ref
,
3250 vn_valueize (gimple_call_arg (call
, 0)),
3251 TYPE_SIZE_UNIT (TREE_TYPE (def_rhs
)));
3253 poly_int64 offset2
, size2
, maxsize2
;
3254 HOST_WIDE_INT offset2i
, size2i
, offseti
;
3255 base2
= ao_ref_base (&lhs_ref
);
3256 offset2
= lhs_ref
.offset
;
3257 size2
= lhs_ref
.size
;
3258 maxsize2
= lhs_ref
.max_size
;
3259 if (known_size_p (maxsize2
)
3260 && known_eq (maxsize2
, size2
)
3261 && adjust_offsets_for_equal_base_address (base
, &offset
,
3263 && maxsize
.is_constant (&maxsizei
)
3264 && offset
.is_constant (&offseti
)
3265 && offset2
.is_constant (&offset2i
)
3266 && size2
.is_constant (&size2i
))
3268 if (!ranges_maybe_overlap_p (offset
, maxsize
, offset2
, size2
))
3269 /* Poor-mans disambiguation. */
3271 else if (ranges_known_overlap_p (offset
, maxsize
, offset2
, size2
))
3275 tree aa
= gimple_call_arg (call
, 1);
3276 alias_set_type set
= get_deref_alias_set (TREE_TYPE (aa
));
3277 tree vectype
= TREE_TYPE (def_rhs
);
3278 unsigned HOST_WIDE_INT elsz
3279 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype
)));
3282 HOST_WIDE_INT start
= 0, len
= 0;
3283 unsigned mask_idx
= 0;
3286 if (integer_zerop (VECTOR_CST_ELT (mask
, mask_idx
)))
3291 pd
.offset
= offset2i
+ start
;
3293 if (ranges_known_overlap_p
3294 (offset
, maxsize
, pd
.offset
, pd
.size
))
3296 void *res
= data
->push_partial_def
3297 (pd
, set
, set
, offseti
, maxsizei
);
3302 start
= (mask_idx
+ 1) * elsz
;
3309 while (known_lt (mask_idx
, TYPE_VECTOR_SUBPARTS (vectype
)));
3313 pd
.offset
= offset2i
+ start
;
3315 if (ranges_known_overlap_p (offset
, maxsize
,
3316 pd
.offset
, pd
.size
))
3317 return data
->push_partial_def (pd
, set
, set
,
3321 else if (fn
== IFN_LEN_STORE
)
3324 pd
.offset
= offset2i
;
3325 pd
.size
= (tree_to_uhwi (len
)
3326 + -tree_to_shwi (bias
)) * BITS_PER_UNIT
;
3327 if (ranges_known_overlap_p (offset
, maxsize
,
3328 pd
.offset
, pd
.size
))
3329 return data
->push_partial_def (pd
, set
, set
,
3339 /* 5) For aggregate copies translate the reference through them if
3340 the copy kills ref. */
3341 else if (data
->vn_walk_kind
== VN_WALKREWRITE
3342 && gimple_assign_single_p (def_stmt
)
3343 && (DECL_P (gimple_assign_rhs1 (def_stmt
))
3344 || TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == MEM_REF
3345 || handled_component_p (gimple_assign_rhs1 (def_stmt
))))
3349 auto_vec
<vn_reference_op_s
> rhs
;
3350 vn_reference_op_t vro
;
3353 gcc_assert (lhs_ref_ok
);
3355 /* See if the assignment kills REF. */
3356 base2
= ao_ref_base (&lhs_ref
);
3357 if (!lhs_ref
.max_size_known_p ()
3359 && (TREE_CODE (base
) != MEM_REF
3360 || TREE_CODE (base2
) != MEM_REF
3361 || TREE_OPERAND (base
, 0) != TREE_OPERAND (base2
, 0)
3362 || !tree_int_cst_equal (TREE_OPERAND (base
, 1),
3363 TREE_OPERAND (base2
, 1))))
3364 || !stmt_kills_ref_p (def_stmt
, ref
))
3367 /* Find the common base of ref and the lhs. lhs_ops already
3368 contains valueized operands for the lhs. */
3369 i
= vr
->operands
.length () - 1;
3370 j
= lhs_ops
.length () - 1;
3371 while (j
>= 0 && i
>= 0
3372 && vn_reference_op_eq (&vr
->operands
[i
], &lhs_ops
[j
]))
3378 /* ??? The innermost op should always be a MEM_REF and we already
3379 checked that the assignment to the lhs kills vr. Thus for
3380 aggregate copies using char[] types the vn_reference_op_eq
3381 may fail when comparing types for compatibility. But we really
3382 don't care here - further lookups with the rewritten operands
3383 will simply fail if we messed up types too badly. */
3384 poly_int64 extra_off
= 0;
3385 if (j
== 0 && i
>= 0
3386 && lhs_ops
[0].opcode
== MEM_REF
3387 && maybe_ne (lhs_ops
[0].off
, -1))
3389 if (known_eq (lhs_ops
[0].off
, vr
->operands
[i
].off
))
3391 else if (vr
->operands
[i
].opcode
== MEM_REF
3392 && maybe_ne (vr
->operands
[i
].off
, -1))
3394 extra_off
= vr
->operands
[i
].off
- lhs_ops
[0].off
;
3399 /* i now points to the first additional op.
3400 ??? LHS may not be completely contained in VR, one or more
3401 VIEW_CONVERT_EXPRs could be in its way. We could at least
3402 try handling outermost VIEW_CONVERT_EXPRs. */
3406 /* Punt if the additional ops contain a storage order barrier. */
3407 for (k
= i
; k
>= 0; k
--)
3409 vro
= &vr
->operands
[k
];
3410 if (vro
->opcode
== VIEW_CONVERT_EXPR
&& vro
->reverse
)
3414 /* Now re-write REF to be based on the rhs of the assignment. */
3415 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
3416 copy_reference_ops_from_ref (rhs1
, &rhs
);
3418 /* Apply an extra offset to the inner MEM_REF of the RHS. */
3419 bool force_no_tbaa
= false;
3420 if (maybe_ne (extra_off
, 0))
3422 if (rhs
.length () < 2)
3424 int ix
= rhs
.length () - 2;
3425 if (rhs
[ix
].opcode
!= MEM_REF
3426 || known_eq (rhs
[ix
].off
, -1))
3428 rhs
[ix
].off
+= extra_off
;
3429 rhs
[ix
].op0
= int_const_binop (PLUS_EXPR
, rhs
[ix
].op0
,
3430 build_int_cst (TREE_TYPE (rhs
[ix
].op0
),
3432 /* When we have offsetted the RHS, reading only parts of it,
3433 we can no longer use the original TBAA type, force alias-set
3435 force_no_tbaa
= true;
3438 /* Save the operands since we need to use the original ones for
3439 the hash entry we use. */
3440 if (!data
->saved_operands
.exists ())
3441 data
->saved_operands
= vr
->operands
.copy ();
3443 /* We need to pre-pend vr->operands[0..i] to rhs. */
3444 vec
<vn_reference_op_s
> old
= vr
->operands
;
3445 if (i
+ 1 + rhs
.length () > vr
->operands
.length ())
3446 vr
->operands
.safe_grow (i
+ 1 + rhs
.length (), true);
3448 vr
->operands
.truncate (i
+ 1 + rhs
.length ());
3449 FOR_EACH_VEC_ELT (rhs
, j
, vro
)
3450 vr
->operands
[i
+ 1 + j
] = *vro
;
3451 valueize_refs (&vr
->operands
);
3452 if (old
== shared_lookup_references
)
3453 shared_lookup_references
= vr
->operands
;
3454 vr
->hashcode
= vn_reference_compute_hash (vr
);
3456 /* Try folding the new reference to a constant. */
3457 tree val
= fully_constant_vn_reference_p (vr
);
3460 if (data
->partial_defs
.is_empty ())
3461 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3462 ao_ref_base_alias_set (&lhs_ref
), val
);
3463 /* This is the only interesting case for partial-def handling
3464 coming from targets that like to gimplify init-ctors as
3465 aggregate copies from constant data like aarch64 for
3467 if (maxsize
.is_constant (&maxsizei
) && known_eq (ref
->size
, maxsize
))
3474 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3475 ao_ref_base_alias_set (&lhs_ref
),
3480 /* Continuing with partial defs isn't easily possible here, we
3481 have to find a full def from further lookups from here. Probably
3482 not worth the special-casing everywhere. */
3483 if (!data
->partial_defs
.is_empty ())
3486 /* Adjust *ref from the new operands. */
3488 ao_ref_init (&rhs1_ref
, rhs1
);
3489 if (!ao_ref_init_from_vn_reference (&r
,
3491 : ao_ref_alias_set (&rhs1_ref
),
3493 : ao_ref_base_alias_set (&rhs1_ref
),
3494 vr
->type
, vr
->operands
))
3496 /* This can happen with bitfields. */
3497 if (maybe_ne (ref
->size
, r
.size
))
3499 /* If the access lacks some subsetting simply apply that by
3500 shortening it. That in the end can only be successful
3501 if we can pun the lookup result which in turn requires
3503 if (known_eq (r
.size
, r
.max_size
)
3504 && known_lt (ref
->size
, r
.size
))
3505 r
.size
= r
.max_size
= ref
->size
;
3511 /* Do not update last seen VUSE after translating. */
3512 data
->last_vuse_ptr
= NULL
;
3513 /* Invalidate the original access path since it now contains
3515 data
->orig_ref
.ref
= NULL_TREE
;
3516 /* Use the alias-set of this LHS for recording an eventual result. */
3517 if (data
->first_set
== -2)
3519 data
->first_set
= ao_ref_alias_set (&lhs_ref
);
3520 data
->first_base_set
= ao_ref_base_alias_set (&lhs_ref
);
3523 /* Keep looking for the adjusted *REF / VR pair. */
3527 /* 6) For memcpy copies translate the reference through them if the copy
3528 kills ref. But we cannot (easily) do this translation if the memcpy is
3529 a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3530 can modify the storage order of objects (see storage_order_barrier_p). */
3531 else if (data
->vn_walk_kind
== VN_WALKREWRITE
3532 && is_gimple_reg_type (vr
->type
)
3533 /* ??? Handle BCOPY as well. */
3534 && (gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMCPY
)
3535 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMCPY_CHK
)
3536 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMPCPY
)
3537 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMPCPY_CHK
)
3538 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMMOVE
)
3539 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMMOVE_CHK
))
3540 && (TREE_CODE (gimple_call_arg (def_stmt
, 0)) == ADDR_EXPR
3541 || TREE_CODE (gimple_call_arg (def_stmt
, 0)) == SSA_NAME
)
3542 && (TREE_CODE (gimple_call_arg (def_stmt
, 1)) == ADDR_EXPR
3543 || TREE_CODE (gimple_call_arg (def_stmt
, 1)) == SSA_NAME
)
3544 && (poly_int_tree_p (gimple_call_arg (def_stmt
, 2), ©_size
)
3545 || (TREE_CODE (gimple_call_arg (def_stmt
, 2)) == SSA_NAME
3546 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt
, 2)),
3548 /* Handling this is more complicated, give up for now. */
3549 && data
->partial_defs
.is_empty ())
3553 poly_int64 rhs_offset
, lhs_offset
;
3554 vn_reference_op_s op
;
3555 poly_uint64 mem_offset
;
3556 poly_int64 at
, byte_maxsize
;
3558 /* Only handle non-variable, addressable refs. */
3559 if (maybe_ne (ref
->size
, maxsize
)
3560 || !multiple_p (offset
, BITS_PER_UNIT
, &at
)
3561 || !multiple_p (maxsize
, BITS_PER_UNIT
, &byte_maxsize
))
3564 /* Extract a pointer base and an offset for the destination. */
3565 lhs
= gimple_call_arg (def_stmt
, 0);
3567 if (TREE_CODE (lhs
) == SSA_NAME
)
3569 lhs
= vn_valueize (lhs
);
3570 if (TREE_CODE (lhs
) == SSA_NAME
)
3572 gimple
*def_stmt
= SSA_NAME_DEF_STMT (lhs
);
3573 if (gimple_assign_single_p (def_stmt
)
3574 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
3575 lhs
= gimple_assign_rhs1 (def_stmt
);
3578 if (TREE_CODE (lhs
) == ADDR_EXPR
)
3580 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs
)))
3581 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs
))))
3583 tree tem
= get_addr_base_and_unit_offset (TREE_OPERAND (lhs
, 0),
3587 if (TREE_CODE (tem
) == MEM_REF
3588 && poly_int_tree_p (TREE_OPERAND (tem
, 1), &mem_offset
))
3590 lhs
= TREE_OPERAND (tem
, 0);
3591 if (TREE_CODE (lhs
) == SSA_NAME
)
3592 lhs
= vn_valueize (lhs
);
3593 lhs_offset
+= mem_offset
;
3595 else if (DECL_P (tem
))
3596 lhs
= build_fold_addr_expr (tem
);
3600 if (TREE_CODE (lhs
) != SSA_NAME
3601 && TREE_CODE (lhs
) != ADDR_EXPR
)
3604 /* Extract a pointer base and an offset for the source. */
3605 rhs
= gimple_call_arg (def_stmt
, 1);
3607 if (TREE_CODE (rhs
) == SSA_NAME
)
3608 rhs
= vn_valueize (rhs
);
3609 if (TREE_CODE (rhs
) == ADDR_EXPR
)
3611 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs
)))
3612 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs
))))
3614 tree tem
= get_addr_base_and_unit_offset (TREE_OPERAND (rhs
, 0),
3618 if (TREE_CODE (tem
) == MEM_REF
3619 && poly_int_tree_p (TREE_OPERAND (tem
, 1), &mem_offset
))
3621 rhs
= TREE_OPERAND (tem
, 0);
3622 rhs_offset
+= mem_offset
;
3624 else if (DECL_P (tem
)
3625 || TREE_CODE (tem
) == STRING_CST
)
3626 rhs
= build_fold_addr_expr (tem
);
3630 if (TREE_CODE (rhs
) == SSA_NAME
)
3631 rhs
= SSA_VAL (rhs
);
3632 else if (TREE_CODE (rhs
) != ADDR_EXPR
)
3635 /* The bases of the destination and the references have to agree. */
3636 if (TREE_CODE (base
) == MEM_REF
)
3638 if (TREE_OPERAND (base
, 0) != lhs
3639 || !poly_int_tree_p (TREE_OPERAND (base
, 1), &mem_offset
))
3643 else if (!DECL_P (base
)
3644 || TREE_CODE (lhs
) != ADDR_EXPR
3645 || TREE_OPERAND (lhs
, 0) != base
)
3648 /* If the access is completely outside of the memcpy destination
3649 area there is no aliasing. */
3650 if (!ranges_maybe_overlap_p (lhs_offset
, copy_size
, at
, byte_maxsize
))
3652 /* And the access has to be contained within the memcpy destination. */
3653 if (!known_subrange_p (at
, byte_maxsize
, lhs_offset
, copy_size
))
3656 /* Save the operands since we need to use the original ones for
3657 the hash entry we use. */
3658 if (!data
->saved_operands
.exists ())
3659 data
->saved_operands
= vr
->operands
.copy ();
3661 /* Make room for 2 operands in the new reference. */
3662 if (vr
->operands
.length () < 2)
3664 vec
<vn_reference_op_s
> old
= vr
->operands
;
3665 vr
->operands
.safe_grow_cleared (2, true);
3666 if (old
== shared_lookup_references
)
3667 shared_lookup_references
= vr
->operands
;
3670 vr
->operands
.truncate (2);
3672 /* The looked-through reference is a simple MEM_REF. */
3673 memset (&op
, 0, sizeof (op
));
3675 op
.opcode
= MEM_REF
;
3676 op
.op0
= build_int_cst (ptr_type_node
, at
- lhs_offset
+ rhs_offset
);
3677 op
.off
= at
- lhs_offset
+ rhs_offset
;
3678 vr
->operands
[0] = op
;
3679 op
.type
= TREE_TYPE (rhs
);
3680 op
.opcode
= TREE_CODE (rhs
);
3683 vr
->operands
[1] = op
;
3684 vr
->hashcode
= vn_reference_compute_hash (vr
);
3686 /* Try folding the new reference to a constant. */
3687 tree val
= fully_constant_vn_reference_p (vr
);
3689 return data
->finish (0, 0, val
);
3691 /* Adjust *ref from the new operands. */
3692 if (!ao_ref_init_from_vn_reference (&r
, 0, 0, vr
->type
, vr
->operands
))
3694 /* This can happen with bitfields. */
3695 if (maybe_ne (ref
->size
, r
.size
))
3699 /* Do not update last seen VUSE after translating. */
3700 data
->last_vuse_ptr
= NULL
;
3701 /* Invalidate the original access path since it now contains
3703 data
->orig_ref
.ref
= NULL_TREE
;
3704 /* Use the alias-set of this stmt for recording an eventual result. */
3705 if (data
->first_set
== -2)
3707 data
->first_set
= 0;
3708 data
->first_base_set
= 0;
3711 /* Keep looking for the adjusted *REF / VR pair. */
3715 /* Bail out and stop walking. */
3719 /* Return a reference op vector from OP that can be used for
3720 vn_reference_lookup_pieces. The caller is responsible for releasing
3723 vec
<vn_reference_op_s
>
3724 vn_reference_operands_for_lookup (tree op
)
3727 return valueize_shared_reference_ops_from_ref (op
, &valueized
).copy ();
3730 /* Lookup a reference operation by it's parts, in the current hash table.
3731 Returns the resulting value number if it exists in the hash table,
3732 NULL_TREE otherwise. VNRESULT will be filled in with the actual
3733 vn_reference_t stored in the hashtable if something is found. */
3736 vn_reference_lookup_pieces (tree vuse
, alias_set_type set
,
3737 alias_set_type base_set
, tree type
,
3738 vec
<vn_reference_op_s
> operands
,
3739 vn_reference_t
*vnresult
, vn_lookup_kind kind
)
3741 struct vn_reference_s vr1
;
3749 vr1
.vuse
= vuse_ssa_val (vuse
);
3750 shared_lookup_references
.truncate (0);
3751 shared_lookup_references
.safe_grow (operands
.length (), true);
3752 memcpy (shared_lookup_references
.address (),
3753 operands
.address (),
3754 sizeof (vn_reference_op_s
)
3755 * operands
.length ());
3757 valueize_refs_1 (&shared_lookup_references
, &valueized_p
);
3758 vr1
.operands
= shared_lookup_references
;
3761 vr1
.base_set
= base_set
;
3762 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
3763 if ((cst
= fully_constant_vn_reference_p (&vr1
)))
3766 vn_reference_lookup_1 (&vr1
, vnresult
);
3768 && kind
!= VN_NOWALK
3772 unsigned limit
= param_sccvn_max_alias_queries_per_access
;
3773 vn_walk_cb_data
data (&vr1
, NULL_TREE
, NULL
, kind
, true, NULL_TREE
,
3775 vec
<vn_reference_op_s
> ops_for_ref
;
3777 ops_for_ref
= vr1
.operands
;
3780 /* For ao_ref_from_mem we have to ensure only available SSA names
3781 end up in base and the only convenient way to make this work
3782 for PRE is to re-valueize with that in mind. */
3783 ops_for_ref
.create (operands
.length ());
3784 ops_for_ref
.quick_grow (operands
.length ());
3785 memcpy (ops_for_ref
.address (),
3786 operands
.address (),
3787 sizeof (vn_reference_op_s
)
3788 * operands
.length ());
3789 valueize_refs_1 (&ops_for_ref
, &valueized_p
, true);
3791 if (ao_ref_init_from_vn_reference (&r
, set
, base_set
, type
,
3795 walk_non_aliased_vuses (&r
, vr1
.vuse
, true, vn_reference_lookup_2
,
3796 vn_reference_lookup_3
, vuse_valueize
,
3798 if (ops_for_ref
!= shared_lookup_references
)
3799 ops_for_ref
.release ();
3800 gcc_checking_assert (vr1
.operands
== shared_lookup_references
);
3804 return (*vnresult
)->result
;
3809 /* Lookup OP in the current hash table, and return the resulting value
3810 number if it exists in the hash table. Return NULL_TREE if it does
3811 not exist in the hash table or if the result field of the structure
3812 was NULL.. VNRESULT will be filled in with the vn_reference_t
3813 stored in the hashtable if one exists. When TBAA_P is false assume
3814 we are looking up a store and treat it as having alias-set zero.
3815 *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
3816 MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
3817 load is bitwise anded with MASK and so we are only interested in a subset
3818 of the bits and can ignore if the other bits are uninitialized or
3819 not initialized with constants. When doing redundant store removal
3820 the caller has to set REDUNDANT_STORE_REMOVAL_P. */
3823 vn_reference_lookup (tree op
, tree vuse
, vn_lookup_kind kind
,
3824 vn_reference_t
*vnresult
, bool tbaa_p
,
3825 tree
*last_vuse_ptr
, tree mask
,
3826 bool redundant_store_removal_p
)
3828 vec
<vn_reference_op_s
> operands
;
3829 struct vn_reference_s vr1
;
3830 bool valueized_anything
;
3835 vr1
.vuse
= vuse_ssa_val (vuse
);
3836 vr1
.operands
= operands
3837 = valueize_shared_reference_ops_from_ref (op
, &valueized_anything
);
3839 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
3840 this before the pass folding __builtin_object_size had a chance to run. */
3841 if ((cfun
->curr_properties
& PROP_objsz
)
3842 && operands
[0].opcode
== ADDR_EXPR
3843 && operands
.last ().opcode
== SSA_NAME
)
3846 vn_reference_op_t vro
;
3848 for (i
= 1; operands
.iterate (i
, &vro
); ++i
)
3850 if (vro
->opcode
== SSA_NAME
)
3852 else if (known_eq (vro
->off
, -1))
3856 if (i
== operands
.length () - 1
3857 /* Make sure we the offset we accumulated in a 64bit int
3858 fits the address computation carried out in target
3859 offset precision. */
3861 == sext_hwi (off
.coeffs
[0], TYPE_PRECISION (sizetype
))))
3863 gcc_assert (operands
[i
-1].opcode
== MEM_REF
);
3865 ops
[0] = operands
[i
].op0
;
3866 ops
[1] = wide_int_to_tree (sizetype
, off
);
3867 tree res
= vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR
,
3868 TREE_TYPE (op
), ops
, NULL
);
3875 vr1
.type
= TREE_TYPE (op
);
3877 ao_ref_init (&op_ref
, op
);
3878 vr1
.set
= ao_ref_alias_set (&op_ref
);
3879 vr1
.base_set
= ao_ref_base_alias_set (&op_ref
);
3880 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
3881 if (mask
== NULL_TREE
)
3882 if (tree cst
= fully_constant_vn_reference_p (&vr1
))
3885 if (kind
!= VN_NOWALK
&& vr1
.vuse
)
3887 vn_reference_t wvnresult
;
3889 unsigned limit
= param_sccvn_max_alias_queries_per_access
;
3890 auto_vec
<vn_reference_op_s
> ops_for_ref
;
3891 if (valueized_anything
)
3893 copy_reference_ops_from_ref (op
, &ops_for_ref
);
3895 valueize_refs_1 (&ops_for_ref
, &tem
, true);
3897 /* Make sure to use a valueized reference if we valueized anything.
3898 Otherwise preserve the full reference for advanced TBAA. */
3899 if (!valueized_anything
3900 || !ao_ref_init_from_vn_reference (&r
, vr1
.set
, vr1
.base_set
,
3901 vr1
.type
, ops_for_ref
))
3902 ao_ref_init (&r
, op
);
3903 vn_walk_cb_data
data (&vr1
, r
.ref
? NULL_TREE
: op
,
3904 last_vuse_ptr
, kind
, tbaa_p
, mask
,
3905 redundant_store_removal_p
);
3909 walk_non_aliased_vuses (&r
, vr1
.vuse
, tbaa_p
, vn_reference_lookup_2
,
3910 vn_reference_lookup_3
, vuse_valueize
, limit
,
3912 gcc_checking_assert (vr1
.operands
== shared_lookup_references
);
3915 gcc_assert (mask
== NULL_TREE
);
3917 *vnresult
= wvnresult
;
3918 return wvnresult
->result
;
3921 return data
.masked_result
;
3927 *last_vuse_ptr
= vr1
.vuse
;
3930 return vn_reference_lookup_1 (&vr1
, vnresult
);
3933 /* Lookup CALL in the current hash table and return the entry in
3934 *VNRESULT if found. Populates *VR for the hashtable lookup. */
3937 vn_reference_lookup_call (gcall
*call
, vn_reference_t
*vnresult
,
3943 tree vuse
= gimple_vuse (call
);
3945 vr
->vuse
= vuse
? SSA_VAL (vuse
) : NULL_TREE
;
3946 vr
->operands
= valueize_shared_reference_ops_from_call (call
);
3947 tree lhs
= gimple_call_lhs (call
);
3948 /* For non-SSA return values the referece ops contain the LHS. */
3949 vr
->type
= ((lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
3950 ? TREE_TYPE (lhs
) : NULL_TREE
);
3954 vr
->hashcode
= vn_reference_compute_hash (vr
);
3955 vn_reference_lookup_1 (vr
, vnresult
);
3958 /* Insert OP into the current hash table with a value number of RESULT. */
3961 vn_reference_insert (tree op
, tree result
, tree vuse
, tree vdef
)
3963 vn_reference_s
**slot
;
3967 vec
<vn_reference_op_s
> operands
3968 = valueize_shared_reference_ops_from_ref (op
, &tem
);
3969 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
3970 before the pass folding __builtin_object_size had a chance to run. */
3971 if ((cfun
->curr_properties
& PROP_objsz
)
3972 && operands
[0].opcode
== ADDR_EXPR
3973 && operands
.last ().opcode
== SSA_NAME
)
3976 vn_reference_op_t vro
;
3978 for (i
= 1; operands
.iterate (i
, &vro
); ++i
)
3980 if (vro
->opcode
== SSA_NAME
)
3982 else if (known_eq (vro
->off
, -1))
3986 if (i
== operands
.length () - 1
3987 /* Make sure we the offset we accumulated in a 64bit int
3988 fits the address computation carried out in target
3989 offset precision. */
3991 == sext_hwi (off
.coeffs
[0], TYPE_PRECISION (sizetype
))))
3993 gcc_assert (operands
[i
-1].opcode
== MEM_REF
);
3995 ops
[0] = operands
[i
].op0
;
3996 ops
[1] = wide_int_to_tree (sizetype
, off
);
3997 vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR
,
3998 TREE_TYPE (op
), ops
, result
,
3999 VN_INFO (result
)->value_id
);
4004 vr1
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
4005 if (TREE_CODE (result
) == SSA_NAME
)
4006 vr1
->value_id
= VN_INFO (result
)->value_id
;
4008 vr1
->value_id
= get_or_alloc_constant_value_id (result
);
4009 vr1
->vuse
= vuse_ssa_val (vuse
);
4010 vr1
->operands
= operands
.copy ();
4011 vr1
->type
= TREE_TYPE (op
);
4012 vr1
->punned
= false;
4014 ao_ref_init (&op_ref
, op
);
4015 vr1
->set
= ao_ref_alias_set (&op_ref
);
4016 vr1
->base_set
= ao_ref_base_alias_set (&op_ref
);
4017 vr1
->hashcode
= vn_reference_compute_hash (vr1
);
4018 vr1
->result
= TREE_CODE (result
) == SSA_NAME
? SSA_VAL (result
) : result
;
4019 vr1
->result_vdef
= vdef
;
4021 slot
= valid_info
->references
->find_slot_with_hash (vr1
, vr1
->hashcode
,
4024 /* Because IL walking on reference lookup can end up visiting
4025 a def that is only to be visited later in iteration order
4026 when we are about to make an irreducible region reducible
4027 the def can be effectively processed and its ref being inserted
4028 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4029 but save a lookup if we deal with already inserted refs here. */
4032 /* We cannot assert that we have the same value either because
4033 when disentangling an irreducible region we may end up visiting
4034 a use before the corresponding def. That's a missed optimization
4035 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4036 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
4037 && !operand_equal_p ((*slot
)->result
, vr1
->result
, 0))
4039 fprintf (dump_file
, "Keeping old value ");
4040 print_generic_expr (dump_file
, (*slot
)->result
);
4041 fprintf (dump_file
, " because of collision\n");
4043 free_reference (vr1
);
4044 obstack_free (&vn_tables_obstack
, vr1
);
4049 vr1
->next
= last_inserted_ref
;
4050 last_inserted_ref
= vr1
;
4053 /* Insert a reference by it's pieces into the current hash table with
4054 a value number of RESULT. Return the resulting reference
4055 structure we created. */
4058 vn_reference_insert_pieces (tree vuse
, alias_set_type set
,
4059 alias_set_type base_set
, tree type
,
4060 vec
<vn_reference_op_s
> operands
,
4061 tree result
, unsigned int value_id
)
4064 vn_reference_s
**slot
;
4067 vr1
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
4068 vr1
->value_id
= value_id
;
4069 vr1
->vuse
= vuse_ssa_val (vuse
);
4070 vr1
->operands
= operands
;
4071 valueize_refs (&vr1
->operands
);
4073 vr1
->punned
= false;
4075 vr1
->base_set
= base_set
;
4076 vr1
->hashcode
= vn_reference_compute_hash (vr1
);
4077 if (result
&& TREE_CODE (result
) == SSA_NAME
)
4078 result
= SSA_VAL (result
);
4079 vr1
->result
= result
;
4080 vr1
->result_vdef
= NULL_TREE
;
4082 slot
= valid_info
->references
->find_slot_with_hash (vr1
, vr1
->hashcode
,
4085 /* At this point we should have all the things inserted that we have
4086 seen before, and we should never try inserting something that
4088 gcc_assert (!*slot
);
4091 vr1
->next
= last_inserted_ref
;
4092 last_inserted_ref
= vr1
;
4096 /* Compute and return the hash value for nary operation VBO1. */
4099 vn_nary_op_compute_hash (const vn_nary_op_t vno1
)
4101 inchash::hash hstate
;
4104 if (((vno1
->length
== 2
4105 && commutative_tree_code (vno1
->opcode
))
4106 || (vno1
->length
== 3
4107 && commutative_ternary_tree_code (vno1
->opcode
)))
4108 && tree_swap_operands_p (vno1
->op
[0], vno1
->op
[1]))
4109 std::swap (vno1
->op
[0], vno1
->op
[1]);
4110 else if (TREE_CODE_CLASS (vno1
->opcode
) == tcc_comparison
4111 && tree_swap_operands_p (vno1
->op
[0], vno1
->op
[1]))
4113 std::swap (vno1
->op
[0], vno1
->op
[1]);
4114 vno1
->opcode
= swap_tree_comparison (vno1
->opcode
);
4117 hstate
.add_int (vno1
->opcode
);
4118 for (i
= 0; i
< vno1
->length
; ++i
)
4119 inchash::add_expr (vno1
->op
[i
], hstate
);
4121 return hstate
.end ();
4124 /* Compare nary operations VNO1 and VNO2 and return true if they are
4128 vn_nary_op_eq (const_vn_nary_op_t
const vno1
, const_vn_nary_op_t
const vno2
)
4132 if (vno1
->hashcode
!= vno2
->hashcode
)
4135 if (vno1
->length
!= vno2
->length
)
4138 if (vno1
->opcode
!= vno2
->opcode
4139 || !types_compatible_p (vno1
->type
, vno2
->type
))
4142 for (i
= 0; i
< vno1
->length
; ++i
)
4143 if (!expressions_equal_p (vno1
->op
[i
], vno2
->op
[i
]))
4146 /* BIT_INSERT_EXPR has an implict operand as the type precision
4147 of op1. Need to check to make sure they are the same. */
4148 if (vno1
->opcode
== BIT_INSERT_EXPR
4149 && TREE_CODE (vno1
->op
[1]) == INTEGER_CST
4150 && TYPE_PRECISION (TREE_TYPE (vno1
->op
[1]))
4151 != TYPE_PRECISION (TREE_TYPE (vno2
->op
[1])))
4157 /* Initialize VNO from the pieces provided. */
4160 init_vn_nary_op_from_pieces (vn_nary_op_t vno
, unsigned int length
,
4161 enum tree_code code
, tree type
, tree
*ops
)
4164 vno
->length
= length
;
4166 memcpy (&vno
->op
[0], ops
, sizeof (tree
) * length
);
4169 /* Return the number of operands for a vn_nary ops structure from STMT. */
4172 vn_nary_length_from_stmt (gimple
*stmt
)
4174 switch (gimple_assign_rhs_code (stmt
))
4178 case VIEW_CONVERT_EXPR
:
4185 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt
));
4188 return gimple_num_ops (stmt
) - 1;
4192 /* Initialize VNO from STMT. */
4195 init_vn_nary_op_from_stmt (vn_nary_op_t vno
, gassign
*stmt
)
4199 vno
->opcode
= gimple_assign_rhs_code (stmt
);
4200 vno
->type
= TREE_TYPE (gimple_assign_lhs (stmt
));
4201 switch (vno
->opcode
)
4205 case VIEW_CONVERT_EXPR
:
4207 vno
->op
[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
4212 vno
->op
[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
4213 vno
->op
[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1);
4214 vno
->op
[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2);
4218 vno
->length
= CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt
));
4219 for (i
= 0; i
< vno
->length
; ++i
)
4220 vno
->op
[i
] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt
), i
)->value
;
4224 gcc_checking_assert (!gimple_assign_single_p (stmt
));
4225 vno
->length
= gimple_num_ops (stmt
) - 1;
4226 for (i
= 0; i
< vno
->length
; ++i
)
4227 vno
->op
[i
] = gimple_op (stmt
, i
+ 1);
4231 /* Compute the hashcode for VNO and look for it in the hash table;
4232 return the resulting value number if it exists in the hash table.
4233 Return NULL_TREE if it does not exist in the hash table or if the
4234 result field of the operation is NULL. VNRESULT will contain the
4235 vn_nary_op_t from the hashtable if it exists. */
4238 vn_nary_op_lookup_1 (vn_nary_op_t vno
, vn_nary_op_t
*vnresult
)
4240 vn_nary_op_s
**slot
;
4245 for (unsigned i
= 0; i
< vno
->length
; ++i
)
4246 if (TREE_CODE (vno
->op
[i
]) == SSA_NAME
)
4247 vno
->op
[i
] = SSA_VAL (vno
->op
[i
]);
4249 vno
->hashcode
= vn_nary_op_compute_hash (vno
);
4250 slot
= valid_info
->nary
->find_slot_with_hash (vno
, vno
->hashcode
, NO_INSERT
);
4255 return (*slot
)->predicated_values
? NULL_TREE
: (*slot
)->u
.result
;
4258 /* Lookup a n-ary operation by its pieces and return the resulting value
4259 number if it exists in the hash table. Return NULL_TREE if it does
4260 not exist in the hash table or if the result field of the operation
4261 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4265 vn_nary_op_lookup_pieces (unsigned int length
, enum tree_code code
,
4266 tree type
, tree
*ops
, vn_nary_op_t
*vnresult
)
4268 vn_nary_op_t vno1
= XALLOCAVAR (struct vn_nary_op_s
,
4269 sizeof_vn_nary_op (length
));
4270 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4271 return vn_nary_op_lookup_1 (vno1
, vnresult
);
4274 /* Lookup the rhs of STMT in the current hash table, and return the resulting
4275 value number if it exists in the hash table. Return NULL_TREE if
4276 it does not exist in the hash table. VNRESULT will contain the
4277 vn_nary_op_t from the hashtable if it exists. */
4280 vn_nary_op_lookup_stmt (gimple
*stmt
, vn_nary_op_t
*vnresult
)
4283 = XALLOCAVAR (struct vn_nary_op_s
,
4284 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt
)));
4285 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (stmt
));
4286 return vn_nary_op_lookup_1 (vno1
, vnresult
);
4289 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4292 alloc_vn_nary_op_noinit (unsigned int length
, struct obstack
*stack
)
4294 return (vn_nary_op_t
) obstack_alloc (stack
, sizeof_vn_nary_op (length
));
4297 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4301 alloc_vn_nary_op (unsigned int length
, tree result
, unsigned int value_id
)
4303 vn_nary_op_t vno1
= alloc_vn_nary_op_noinit (length
, &vn_tables_obstack
);
4305 vno1
->value_id
= value_id
;
4306 vno1
->length
= length
;
4307 vno1
->predicated_values
= 0;
4308 vno1
->u
.result
= result
;
4313 /* Insert VNO into TABLE. */
4316 vn_nary_op_insert_into (vn_nary_op_t vno
, vn_nary_op_table_type
*table
)
4318 vn_nary_op_s
**slot
;
4320 gcc_assert (! vno
->predicated_values
4321 || (! vno
->u
.values
->next
4322 && vno
->u
.values
->n
== 1));
4324 for (unsigned i
= 0; i
< vno
->length
; ++i
)
4325 if (TREE_CODE (vno
->op
[i
]) == SSA_NAME
)
4326 vno
->op
[i
] = SSA_VAL (vno
->op
[i
]);
4328 vno
->hashcode
= vn_nary_op_compute_hash (vno
);
4329 slot
= table
->find_slot_with_hash (vno
, vno
->hashcode
, INSERT
);
4330 vno
->unwind_to
= *slot
;
4333 /* Prefer non-predicated values.
4334 ??? Only if those are constant, otherwise, with constant predicated
4335 value, turn them into predicated values with entry-block validity
4336 (??? but we always find the first valid result currently). */
4337 if ((*slot
)->predicated_values
4338 && ! vno
->predicated_values
)
4340 /* ??? We cannot remove *slot from the unwind stack list.
4341 For the moment we deal with this by skipping not found
4342 entries but this isn't ideal ... */
4344 /* ??? Maintain a stack of states we can unwind in
4345 vn_nary_op_s? But how far do we unwind? In reality
4346 we need to push change records somewhere... Or not
4347 unwind vn_nary_op_s and linking them but instead
4348 unwind the results "list", linking that, which also
4349 doesn't move on hashtable resize. */
4350 /* We can also have a ->unwind_to recording *slot there.
4351 That way we can make u.values a fixed size array with
4352 recording the number of entries but of course we then
4353 have always N copies for each unwind_to-state. Or we
4354 make sure to only ever append and each unwinding will
4355 pop off one entry (but how to deal with predicated
4356 replaced with non-predicated here?) */
4357 vno
->next
= last_inserted_nary
;
4358 last_inserted_nary
= vno
;
4361 else if (vno
->predicated_values
4362 && ! (*slot
)->predicated_values
)
4364 else if (vno
->predicated_values
4365 && (*slot
)->predicated_values
)
4367 /* ??? Factor this all into a insert_single_predicated_value
4369 gcc_assert (!vno
->u
.values
->next
&& vno
->u
.values
->n
== 1);
4371 = BASIC_BLOCK_FOR_FN (cfun
, vno
->u
.values
->valid_dominated_by_p
[0]);
4372 vn_pval
*nval
= vno
->u
.values
;
4373 vn_pval
**next
= &vno
->u
.values
;
4375 for (vn_pval
*val
= (*slot
)->u
.values
; val
; val
= val
->next
)
4377 if (expressions_equal_p (val
->result
, nval
->result
))
4380 for (unsigned i
= 0; i
< val
->n
; ++i
)
4383 = BASIC_BLOCK_FOR_FN (cfun
,
4384 val
->valid_dominated_by_p
[i
]);
4385 if (dominated_by_p (CDI_DOMINATORS
, vno_bb
, val_bb
))
4386 /* Value registered with more generic predicate. */
4388 else if (flag_checking
)
4389 /* Shouldn't happen, we insert in RPO order. */
4390 gcc_assert (!dominated_by_p (CDI_DOMINATORS
,
4394 *next
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4396 + val
->n
* sizeof (int));
4397 (*next
)->next
= NULL
;
4398 (*next
)->result
= val
->result
;
4399 (*next
)->n
= val
->n
+ 1;
4400 memcpy ((*next
)->valid_dominated_by_p
,
4401 val
->valid_dominated_by_p
,
4402 val
->n
* sizeof (int));
4403 (*next
)->valid_dominated_by_p
[val
->n
] = vno_bb
->index
;
4404 next
= &(*next
)->next
;
4405 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4406 fprintf (dump_file
, "Appending predicate to value.\n");
4409 /* Copy other predicated values. */
4410 *next
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4412 + (val
->n
-1) * sizeof (int));
4413 memcpy (*next
, val
, sizeof (vn_pval
) + (val
->n
-1) * sizeof (int));
4414 (*next
)->next
= NULL
;
4415 next
= &(*next
)->next
;
4421 vno
->next
= last_inserted_nary
;
4422 last_inserted_nary
= vno
;
4426 /* While we do not want to insert things twice it's awkward to
4427 avoid it in the case where visit_nary_op pattern-matches stuff
4428 and ends up simplifying the replacement to itself. We then
4429 get two inserts, one from visit_nary_op and one from
4430 vn_nary_build_or_lookup.
4431 So allow inserts with the same value number. */
4432 if ((*slot
)->u
.result
== vno
->u
.result
)
4436 /* ??? There's also optimistic vs. previous commited state merging
4437 that is problematic for the case of unwinding. */
4439 /* ??? We should return NULL if we do not use 'vno' and have the
4440 caller release it. */
4441 gcc_assert (!*slot
);
4444 vno
->next
= last_inserted_nary
;
4445 last_inserted_nary
= vno
;
4449 /* Insert a n-ary operation into the current hash table using it's
4450 pieces. Return the vn_nary_op_t structure we created and put in
4454 vn_nary_op_insert_pieces (unsigned int length
, enum tree_code code
,
4455 tree type
, tree
*ops
,
4456 tree result
, unsigned int value_id
)
4458 vn_nary_op_t vno1
= alloc_vn_nary_op (length
, result
, value_id
);
4459 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4460 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4464 vn_nary_op_insert_pieces_predicated (unsigned int length
, enum tree_code code
,
4465 tree type
, tree
*ops
,
4466 tree result
, unsigned int value_id
,
4469 /* ??? Currently tracking BBs. */
4470 if (! single_pred_p (pred_e
->dest
))
4472 /* Never record for backedges. */
4473 if (pred_e
->flags
& EDGE_DFS_BACK
)
4478 /* Ignore backedges. */
4479 FOR_EACH_EDGE (e
, ei
, pred_e
->dest
->preds
)
4480 if (! dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
4485 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
4486 /* ??? Fix dumping, but currently we only get comparisons. */
4487 && TREE_CODE_CLASS (code
) == tcc_comparison
)
4489 fprintf (dump_file
, "Recording on edge %d->%d ", pred_e
->src
->index
,
4490 pred_e
->dest
->index
);
4491 print_generic_expr (dump_file
, ops
[0], TDF_SLIM
);
4492 fprintf (dump_file
, " %s ", get_tree_code_name (code
));
4493 print_generic_expr (dump_file
, ops
[1], TDF_SLIM
);
4494 fprintf (dump_file
, " == %s\n",
4495 integer_zerop (result
) ? "false" : "true");
4497 vn_nary_op_t vno1
= alloc_vn_nary_op (length
, NULL_TREE
, value_id
);
4498 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4499 vno1
->predicated_values
= 1;
4500 vno1
->u
.values
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4502 vno1
->u
.values
->next
= NULL
;
4503 vno1
->u
.values
->result
= result
;
4504 vno1
->u
.values
->n
= 1;
4505 vno1
->u
.values
->valid_dominated_by_p
[0] = pred_e
->dest
->index
;
4506 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4510 dominated_by_p_w_unex (basic_block bb1
, basic_block bb2
, bool);
4513 vn_nary_op_get_predicated_value (vn_nary_op_t vno
, basic_block bb
)
4515 if (! vno
->predicated_values
)
4516 return vno
->u
.result
;
4517 for (vn_pval
*val
= vno
->u
.values
; val
; val
= val
->next
)
4518 for (unsigned i
= 0; i
< val
->n
; ++i
)
4519 /* Do not handle backedge executability optimistically since
4520 when figuring out whether to iterate we do not consider
4521 changed predication. */
4522 if (dominated_by_p_w_unex
4523 (bb
, BASIC_BLOCK_FOR_FN (cfun
, val
->valid_dominated_by_p
[i
]),
4529 /* Insert the rhs of STMT into the current hash table with a value number of
4533 vn_nary_op_insert_stmt (gimple
*stmt
, tree result
)
4536 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt
),
4537 result
, VN_INFO (result
)->value_id
);
4538 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (stmt
));
4539 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4542 /* Compute a hashcode for PHI operation VP1 and return it. */
4544 static inline hashval_t
4545 vn_phi_compute_hash (vn_phi_t vp1
)
4547 inchash::hash hstate
;
4553 hstate
.add_int (EDGE_COUNT (vp1
->block
->preds
));
4554 switch (EDGE_COUNT (vp1
->block
->preds
))
4559 if (vp1
->block
->loop_father
->header
== vp1
->block
)
4565 hstate
.add_int (vp1
->block
->index
);
4568 /* If all PHI arguments are constants we need to distinguish
4569 the PHI node via its type. */
4571 hstate
.merge_hash (vn_hash_type (type
));
4573 FOR_EACH_EDGE (e
, ei
, vp1
->block
->preds
)
4575 /* Don't hash backedge values they need to be handled as VN_TOP
4576 for optimistic value-numbering. */
4577 if (e
->flags
& EDGE_DFS_BACK
)
4580 phi1op
= vp1
->phiargs
[e
->dest_idx
];
4581 if (phi1op
== VN_TOP
)
4583 inchash::add_expr (phi1op
, hstate
);
4586 return hstate
.end ();
4590 /* Return true if COND1 and COND2 represent the same condition, set
4591 *INVERTED_P if one needs to be inverted to make it the same as
4595 cond_stmts_equal_p (gcond
*cond1
, tree lhs1
, tree rhs1
,
4596 gcond
*cond2
, tree lhs2
, tree rhs2
, bool *inverted_p
)
4598 enum tree_code code1
= gimple_cond_code (cond1
);
4599 enum tree_code code2
= gimple_cond_code (cond2
);
4601 *inverted_p
= false;
4604 else if (code1
== swap_tree_comparison (code2
))
4605 std::swap (lhs2
, rhs2
);
4606 else if (code1
== invert_tree_comparison (code2
, HONOR_NANS (lhs2
)))
4608 else if (code1
== invert_tree_comparison
4609 (swap_tree_comparison (code2
), HONOR_NANS (lhs2
)))
4611 std::swap (lhs2
, rhs2
);
4617 return ((expressions_equal_p (lhs1
, lhs2
)
4618 && expressions_equal_p (rhs1
, rhs2
))
4619 || (commutative_tree_code (code1
)
4620 && expressions_equal_p (lhs1
, rhs2
)
4621 && expressions_equal_p (rhs1
, lhs2
)));
4624 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4627 vn_phi_eq (const_vn_phi_t
const vp1
, const_vn_phi_t
const vp2
)
4629 if (vp1
->hashcode
!= vp2
->hashcode
)
4632 if (vp1
->block
!= vp2
->block
)
4634 if (EDGE_COUNT (vp1
->block
->preds
) != EDGE_COUNT (vp2
->block
->preds
))
4637 switch (EDGE_COUNT (vp1
->block
->preds
))
4640 /* Single-arg PHIs are just copies. */
4645 /* Rule out backedges into the PHI. */
4646 if (vp1
->block
->loop_father
->header
== vp1
->block
4647 || vp2
->block
->loop_father
->header
== vp2
->block
)
4650 /* If the PHI nodes do not have compatible types
4651 they are not the same. */
4652 if (!types_compatible_p (vp1
->type
, vp2
->type
))
4656 = get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4658 = get_immediate_dominator (CDI_DOMINATORS
, vp2
->block
);
4659 /* If the immediate dominator end in switch stmts multiple
4660 values may end up in the same PHI arg via intermediate
4662 if (EDGE_COUNT (idom1
->succs
) != 2
4663 || EDGE_COUNT (idom2
->succs
) != 2)
4666 /* Verify the controlling stmt is the same. */
4667 gcond
*last1
= safe_dyn_cast
<gcond
*> (last_stmt (idom1
));
4668 gcond
*last2
= safe_dyn_cast
<gcond
*> (last_stmt (idom2
));
4669 if (! last1
|| ! last2
)
4672 if (! cond_stmts_equal_p (last1
, vp1
->cclhs
, vp1
->ccrhs
,
4673 last2
, vp2
->cclhs
, vp2
->ccrhs
,
4677 /* Get at true/false controlled edges into the PHI. */
4678 edge te1
, te2
, fe1
, fe2
;
4679 if (! extract_true_false_controlled_edges (idom1
, vp1
->block
,
4681 || ! extract_true_false_controlled_edges (idom2
, vp2
->block
,
4685 /* Swap edges if the second condition is the inverted of the
4688 std::swap (te2
, fe2
);
4690 /* Since we do not know which edge will be executed we have
4691 to be careful when matching VN_TOP. Be conservative and
4692 only match VN_TOP == VN_TOP for now, we could allow
4693 VN_TOP on the not prevailing PHI though. See for example
4695 if (! expressions_equal_p (vp1
->phiargs
[te1
->dest_idx
],
4696 vp2
->phiargs
[te2
->dest_idx
], false)
4697 || ! expressions_equal_p (vp1
->phiargs
[fe1
->dest_idx
],
4698 vp2
->phiargs
[fe2
->dest_idx
], false))
4709 /* If the PHI nodes do not have compatible types
4710 they are not the same. */
4711 if (!types_compatible_p (vp1
->type
, vp2
->type
))
4714 /* Any phi in the same block will have it's arguments in the
4715 same edge order, because of how we store phi nodes. */
4716 unsigned nargs
= EDGE_COUNT (vp1
->block
->preds
);
4717 for (unsigned i
= 0; i
< nargs
; ++i
)
4719 tree phi1op
= vp1
->phiargs
[i
];
4720 tree phi2op
= vp2
->phiargs
[i
];
4721 if (phi1op
== phi2op
)
4723 if (!expressions_equal_p (phi1op
, phi2op
, false))
4730 /* Lookup PHI in the current hash table, and return the resulting
4731 value number if it exists in the hash table. Return NULL_TREE if
4732 it does not exist in the hash table. */
4735 vn_phi_lookup (gimple
*phi
, bool backedges_varying_p
)
4738 struct vn_phi_s
*vp1
;
4742 vp1
= XALLOCAVAR (struct vn_phi_s
,
4743 sizeof (struct vn_phi_s
)
4744 + (gimple_phi_num_args (phi
) - 1) * sizeof (tree
));
4746 /* Canonicalize the SSA_NAME's to their value number. */
4747 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
4749 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4750 if (TREE_CODE (def
) == SSA_NAME
4751 && (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
)))
4753 if (ssa_undefined_value_p (def
, false))
4756 def
= SSA_VAL (def
);
4758 vp1
->phiargs
[e
->dest_idx
] = def
;
4760 vp1
->type
= TREE_TYPE (gimple_phi_result (phi
));
4761 vp1
->block
= gimple_bb (phi
);
4762 /* Extract values of the controlling condition. */
4763 vp1
->cclhs
= NULL_TREE
;
4764 vp1
->ccrhs
= NULL_TREE
;
4765 basic_block idom1
= get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4766 if (EDGE_COUNT (idom1
->succs
) == 2)
4767 if (gcond
*last1
= safe_dyn_cast
<gcond
*> (last_stmt (idom1
)))
4769 /* ??? We want to use SSA_VAL here. But possibly not
4771 vp1
->cclhs
= vn_valueize (gimple_cond_lhs (last1
));
4772 vp1
->ccrhs
= vn_valueize (gimple_cond_rhs (last1
));
4774 vp1
->hashcode
= vn_phi_compute_hash (vp1
);
4775 slot
= valid_info
->phis
->find_slot_with_hash (vp1
, vp1
->hashcode
, NO_INSERT
);
4778 return (*slot
)->result
;
4781 /* Insert PHI into the current hash table with a value number of
4785 vn_phi_insert (gimple
*phi
, tree result
, bool backedges_varying_p
)
4788 vn_phi_t vp1
= (vn_phi_t
) obstack_alloc (&vn_tables_obstack
,
4790 + ((gimple_phi_num_args (phi
) - 1)
4795 /* Canonicalize the SSA_NAME's to their value number. */
4796 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
4798 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4799 if (TREE_CODE (def
) == SSA_NAME
4800 && (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
)))
4802 if (ssa_undefined_value_p (def
, false))
4805 def
= SSA_VAL (def
);
4807 vp1
->phiargs
[e
->dest_idx
] = def
;
4809 vp1
->value_id
= VN_INFO (result
)->value_id
;
4810 vp1
->type
= TREE_TYPE (gimple_phi_result (phi
));
4811 vp1
->block
= gimple_bb (phi
);
4812 /* Extract values of the controlling condition. */
4813 vp1
->cclhs
= NULL_TREE
;
4814 vp1
->ccrhs
= NULL_TREE
;
4815 basic_block idom1
= get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4816 if (EDGE_COUNT (idom1
->succs
) == 2)
4817 if (gcond
*last1
= safe_dyn_cast
<gcond
*> (last_stmt (idom1
)))
4819 /* ??? We want to use SSA_VAL here. But possibly not
4821 vp1
->cclhs
= vn_valueize (gimple_cond_lhs (last1
));
4822 vp1
->ccrhs
= vn_valueize (gimple_cond_rhs (last1
));
4824 vp1
->result
= result
;
4825 vp1
->hashcode
= vn_phi_compute_hash (vp1
);
4827 slot
= valid_info
->phis
->find_slot_with_hash (vp1
, vp1
->hashcode
, INSERT
);
4828 gcc_assert (!*slot
);
4831 vp1
->next
= last_inserted_phi
;
4832 last_inserted_phi
= vp1
;
4837 /* Return true if BB1 is dominated by BB2 taking into account edges
4838 that are not executable. When ALLOW_BACK is false consider not
4839 executable backedges as executable. */
4842 dominated_by_p_w_unex (basic_block bb1
, basic_block bb2
, bool allow_back
)
4847 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
4850 /* Before iterating we'd like to know if there exists a
4851 (executable) path from bb2 to bb1 at all, if not we can
4852 directly return false. For now simply iterate once. */
4854 /* Iterate to the single executable bb1 predecessor. */
4855 if (EDGE_COUNT (bb1
->preds
) > 1)
4858 FOR_EACH_EDGE (e
, ei
, bb1
->preds
)
4859 if ((e
->flags
& EDGE_EXECUTABLE
)
4860 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
)))
4873 /* Re-do the dominance check with changed bb1. */
4874 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
4879 /* Iterate to the single executable bb2 successor. */
4880 if (EDGE_COUNT (bb2
->succs
) > 1)
4883 FOR_EACH_EDGE (e
, ei
, bb2
->succs
)
4884 if ((e
->flags
& EDGE_EXECUTABLE
)
4885 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
)))
4896 /* Verify the reached block is only reached through succe.
4897 If there is only one edge we can spare us the dominator
4898 check and iterate directly. */
4899 if (EDGE_COUNT (succe
->dest
->preds
) > 1)
4901 FOR_EACH_EDGE (e
, ei
, succe
->dest
->preds
)
4903 && ((e
->flags
& EDGE_EXECUTABLE
)
4904 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
))))
4914 /* Re-do the dominance check with changed bb2. */
4915 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
4921 /* We could now iterate updating bb1 / bb2. */
4925 /* Set the value number of FROM to TO, return true if it has changed
4929 set_ssa_val_to (tree from
, tree to
)
4931 vn_ssa_aux_t from_info
= VN_INFO (from
);
4932 tree currval
= from_info
->valnum
; // SSA_VAL (from)
4933 poly_int64 toff
, coff
;
4934 bool curr_undefined
= false;
4935 bool curr_invariant
= false;
4937 /* The only thing we allow as value numbers are ssa_names
4938 and invariants. So assert that here. We don't allow VN_TOP
4939 as visiting a stmt should produce a value-number other than
4941 ??? Still VN_TOP can happen for unreachable code, so force
4942 it to varying in that case. Not all code is prepared to
4943 get VN_TOP on valueization. */
4946 /* ??? When iterating and visiting PHI <undef, backedge-value>
4947 for the first time we rightfully get VN_TOP and we need to
4948 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
4949 With SCCVN we were simply lucky we iterated the other PHI
4950 cycles first and thus visited the backedge-value DEF. */
4951 if (currval
== VN_TOP
)
4953 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4954 fprintf (dump_file
, "Forcing value number to varying on "
4955 "receiving VN_TOP\n");
4959 gcc_checking_assert (to
!= NULL_TREE
4960 && ((TREE_CODE (to
) == SSA_NAME
4961 && (to
== from
|| SSA_VAL (to
) == to
))
4962 || is_gimple_min_invariant (to
)));
4966 if (currval
== from
)
4968 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4970 fprintf (dump_file
, "Not changing value number of ");
4971 print_generic_expr (dump_file
, from
);
4972 fprintf (dump_file
, " from VARYING to ");
4973 print_generic_expr (dump_file
, to
);
4974 fprintf (dump_file
, "\n");
4978 curr_invariant
= is_gimple_min_invariant (currval
);
4979 curr_undefined
= (TREE_CODE (currval
) == SSA_NAME
4980 && ssa_undefined_value_p (currval
, false));
4981 if (currval
!= VN_TOP
4984 && is_gimple_min_invariant (to
))
4986 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4988 fprintf (dump_file
, "Forcing VARYING instead of changing "
4989 "value number of ");
4990 print_generic_expr (dump_file
, from
);
4991 fprintf (dump_file
, " from ");
4992 print_generic_expr (dump_file
, currval
);
4993 fprintf (dump_file
, " (non-constant) to ");
4994 print_generic_expr (dump_file
, to
);
4995 fprintf (dump_file
, " (constant)\n");
4999 else if (currval
!= VN_TOP
5001 && TREE_CODE (to
) == SSA_NAME
5002 && ssa_undefined_value_p (to
, false))
5004 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5006 fprintf (dump_file
, "Forcing VARYING instead of changing "
5007 "value number of ");
5008 print_generic_expr (dump_file
, from
);
5009 fprintf (dump_file
, " from ");
5010 print_generic_expr (dump_file
, currval
);
5011 fprintf (dump_file
, " (non-undefined) to ");
5012 print_generic_expr (dump_file
, to
);
5013 fprintf (dump_file
, " (undefined)\n");
5017 else if (TREE_CODE (to
) == SSA_NAME
5018 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to
))
5023 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5025 fprintf (dump_file
, "Setting value number of ");
5026 print_generic_expr (dump_file
, from
);
5027 fprintf (dump_file
, " to ");
5028 print_generic_expr (dump_file
, to
);
5032 && !operand_equal_p (currval
, to
, 0)
5033 /* Different undefined SSA names are not actually different. See
5034 PR82320 for a testcase were we'd otherwise not terminate iteration. */
5036 && TREE_CODE (to
) == SSA_NAME
5037 && ssa_undefined_value_p (to
, false))
5038 /* ??? For addresses involving volatile objects or types operand_equal_p
5039 does not reliably detect ADDR_EXPRs as equal. We know we are only
5040 getting invariant gimple addresses here, so can use
5041 get_addr_base_and_unit_offset to do this comparison. */
5042 && !(TREE_CODE (currval
) == ADDR_EXPR
5043 && TREE_CODE (to
) == ADDR_EXPR
5044 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval
, 0), &coff
)
5045 == get_addr_base_and_unit_offset (TREE_OPERAND (to
, 0), &toff
))
5046 && known_eq (coff
, toff
)))
5049 && currval
!= VN_TOP
5051 /* We do not want to allow lattice transitions from one value
5052 to another since that may lead to not terminating iteration
5053 (see PR95049). Since there's no convenient way to check
5054 for the allowed transition of VAL -> PHI (loop entry value,
5055 same on two PHIs, to same PHI result) we restrict the check
5058 && is_gimple_min_invariant (to
))
5060 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5061 fprintf (dump_file
, " forced VARYING");
5064 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5065 fprintf (dump_file
, " (changed)\n");
5066 from_info
->valnum
= to
;
5069 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5070 fprintf (dump_file
, "\n");
5074 /* Set all definitions in STMT to value number to themselves.
5075 Return true if a value number changed. */
5078 defs_to_varying (gimple
*stmt
)
5080 bool changed
= false;
5084 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_ALL_DEFS
)
5086 tree def
= DEF_FROM_PTR (defp
);
5087 changed
|= set_ssa_val_to (def
, def
);
5092 /* Visit a copy between LHS and RHS, return true if the value number
5096 visit_copy (tree lhs
, tree rhs
)
5099 rhs
= SSA_VAL (rhs
);
5101 return set_ssa_val_to (lhs
, rhs
);
5104 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5108 valueized_wider_op (tree wide_type
, tree op
, bool allow_truncate
)
5110 if (TREE_CODE (op
) == SSA_NAME
)
5111 op
= vn_valueize (op
);
5113 /* Either we have the op widened available. */
5116 tree tem
= vn_nary_op_lookup_pieces (1, NOP_EXPR
,
5117 wide_type
, ops
, NULL
);
5121 /* Or the op is truncated from some existing value. */
5122 if (allow_truncate
&& TREE_CODE (op
) == SSA_NAME
)
5124 gimple
*def
= SSA_NAME_DEF_STMT (op
);
5125 if (is_gimple_assign (def
)
5126 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def
)))
5128 tem
= gimple_assign_rhs1 (def
);
5129 if (useless_type_conversion_p (wide_type
, TREE_TYPE (tem
)))
5131 if (TREE_CODE (tem
) == SSA_NAME
)
5132 tem
= vn_valueize (tem
);
5138 /* For constants simply extend it. */
5139 if (TREE_CODE (op
) == INTEGER_CST
)
5140 return wide_int_to_tree (wide_type
, wi::to_widest (op
));
5145 /* Visit a nary operator RHS, value number it, and return true if the
5146 value number of LHS has changed as a result. */
5149 visit_nary_op (tree lhs
, gassign
*stmt
)
5151 vn_nary_op_t vnresult
;
5152 tree result
= vn_nary_op_lookup_stmt (stmt
, &vnresult
);
5153 if (! result
&& vnresult
)
5154 result
= vn_nary_op_get_predicated_value (vnresult
, gimple_bb (stmt
));
5156 return set_ssa_val_to (lhs
, result
);
5158 /* Do some special pattern matching for redundancies of operations
5159 in different types. */
5160 enum tree_code code
= gimple_assign_rhs_code (stmt
);
5161 tree type
= TREE_TYPE (lhs
);
5162 tree rhs1
= gimple_assign_rhs1 (stmt
);
5166 /* Match arithmetic done in a different type where we can easily
5167 substitute the result from some earlier sign-changed or widened
5169 if (INTEGRAL_TYPE_P (type
)
5170 && TREE_CODE (rhs1
) == SSA_NAME
5171 /* We only handle sign-changes, zero-extension -> & mask or
5172 sign-extension if we know the inner operation doesn't
5174 && (((TYPE_UNSIGNED (TREE_TYPE (rhs1
))
5175 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
5176 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1
))))
5177 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
5178 || TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (rhs1
))))
5180 gassign
*def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (rhs1
));
5182 && (gimple_assign_rhs_code (def
) == PLUS_EXPR
5183 || gimple_assign_rhs_code (def
) == MINUS_EXPR
5184 || gimple_assign_rhs_code (def
) == MULT_EXPR
))
5187 /* When requiring a sign-extension we cannot model a
5188 previous truncation with a single op so don't bother. */
5189 bool allow_truncate
= TYPE_UNSIGNED (TREE_TYPE (rhs1
));
5190 /* Either we have the op widened available. */
5191 ops
[0] = valueized_wider_op (type
, gimple_assign_rhs1 (def
),
5194 ops
[1] = valueized_wider_op (type
, gimple_assign_rhs2 (def
),
5196 if (ops
[0] && ops
[1])
5198 ops
[0] = vn_nary_op_lookup_pieces
5199 (2, gimple_assign_rhs_code (def
), type
, ops
, NULL
);
5200 /* We have wider operation available. */
5202 /* If the leader is a wrapping operation we can
5203 insert it for code hoisting w/o introducing
5204 undefined overflow. If it is not it has to
5205 be available. See PR86554. */
5206 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops
[0]))
5207 || (rpo_avail
&& vn_context_bb
5208 && rpo_avail
->eliminate_avail (vn_context_bb
,
5211 unsigned lhs_prec
= TYPE_PRECISION (type
);
5212 unsigned rhs_prec
= TYPE_PRECISION (TREE_TYPE (rhs1
));
5213 if (lhs_prec
== rhs_prec
5214 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
5215 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1
))))
5217 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5218 NOP_EXPR
, type
, ops
[0]);
5219 result
= vn_nary_build_or_lookup (&match_op
);
5222 bool changed
= set_ssa_val_to (lhs
, result
);
5223 vn_nary_op_insert_stmt (stmt
, result
);
5229 tree mask
= wide_int_to_tree
5230 (type
, wi::mask (rhs_prec
, false, lhs_prec
));
5231 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5235 result
= vn_nary_build_or_lookup (&match_op
);
5238 bool changed
= set_ssa_val_to (lhs
, result
);
5239 vn_nary_op_insert_stmt (stmt
, result
);
5249 if (INTEGRAL_TYPE_P (type
)
5250 && TREE_CODE (rhs1
) == SSA_NAME
5251 && TREE_CODE (gimple_assign_rhs2 (stmt
)) == INTEGER_CST
5252 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
)
5253 && default_vn_walk_kind
!= VN_NOWALK
5255 && BITS_PER_UNIT
== 8
5256 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
5257 && !integer_all_onesp (gimple_assign_rhs2 (stmt
))
5258 && !integer_zerop (gimple_assign_rhs2 (stmt
)))
5260 gassign
*ass
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (rhs1
));
5262 && !gimple_has_volatile_ops (ass
)
5263 && vn_get_stmt_kind (ass
) == VN_REFERENCE
)
5265 tree last_vuse
= gimple_vuse (ass
);
5266 tree op
= gimple_assign_rhs1 (ass
);
5267 tree result
= vn_reference_lookup (op
, gimple_vuse (ass
),
5268 default_vn_walk_kind
,
5269 NULL
, true, &last_vuse
,
5270 gimple_assign_rhs2 (stmt
));
5272 && useless_type_conversion_p (TREE_TYPE (result
),
5274 return set_ssa_val_to (lhs
, result
);
5278 case TRUNC_DIV_EXPR
:
5279 if (TYPE_UNSIGNED (type
))
5284 /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5285 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type
))
5289 rhs
[1] = gimple_assign_rhs2 (stmt
);
5290 for (unsigned i
= 0; i
<= 1; ++i
)
5292 unsigned j
= i
== 0 ? 1 : 0;
5294 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5295 NEGATE_EXPR
, type
, rhs
[i
]);
5296 ops
[i
] = vn_nary_build_or_lookup_1 (&match_op
, false, true);
5299 && (ops
[0] = vn_nary_op_lookup_pieces (2, code
,
5302 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5303 NEGATE_EXPR
, type
, ops
[0]);
5304 result
= vn_nary_build_or_lookup_1 (&match_op
, true, false);
5307 bool changed
= set_ssa_val_to (lhs
, result
);
5308 vn_nary_op_insert_stmt (stmt
, result
);
5319 bool changed
= set_ssa_val_to (lhs
, lhs
);
5320 vn_nary_op_insert_stmt (stmt
, lhs
);
5324 /* Visit a call STMT storing into LHS. Return true if the value number
5325 of the LHS has changed as a result. */
5328 visit_reference_op_call (tree lhs
, gcall
*stmt
)
5330 bool changed
= false;
5331 struct vn_reference_s vr1
;
5332 vn_reference_t vnresult
= NULL
;
5333 tree vdef
= gimple_vdef (stmt
);
5334 modref_summary
*summary
;
5336 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5337 if (lhs
&& TREE_CODE (lhs
) != SSA_NAME
)
5340 vn_reference_lookup_call (stmt
, &vnresult
, &vr1
);
5342 /* If the lookup did not succeed for pure functions try to use
5343 modref info to find a candidate to CSE to. */
5344 const unsigned accesses_limit
= 8;
5348 && gimple_vuse (stmt
)
5349 && (((summary
= get_modref_function_summary (stmt
, NULL
))
5350 && !summary
->global_memory_read
5351 && summary
->load_accesses
< accesses_limit
)
5352 || gimple_call_flags (stmt
) & ECF_CONST
))
5354 /* First search if we can do someting useful and build a
5355 vector of all loads we have to check. */
5356 bool unknown_memory_access
= false;
5357 auto_vec
<ao_ref
, accesses_limit
> accesses
;
5358 unsigned load_accesses
= summary
? summary
->load_accesses
: 0;
5359 if (!unknown_memory_access
)
5360 /* Add loads done as part of setting up the call arguments.
5361 That's also necessary for CONST functions which will
5362 not have a modref summary. */
5363 for (unsigned i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
5365 tree arg
= gimple_call_arg (stmt
, i
);
5366 if (TREE_CODE (arg
) != SSA_NAME
5367 && !is_gimple_min_invariant (arg
))
5369 if (accesses
.length () >= accesses_limit
- load_accesses
)
5371 unknown_memory_access
= true;
5374 accesses
.quick_grow (accesses
.length () + 1);
5375 ao_ref_init (&accesses
.last (), arg
);
5378 if (summary
&& !unknown_memory_access
)
5380 /* Add loads as analyzed by IPA modref. */
5381 for (auto base_node
: summary
->loads
->bases
)
5382 if (unknown_memory_access
)
5384 else for (auto ref_node
: base_node
->refs
)
5385 if (unknown_memory_access
)
5387 else for (auto access_node
: ref_node
->accesses
)
5389 accesses
.quick_grow (accesses
.length () + 1);
5390 ao_ref
*r
= &accesses
.last ();
5391 if (!access_node
.get_ao_ref (stmt
, r
))
5393 /* Initialize a ref based on the argument and
5394 unknown offset if possible. */
5395 tree arg
= access_node
.get_call_arg (stmt
);
5396 if (arg
&& TREE_CODE (arg
) == SSA_NAME
)
5397 arg
= SSA_VAL (arg
);
5399 && TREE_CODE (arg
) == ADDR_EXPR
5400 && (arg
= get_base_address (arg
))
5403 ao_ref_init (r
, arg
);
5409 unknown_memory_access
= true;
5413 r
->base_alias_set
= base_node
->base
;
5414 r
->ref_alias_set
= ref_node
->ref
;
5418 /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5419 for the call in the hashtable. */
5420 unsigned limit
= (unknown_memory_access
5422 : (param_sccvn_max_alias_queries_per_access
5423 / (accesses
.length () + 1)));
5424 tree saved_vuse
= vr1
.vuse
;
5425 hashval_t saved_hashcode
= vr1
.hashcode
;
5426 while (limit
> 0 && !vnresult
&& !SSA_NAME_IS_DEFAULT_DEF (vr1
.vuse
))
5428 vr1
.hashcode
= vr1
.hashcode
- SSA_NAME_VERSION (vr1
.vuse
);
5429 gimple
*def
= SSA_NAME_DEF_STMT (vr1
.vuse
);
5430 /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5431 do not bother for now. */
5432 if (is_a
<gphi
*> (def
))
5434 vr1
.vuse
= vuse_ssa_val (gimple_vuse (def
));
5435 vr1
.hashcode
= vr1
.hashcode
+ SSA_NAME_VERSION (vr1
.vuse
);
5436 vn_reference_lookup_1 (&vr1
, &vnresult
);
5440 /* If we found a candidate to CSE to verify it is valid. */
5441 if (vnresult
&& !accesses
.is_empty ())
5443 tree vuse
= vuse_ssa_val (gimple_vuse (stmt
));
5444 while (vnresult
&& vuse
!= vr1
.vuse
)
5446 gimple
*def
= SSA_NAME_DEF_STMT (vuse
);
5447 for (auto &ref
: accesses
)
5449 /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5450 analysis overhead that we might be able to cache. */
5451 if (stmt_may_clobber_ref_p_1 (def
, &ref
, true))
5457 vuse
= vuse_ssa_val (gimple_vuse (def
));
5460 vr1
.vuse
= saved_vuse
;
5461 vr1
.hashcode
= saved_hashcode
;
5468 if (vnresult
->result_vdef
)
5469 changed
|= set_ssa_val_to (vdef
, vnresult
->result_vdef
);
5470 else if (!lhs
&& gimple_call_lhs (stmt
))
5471 /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5472 as the call still acts as a lhs store. */
5473 changed
|= set_ssa_val_to (vdef
, vdef
);
5475 /* If the call was discovered to be pure or const reflect
5476 that as far as possible. */
5477 changed
|= set_ssa_val_to (vdef
,
5478 vuse_ssa_val (gimple_vuse (stmt
)));
5481 if (!vnresult
->result
&& lhs
)
5482 vnresult
->result
= lhs
;
5484 if (vnresult
->result
&& lhs
)
5485 changed
|= set_ssa_val_to (lhs
, vnresult
->result
);
5490 vn_reference_s
**slot
;
5491 tree vdef_val
= vdef
;
5494 /* If we value numbered an indirect functions function to
5495 one not clobbering memory value number its VDEF to its
5497 tree fn
= gimple_call_fn (stmt
);
5498 if (fn
&& TREE_CODE (fn
) == SSA_NAME
)
5501 if (TREE_CODE (fn
) == ADDR_EXPR
5502 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
5503 && (flags_from_decl_or_type (TREE_OPERAND (fn
, 0))
5504 & (ECF_CONST
| ECF_PURE
))
5505 /* If stmt has non-SSA_NAME lhs, value number the
5506 vdef to itself, as the call still acts as a lhs
5508 && (lhs
|| gimple_call_lhs (stmt
) == NULL_TREE
))
5509 vdef_val
= vuse_ssa_val (gimple_vuse (stmt
));
5511 changed
|= set_ssa_val_to (vdef
, vdef_val
);
5514 changed
|= set_ssa_val_to (lhs
, lhs
);
5515 vr2
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
5516 vr2
->vuse
= vr1
.vuse
;
5517 /* As we are not walking the virtual operand chain we know the
5518 shared_lookup_references are still original so we can re-use
5520 vr2
->operands
= vr1
.operands
.copy ();
5521 vr2
->type
= vr1
.type
;
5522 vr2
->punned
= vr1
.punned
;
5524 vr2
->base_set
= vr1
.base_set
;
5525 vr2
->hashcode
= vr1
.hashcode
;
5527 vr2
->result_vdef
= vdef_val
;
5529 slot
= valid_info
->references
->find_slot_with_hash (vr2
, vr2
->hashcode
,
5531 gcc_assert (!*slot
);
5533 vr2
->next
= last_inserted_ref
;
5534 last_inserted_ref
= vr2
;
5540 /* Visit a load from a reference operator RHS, part of STMT, value number it,
5541 and return true if the value number of the LHS has changed as a result. */
5544 visit_reference_op_load (tree lhs
, tree op
, gimple
*stmt
)
5546 bool changed
= false;
5550 tree vuse
= gimple_vuse (stmt
);
5551 tree last_vuse
= vuse
;
5552 result
= vn_reference_lookup (op
, vuse
, default_vn_walk_kind
, &res
, true, &last_vuse
);
5554 /* We handle type-punning through unions by value-numbering based
5555 on offset and size of the access. Be prepared to handle a
5556 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5558 && !useless_type_conversion_p (TREE_TYPE (result
), TREE_TYPE (op
)))
5560 /* Avoid the type punning in case the result mode has padding where
5561 the op we lookup has not. */
5562 if (maybe_lt (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (result
))),
5563 GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op
)))))
5567 /* We will be setting the value number of lhs to the value number
5568 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5569 So first simplify and lookup this expression to see if it
5570 is already available. */
5571 gimple_match_op
res_op (gimple_match_cond::UNCOND
,
5572 VIEW_CONVERT_EXPR
, TREE_TYPE (op
), result
);
5573 result
= vn_nary_build_or_lookup (&res_op
);
5575 && TREE_CODE (result
) == SSA_NAME
5576 && VN_INFO (result
)->needs_insertion
)
5577 /* Track whether this is the canonical expression for different
5578 typed loads. We use that as a stopgap measure for code
5579 hoisting when dealing with floating point loads. */
5583 /* When building the conversion fails avoid inserting the reference
5586 return set_ssa_val_to (lhs
, lhs
);
5590 changed
= set_ssa_val_to (lhs
, result
);
5593 changed
= set_ssa_val_to (lhs
, lhs
);
5594 vn_reference_insert (op
, lhs
, last_vuse
, NULL_TREE
);
5595 if (vuse
&& SSA_VAL (last_vuse
) != SSA_VAL (vuse
))
5597 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5599 fprintf (dump_file
, "Using extra use virtual operand ");
5600 print_generic_expr (dump_file
, last_vuse
);
5601 fprintf (dump_file
, "\n");
5603 vn_reference_insert (op
, lhs
, vuse
, NULL_TREE
);
5611 /* Visit a store to a reference operator LHS, part of STMT, value number it,
5612 and return true if the value number of the LHS has changed as a result. */
5615 visit_reference_op_store (tree lhs
, tree op
, gimple
*stmt
)
5617 bool changed
= false;
5618 vn_reference_t vnresult
= NULL
;
5620 bool resultsame
= false;
5621 tree vuse
= gimple_vuse (stmt
);
5622 tree vdef
= gimple_vdef (stmt
);
5624 if (TREE_CODE (op
) == SSA_NAME
)
5627 /* First we want to lookup using the *vuses* from the store and see
5628 if there the last store to this location with the same address
5631 The vuses represent the memory state before the store. If the
5632 memory state, address, and value of the store is the same as the
5633 last store to this location, then this store will produce the
5634 same memory state as that store.
5636 In this case the vdef versions for this store are value numbered to those
5637 vuse versions, since they represent the same memory state after
5640 Otherwise, the vdefs for the store are used when inserting into
5641 the table, since the store generates a new memory state. */
5643 vn_reference_lookup (lhs
, vuse
, VN_NOWALK
, &vnresult
, false);
5645 && vnresult
->result
)
5647 tree result
= vnresult
->result
;
5648 gcc_checking_assert (TREE_CODE (result
) != SSA_NAME
5649 || result
== SSA_VAL (result
));
5650 resultsame
= expressions_equal_p (result
, op
);
5653 /* If the TBAA state isn't compatible for downstream reads
5654 we cannot value-number the VDEFs the same. */
5656 ao_ref_init (&lhs_ref
, lhs
);
5657 alias_set_type set
= ao_ref_alias_set (&lhs_ref
);
5658 alias_set_type base_set
= ao_ref_base_alias_set (&lhs_ref
);
5659 if ((vnresult
->set
!= set
5660 && ! alias_set_subset_of (set
, vnresult
->set
))
5661 || (vnresult
->base_set
!= base_set
5662 && ! alias_set_subset_of (base_set
, vnresult
->base_set
)))
5669 /* Only perform the following when being called from PRE
5670 which embeds tail merging. */
5671 if (default_vn_walk_kind
== VN_WALK
)
5673 assign
= build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, op
);
5674 vn_reference_lookup (assign
, vuse
, VN_NOWALK
, &vnresult
, false);
5677 VN_INFO (vdef
)->visited
= true;
5678 return set_ssa_val_to (vdef
, vnresult
->result_vdef
);
5682 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5684 fprintf (dump_file
, "No store match\n");
5685 fprintf (dump_file
, "Value numbering store ");
5686 print_generic_expr (dump_file
, lhs
);
5687 fprintf (dump_file
, " to ");
5688 print_generic_expr (dump_file
, op
);
5689 fprintf (dump_file
, "\n");
5691 /* Have to set value numbers before insert, since insert is
5692 going to valueize the references in-place. */
5694 changed
|= set_ssa_val_to (vdef
, vdef
);
5696 /* Do not insert structure copies into the tables. */
5697 if (is_gimple_min_invariant (op
)
5698 || is_gimple_reg (op
))
5699 vn_reference_insert (lhs
, op
, vdef
, NULL
);
5701 /* Only perform the following when being called from PRE
5702 which embeds tail merging. */
5703 if (default_vn_walk_kind
== VN_WALK
)
5705 assign
= build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, op
);
5706 vn_reference_insert (assign
, lhs
, vuse
, vdef
);
5711 /* We had a match, so value number the vdef to have the value
5712 number of the vuse it came from. */
5714 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5715 fprintf (dump_file
, "Store matched earlier value, "
5716 "value numbering store vdefs to matching vuses.\n");
5718 changed
|= set_ssa_val_to (vdef
, SSA_VAL (vuse
));
5724 /* Visit and value number PHI, return true if the value number
5725 changed. When BACKEDGES_VARYING_P is true then assume all
5726 backedge values are varying. When INSERTED is not NULL then
5727 this is just a ahead query for a possible iteration, set INSERTED
5728 to true if we'd insert into the hashtable. */
5731 visit_phi (gimple
*phi
, bool *inserted
, bool backedges_varying_p
)
5733 tree result
, sameval
= VN_TOP
, seen_undef
= NULL_TREE
;
5734 tree backedge_val
= NULL_TREE
;
5735 bool seen_non_backedge
= false;
5736 tree sameval_base
= NULL_TREE
;
5737 poly_int64 soff
, doff
;
5738 unsigned n_executable
= 0;
5742 /* TODO: We could check for this in initialization, and replace this
5743 with a gcc_assert. */
5744 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi
)))
5745 return set_ssa_val_to (PHI_RESULT (phi
), PHI_RESULT (phi
));
5747 /* We track whether a PHI was CSEd to to avoid excessive iterations
5748 that would be necessary only because the PHI changed arguments
5751 gimple_set_plf (phi
, GF_PLF_1
, false);
5753 /* See if all non-TOP arguments have the same value. TOP is
5754 equivalent to everything, so we can ignore it. */
5755 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
5756 if (e
->flags
& EDGE_EXECUTABLE
)
5758 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
5760 if (def
== PHI_RESULT (phi
))
5763 if (TREE_CODE (def
) == SSA_NAME
)
5765 if (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
))
5766 def
= SSA_VAL (def
);
5767 if (e
->flags
& EDGE_DFS_BACK
)
5770 if (!(e
->flags
& EDGE_DFS_BACK
))
5771 seen_non_backedge
= true;
5774 /* Ignore undefined defs for sameval but record one. */
5775 else if (TREE_CODE (def
) == SSA_NAME
5776 && ! virtual_operand_p (def
)
5777 && ssa_undefined_value_p (def
, false))
5779 else if (sameval
== VN_TOP
)
5781 else if (!expressions_equal_p (def
, sameval
))
5783 /* We know we're arriving only with invariant addresses here,
5784 try harder comparing them. We can do some caching here
5785 which we cannot do in expressions_equal_p. */
5786 if (TREE_CODE (def
) == ADDR_EXPR
5787 && TREE_CODE (sameval
) == ADDR_EXPR
5788 && sameval_base
!= (void *)-1)
5791 sameval_base
= get_addr_base_and_unit_offset
5792 (TREE_OPERAND (sameval
, 0), &soff
);
5794 sameval_base
= (tree
)(void *)-1;
5795 else if ((get_addr_base_and_unit_offset
5796 (TREE_OPERAND (def
, 0), &doff
) == sameval_base
)
5797 && known_eq (soff
, doff
))
5800 sameval
= NULL_TREE
;
5805 /* If the value we want to use is flowing over the backedge and we
5806 should take it as VARYING but it has a non-VARYING value drop to
5808 If we value-number a virtual operand never value-number to the
5809 value from the backedge as that confuses the alias-walking code.
5810 See gcc.dg/torture/pr87176.c. If the value is the same on a
5811 non-backedge everything is OK though. */
5814 && !seen_non_backedge
5815 && TREE_CODE (backedge_val
) == SSA_NAME
5816 && sameval
== backedge_val
5817 && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val
)
5818 || SSA_VAL (backedge_val
) != backedge_val
))
5819 /* Do not value-number a virtual operand to sth not visited though
5820 given that allows us to escape a region in alias walking. */
5822 && TREE_CODE (sameval
) == SSA_NAME
5823 && !SSA_NAME_IS_DEFAULT_DEF (sameval
)
5824 && SSA_NAME_IS_VIRTUAL_OPERAND (sameval
)
5825 && (SSA_VAL (sameval
, &visited_p
), !visited_p
)))
5826 /* Note this just drops to VARYING without inserting the PHI into
5828 result
= PHI_RESULT (phi
);
5829 /* If none of the edges was executable keep the value-number at VN_TOP,
5830 if only a single edge is exectuable use its value. */
5831 else if (n_executable
<= 1)
5832 result
= seen_undef
? seen_undef
: sameval
;
5833 /* If we saw only undefined values and VN_TOP use one of the
5834 undefined values. */
5835 else if (sameval
== VN_TOP
)
5836 result
= seen_undef
? seen_undef
: sameval
;
5837 /* First see if it is equivalent to a phi node in this block. We prefer
5838 this as it allows IV elimination - see PRs 66502 and 67167. */
5839 else if ((result
= vn_phi_lookup (phi
, backedges_varying_p
)))
5842 && TREE_CODE (result
) == SSA_NAME
5843 && gimple_code (SSA_NAME_DEF_STMT (result
)) == GIMPLE_PHI
)
5845 gimple_set_plf (SSA_NAME_DEF_STMT (result
), GF_PLF_1
, true);
5846 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5848 fprintf (dump_file
, "Marking CSEd to PHI node ");
5849 print_gimple_expr (dump_file
, SSA_NAME_DEF_STMT (result
),
5851 fprintf (dump_file
, "\n");
5855 /* If all values are the same use that, unless we've seen undefined
5856 values as well and the value isn't constant.
5857 CCP/copyprop have the same restriction to not remove uninit warnings. */
5859 && (! seen_undef
|| is_gimple_min_invariant (sameval
)))
5863 result
= PHI_RESULT (phi
);
5864 /* Only insert PHIs that are varying, for constant value numbers
5865 we mess up equivalences otherwise as we are only comparing
5866 the immediate controlling predicates. */
5867 vn_phi_insert (phi
, result
, backedges_varying_p
);
5872 return set_ssa_val_to (PHI_RESULT (phi
), result
);
5875 /* Try to simplify RHS using equivalences and constant folding. */
5878 try_to_simplify (gassign
*stmt
)
5880 enum tree_code code
= gimple_assign_rhs_code (stmt
);
5883 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
5884 in this case, there is no point in doing extra work. */
5885 if (code
== SSA_NAME
)
5888 /* First try constant folding based on our current lattice. */
5889 mprts_hook
= vn_lookup_simplify_result
;
5890 tem
= gimple_fold_stmt_to_constant_1 (stmt
, vn_valueize
, vn_valueize
);
5893 && (TREE_CODE (tem
) == SSA_NAME
5894 || is_gimple_min_invariant (tem
)))
5900 /* Visit and value number STMT, return true if the value number
5904 visit_stmt (gimple
*stmt
, bool backedges_varying_p
= false)
5906 bool changed
= false;
5908 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5910 fprintf (dump_file
, "Value numbering stmt = ");
5911 print_gimple_stmt (dump_file
, stmt
, 0);
5914 if (gimple_code (stmt
) == GIMPLE_PHI
)
5915 changed
= visit_phi (stmt
, NULL
, backedges_varying_p
);
5916 else if (gimple_has_volatile_ops (stmt
))
5917 changed
= defs_to_varying (stmt
);
5918 else if (gassign
*ass
= dyn_cast
<gassign
*> (stmt
))
5920 enum tree_code code
= gimple_assign_rhs_code (ass
);
5921 tree lhs
= gimple_assign_lhs (ass
);
5922 tree rhs1
= gimple_assign_rhs1 (ass
);
5925 /* Shortcut for copies. Simplifying copies is pointless,
5926 since we copy the expression and value they represent. */
5927 if (code
== SSA_NAME
5928 && TREE_CODE (lhs
) == SSA_NAME
)
5930 changed
= visit_copy (lhs
, rhs1
);
5933 simplified
= try_to_simplify (ass
);
5936 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5938 fprintf (dump_file
, "RHS ");
5939 print_gimple_expr (dump_file
, ass
, 0);
5940 fprintf (dump_file
, " simplified to ");
5941 print_generic_expr (dump_file
, simplified
);
5942 fprintf (dump_file
, "\n");
5945 /* Setting value numbers to constants will occasionally
5946 screw up phi congruence because constants are not
5947 uniquely associated with a single ssa name that can be
5950 && is_gimple_min_invariant (simplified
)
5951 && TREE_CODE (lhs
) == SSA_NAME
)
5953 changed
= set_ssa_val_to (lhs
, simplified
);
5957 && TREE_CODE (simplified
) == SSA_NAME
5958 && TREE_CODE (lhs
) == SSA_NAME
)
5960 changed
= visit_copy (lhs
, simplified
);
5964 if ((TREE_CODE (lhs
) == SSA_NAME
5965 /* We can substitute SSA_NAMEs that are live over
5966 abnormal edges with their constant value. */
5967 && !(gimple_assign_copy_p (ass
)
5968 && is_gimple_min_invariant (rhs1
))
5970 && is_gimple_min_invariant (simplified
))
5971 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
5972 /* Stores or copies from SSA_NAMEs that are live over
5973 abnormal edges are a problem. */
5974 || (code
== SSA_NAME
5975 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
)))
5976 changed
= defs_to_varying (ass
);
5977 else if (REFERENCE_CLASS_P (lhs
)
5979 changed
= visit_reference_op_store (lhs
, rhs1
, ass
);
5980 else if (TREE_CODE (lhs
) == SSA_NAME
)
5982 if ((gimple_assign_copy_p (ass
)
5983 && is_gimple_min_invariant (rhs1
))
5985 && is_gimple_min_invariant (simplified
)))
5988 changed
= set_ssa_val_to (lhs
, simplified
);
5990 changed
= set_ssa_val_to (lhs
, rhs1
);
5994 /* Visit the original statement. */
5995 switch (vn_get_stmt_kind (ass
))
5998 changed
= visit_nary_op (lhs
, ass
);
6001 changed
= visit_reference_op_load (lhs
, rhs1
, ass
);
6004 changed
= defs_to_varying (ass
);
6010 changed
= defs_to_varying (ass
);
6012 else if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
6014 tree lhs
= gimple_call_lhs (call_stmt
);
6015 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6017 /* Try constant folding based on our current lattice. */
6018 tree simplified
= gimple_fold_stmt_to_constant_1 (call_stmt
,
6022 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6024 fprintf (dump_file
, "call ");
6025 print_gimple_expr (dump_file
, call_stmt
, 0);
6026 fprintf (dump_file
, " simplified to ");
6027 print_generic_expr (dump_file
, simplified
);
6028 fprintf (dump_file
, "\n");
6031 /* Setting value numbers to constants will occasionally
6032 screw up phi congruence because constants are not
6033 uniquely associated with a single ssa name that can be
6036 && is_gimple_min_invariant (simplified
))
6038 changed
= set_ssa_val_to (lhs
, simplified
);
6039 if (gimple_vdef (call_stmt
))
6040 changed
|= set_ssa_val_to (gimple_vdef (call_stmt
),
6041 SSA_VAL (gimple_vuse (call_stmt
)));
6045 && TREE_CODE (simplified
) == SSA_NAME
)
6047 changed
= visit_copy (lhs
, simplified
);
6048 if (gimple_vdef (call_stmt
))
6049 changed
|= set_ssa_val_to (gimple_vdef (call_stmt
),
6050 SSA_VAL (gimple_vuse (call_stmt
)));
6053 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
6055 changed
= defs_to_varying (call_stmt
);
6060 /* Pick up flags from a devirtualization target. */
6061 tree fn
= gimple_call_fn (stmt
);
6062 int extra_fnflags
= 0;
6063 if (fn
&& TREE_CODE (fn
) == SSA_NAME
)
6066 if (TREE_CODE (fn
) == ADDR_EXPR
6067 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
)
6068 extra_fnflags
= flags_from_decl_or_type (TREE_OPERAND (fn
, 0));
6070 if ((/* Calls to the same function with the same vuse
6071 and the same operands do not necessarily return the same
6072 value, unless they're pure or const. */
6073 ((gimple_call_flags (call_stmt
) | extra_fnflags
)
6074 & (ECF_PURE
| ECF_CONST
))
6075 /* If calls have a vdef, subsequent calls won't have
6076 the same incoming vuse. So, if 2 calls with vdef have the
6077 same vuse, we know they're not subsequent.
6078 We can value number 2 calls to the same function with the
6079 same vuse and the same operands which are not subsequent
6080 the same, because there is no code in the program that can
6081 compare the 2 values... */
6082 || (gimple_vdef (call_stmt
)
6083 /* ... unless the call returns a pointer which does
6084 not alias with anything else. In which case the
6085 information that the values are distinct are encoded
6087 && !(gimple_call_return_flags (call_stmt
) & ERF_NOALIAS
)
6088 /* Only perform the following when being called from PRE
6089 which embeds tail merging. */
6090 && default_vn_walk_kind
== VN_WALK
))
6091 /* Do not process .DEFERRED_INIT since that confuses uninit
6093 && !gimple_call_internal_p (call_stmt
, IFN_DEFERRED_INIT
))
6094 changed
= visit_reference_op_call (lhs
, call_stmt
);
6096 changed
= defs_to_varying (call_stmt
);
6099 changed
= defs_to_varying (stmt
);
6105 /* Allocate a value number table. */
6108 allocate_vn_table (vn_tables_t table
, unsigned size
)
6110 table
->phis
= new vn_phi_table_type (size
);
6111 table
->nary
= new vn_nary_op_table_type (size
);
6112 table
->references
= new vn_reference_table_type (size
);
6115 /* Free a value number table. */
6118 free_vn_table (vn_tables_t table
)
6120 /* Walk over elements and release vectors. */
6121 vn_reference_iterator_type hir
;
6123 FOR_EACH_HASH_TABLE_ELEMENT (*table
->references
, vr
, vn_reference_t
, hir
)
6124 vr
->operands
.release ();
6129 delete table
->references
;
6130 table
->references
= NULL
;
6133 /* Set *ID according to RESULT. */
6136 set_value_id_for_result (tree result
, unsigned int *id
)
6138 if (result
&& TREE_CODE (result
) == SSA_NAME
)
6139 *id
= VN_INFO (result
)->value_id
;
6140 else if (result
&& is_gimple_min_invariant (result
))
6141 *id
= get_or_alloc_constant_value_id (result
);
6143 *id
= get_next_value_id ();
6146 /* Set the value ids in the valid hash tables. */
6149 set_hashtable_value_ids (void)
6151 vn_nary_op_iterator_type hin
;
6152 vn_phi_iterator_type hip
;
6153 vn_reference_iterator_type hir
;
6158 /* Now set the value ids of the things we had put in the hash
6161 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->nary
, vno
, vn_nary_op_t
, hin
)
6162 if (! vno
->predicated_values
)
6163 set_value_id_for_result (vno
->u
.result
, &vno
->value_id
);
6165 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->phis
, vp
, vn_phi_t
, hip
)
6166 set_value_id_for_result (vp
->result
, &vp
->value_id
);
6168 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->references
, vr
, vn_reference_t
,
6170 set_value_id_for_result (vr
->result
, &vr
->value_id
);
6173 /* Return the maximum value id we have ever seen. */
6176 get_max_value_id (void)
6178 return next_value_id
;
6181 /* Return the maximum constant value id we have ever seen. */
6184 get_max_constant_value_id (void)
6186 return -next_constant_value_id
;
6189 /* Return the next unique value id. */
6192 get_next_value_id (void)
6194 gcc_checking_assert ((int)next_value_id
> 0);
6195 return next_value_id
++;
6198 /* Return the next unique value id for constants. */
6201 get_next_constant_value_id (void)
6203 gcc_checking_assert (next_constant_value_id
< 0);
6204 return next_constant_value_id
--;
6208 /* Compare two expressions E1 and E2 and return true if they are equal.
6209 If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6210 otherwise VN_TOP only matches VN_TOP. */
6213 expressions_equal_p (tree e1
, tree e2
, bool match_vn_top_optimistically
)
6215 /* The obvious case. */
6219 /* If either one is VN_TOP consider them equal. */
6220 if (match_vn_top_optimistically
6221 && (e1
== VN_TOP
|| e2
== VN_TOP
))
6224 /* SSA_NAME compare pointer equal. */
6225 if (TREE_CODE (e1
) == SSA_NAME
|| TREE_CODE (e2
) == SSA_NAME
)
6228 /* Now perform the actual comparison. */
6229 if (TREE_CODE (e1
) == TREE_CODE (e2
)
6230 && operand_equal_p (e1
, e2
, OEP_PURE_SAME
))
6237 /* Return true if the nary operation NARY may trap. This is a copy
6238 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6241 vn_nary_may_trap (vn_nary_op_t nary
)
6244 tree rhs2
= NULL_TREE
;
6245 bool honor_nans
= false;
6246 bool honor_snans
= false;
6247 bool fp_operation
= false;
6248 bool honor_trapv
= false;
6252 if (TREE_CODE_CLASS (nary
->opcode
) == tcc_comparison
6253 || TREE_CODE_CLASS (nary
->opcode
) == tcc_unary
6254 || TREE_CODE_CLASS (nary
->opcode
) == tcc_binary
)
6257 fp_operation
= FLOAT_TYPE_P (type
);
6260 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
6261 honor_snans
= flag_signaling_nans
!= 0;
6263 else if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_TRAPS (type
))
6266 if (nary
->length
>= 2)
6268 ret
= operation_could_trap_helper_p (nary
->opcode
, fp_operation
,
6269 honor_trapv
, honor_nans
, honor_snans
,
6274 for (i
= 0; i
< nary
->length
; ++i
)
6275 if (tree_could_trap_p (nary
->op
[i
]))
6281 /* Return true if the reference operation REF may trap. */
6284 vn_reference_may_trap (vn_reference_t ref
)
6286 switch (ref
->operands
[0].opcode
)
6290 /* We do not handle calls. */
6293 /* And toplevel address computations never trap. */
6298 vn_reference_op_t op
;
6300 FOR_EACH_VEC_ELT (ref
->operands
, i
, op
)
6304 case WITH_SIZE_EXPR
:
6305 case TARGET_MEM_REF
:
6306 /* Always variable. */
6309 if (op
->op1
&& TREE_CODE (op
->op1
) == SSA_NAME
)
6312 case ARRAY_RANGE_REF
:
6313 if (TREE_CODE (op
->op0
) == SSA_NAME
)
6318 if (TREE_CODE (op
->op0
) != INTEGER_CST
)
6321 /* !in_array_bounds */
6322 tree domain_type
= TYPE_DOMAIN (ref
->operands
[i
+1].type
);
6327 tree max
= TYPE_MAX_VALUE (domain_type
);
6330 || TREE_CODE (min
) != INTEGER_CST
6331 || TREE_CODE (max
) != INTEGER_CST
)
6334 if (tree_int_cst_lt (op
->op0
, min
)
6335 || tree_int_cst_lt (max
, op
->op0
))
6341 /* Nothing interesting in itself, the base is separate. */
6343 /* The following are the address bases. */
6348 return tree_could_trap_p (TREE_OPERAND (op
->op0
, 0));
6356 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction
,
6357 bitmap inserted_exprs_
)
6358 : dom_walker (direction
), do_pre (inserted_exprs_
!= NULL
),
6359 el_todo (0), eliminations (0), insertions (0),
6360 inserted_exprs (inserted_exprs_
)
6362 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
6363 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
6366 eliminate_dom_walker::~eliminate_dom_walker ()
6368 BITMAP_FREE (need_eh_cleanup
);
6369 BITMAP_FREE (need_ab_cleanup
);
6372 /* Return a leader for OP that is available at the current point of the
6373 eliminate domwalk. */
6376 eliminate_dom_walker::eliminate_avail (basic_block
, tree op
)
6378 tree valnum
= VN_INFO (op
)->valnum
;
6379 if (TREE_CODE (valnum
) == SSA_NAME
)
6381 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
6383 if (avail
.length () > SSA_NAME_VERSION (valnum
))
6384 return avail
[SSA_NAME_VERSION (valnum
)];
6386 else if (is_gimple_min_invariant (valnum
))
6391 /* At the current point of the eliminate domwalk make OP available. */
6394 eliminate_dom_walker::eliminate_push_avail (basic_block
, tree op
)
6396 tree valnum
= VN_INFO (op
)->valnum
;
6397 if (TREE_CODE (valnum
) == SSA_NAME
)
6399 if (avail
.length () <= SSA_NAME_VERSION (valnum
))
6400 avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1, true);
6402 if (avail
[SSA_NAME_VERSION (valnum
)])
6403 pushop
= avail
[SSA_NAME_VERSION (valnum
)];
6404 avail_stack
.safe_push (pushop
);
6405 avail
[SSA_NAME_VERSION (valnum
)] = op
;
6409 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
6410 the leader for the expression if insertion was successful. */
6413 eliminate_dom_walker::eliminate_insert (basic_block bb
,
6414 gimple_stmt_iterator
*gsi
, tree val
)
6416 /* We can insert a sequence with a single assignment only. */
6417 gimple_seq stmts
= VN_INFO (val
)->expr
;
6418 if (!gimple_seq_singleton_p (stmts
))
6420 gassign
*stmt
= dyn_cast
<gassign
*> (gimple_seq_first_stmt (stmts
));
6422 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
6423 && gimple_assign_rhs_code (stmt
) != VIEW_CONVERT_EXPR
6424 && gimple_assign_rhs_code (stmt
) != NEGATE_EXPR
6425 && gimple_assign_rhs_code (stmt
) != BIT_FIELD_REF
6426 && (gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6427 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)))
6430 tree op
= gimple_assign_rhs1 (stmt
);
6431 if (gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
6432 || gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
6433 op
= TREE_OPERAND (op
, 0);
6434 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (bb
, op
) : op
;
6440 if (gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
6441 res
= gimple_build (&stmts
, BIT_FIELD_REF
,
6442 TREE_TYPE (val
), leader
,
6443 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1),
6444 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2));
6445 else if (gimple_assign_rhs_code (stmt
) == BIT_AND_EXPR
)
6446 res
= gimple_build (&stmts
, BIT_AND_EXPR
,
6447 TREE_TYPE (val
), leader
, gimple_assign_rhs2 (stmt
));
6449 res
= gimple_build (&stmts
, gimple_assign_rhs_code (stmt
),
6450 TREE_TYPE (val
), leader
);
6451 if (TREE_CODE (res
) != SSA_NAME
6452 || SSA_NAME_IS_DEFAULT_DEF (res
)
6453 || gimple_bb (SSA_NAME_DEF_STMT (res
)))
6455 gimple_seq_discard (stmts
);
6457 /* During propagation we have to treat SSA info conservatively
6458 and thus we can end up simplifying the inserted expression
6459 at elimination time to sth not defined in stmts. */
6460 /* But then this is a redundancy we failed to detect. Which means
6461 res now has two values. That doesn't play well with how
6462 we track availability here, so give up. */
6463 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6465 if (TREE_CODE (res
) == SSA_NAME
)
6466 res
= eliminate_avail (bb
, res
);
6469 fprintf (dump_file
, "Failed to insert expression for value ");
6470 print_generic_expr (dump_file
, val
);
6471 fprintf (dump_file
, " which is really fully redundant to ");
6472 print_generic_expr (dump_file
, res
);
6473 fprintf (dump_file
, "\n");
6481 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
6482 vn_ssa_aux_t vn_info
= VN_INFO (res
);
6483 vn_info
->valnum
= val
;
6484 vn_info
->visited
= true;
6488 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6490 fprintf (dump_file
, "Inserted ");
6491 print_gimple_stmt (dump_file
, SSA_NAME_DEF_STMT (res
), 0);
6498 eliminate_dom_walker::eliminate_stmt (basic_block b
, gimple_stmt_iterator
*gsi
)
6500 tree sprime
= NULL_TREE
;
6501 gimple
*stmt
= gsi_stmt (*gsi
);
6502 tree lhs
= gimple_get_lhs (stmt
);
6503 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6504 && !gimple_has_volatile_ops (stmt
)
6505 /* See PR43491. Do not replace a global register variable when
6506 it is a the RHS of an assignment. Do replace local register
6507 variables since gcc does not guarantee a local variable will
6508 be allocated in register.
6509 ??? The fix isn't effective here. This should instead
6510 be ensured by not value-numbering them the same but treating
6511 them like volatiles? */
6512 && !(gimple_assign_single_p (stmt
)
6513 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
6514 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
6515 && is_global_var (gimple_assign_rhs1 (stmt
)))))
6517 sprime
= eliminate_avail (b
, lhs
);
6520 /* If there is no existing usable leader but SCCVN thinks
6521 it has an expression it wants to use as replacement,
6523 tree val
= VN_INFO (lhs
)->valnum
;
6524 vn_ssa_aux_t vn_info
;
6526 && TREE_CODE (val
) == SSA_NAME
6527 && (vn_info
= VN_INFO (val
), true)
6528 && vn_info
->needs_insertion
6529 && vn_info
->expr
!= NULL
6530 && (sprime
= eliminate_insert (b
, gsi
, val
)) != NULL_TREE
)
6531 eliminate_push_avail (b
, sprime
);
6534 /* If this now constitutes a copy duplicate points-to
6535 and range info appropriately. This is especially
6536 important for inserted code. See tree-ssa-copy.cc
6537 for similar code. */
6539 && TREE_CODE (sprime
) == SSA_NAME
)
6541 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
6542 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
6543 && SSA_NAME_PTR_INFO (lhs
)
6544 && ! SSA_NAME_PTR_INFO (sprime
))
6546 duplicate_ssa_name_ptr_info (sprime
,
6547 SSA_NAME_PTR_INFO (lhs
));
6549 reset_flow_sensitive_info (sprime
);
6551 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6552 && SSA_NAME_RANGE_INFO (lhs
)
6553 && ! SSA_NAME_RANGE_INFO (sprime
)
6555 duplicate_ssa_name_range_info (sprime
, lhs
);
6558 /* Inhibit the use of an inserted PHI on a loop header when
6559 the address of the memory reference is a simple induction
6560 variable. In other cases the vectorizer won't do anything
6561 anyway (either it's loop invariant or a complicated
6564 && TREE_CODE (sprime
) == SSA_NAME
6566 && (flag_tree_loop_vectorize
|| flag_tree_parallelize_loops
> 1)
6567 && loop_outer (b
->loop_father
)
6568 && has_zero_uses (sprime
)
6569 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
6570 && gimple_assign_load_p (stmt
))
6572 gimple
*def_stmt
= SSA_NAME_DEF_STMT (sprime
);
6573 basic_block def_bb
= gimple_bb (def_stmt
);
6574 if (gimple_code (def_stmt
) == GIMPLE_PHI
6575 && def_bb
->loop_father
->header
== def_bb
)
6577 loop_p loop
= def_bb
->loop_father
;
6581 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
6584 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
6586 && flow_bb_inside_loop_p (loop
, def_bb
)
6587 && simple_iv (loop
, loop
, op
, &iv
, true))
6595 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6597 fprintf (dump_file
, "Not replacing ");
6598 print_gimple_expr (dump_file
, stmt
, 0);
6599 fprintf (dump_file
, " with ");
6600 print_generic_expr (dump_file
, sprime
);
6601 fprintf (dump_file
, " which would add a loop"
6602 " carried dependence to loop %d\n",
6605 /* Don't keep sprime available. */
6613 /* If we can propagate the value computed for LHS into
6614 all uses don't bother doing anything with this stmt. */
6615 if (may_propagate_copy (lhs
, sprime
))
6617 /* Mark it for removal. */
6618 to_remove
.safe_push (stmt
);
6620 /* ??? Don't count copy/constant propagations. */
6621 if (gimple_assign_single_p (stmt
)
6622 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
6623 || gimple_assign_rhs1 (stmt
) == sprime
))
6626 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6628 fprintf (dump_file
, "Replaced ");
6629 print_gimple_expr (dump_file
, stmt
, 0);
6630 fprintf (dump_file
, " with ");
6631 print_generic_expr (dump_file
, sprime
);
6632 fprintf (dump_file
, " in all uses of ");
6633 print_gimple_stmt (dump_file
, stmt
, 0);
6640 /* If this is an assignment from our leader (which
6641 happens in the case the value-number is a constant)
6642 then there is nothing to do. Likewise if we run into
6643 inserted code that needed a conversion because of
6644 our type-agnostic value-numbering of loads. */
6645 if ((gimple_assign_single_p (stmt
)
6646 || (is_gimple_assign (stmt
)
6647 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
6648 || gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
)))
6649 && sprime
== gimple_assign_rhs1 (stmt
))
6652 /* Else replace its RHS. */
6653 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6655 fprintf (dump_file
, "Replaced ");
6656 print_gimple_expr (dump_file
, stmt
, 0);
6657 fprintf (dump_file
, " with ");
6658 print_generic_expr (dump_file
, sprime
);
6659 fprintf (dump_file
, " in ");
6660 print_gimple_stmt (dump_file
, stmt
, 0);
6664 bool can_make_abnormal_goto
= (is_gimple_call (stmt
)
6665 && stmt_can_make_abnormal_goto (stmt
));
6666 gimple
*orig_stmt
= stmt
;
6667 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
6668 TREE_TYPE (sprime
)))
6670 /* We preserve conversions to but not from function or method
6671 types. This asymmetry makes it necessary to re-instantiate
6672 conversions here. */
6673 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
6674 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs
))))
6675 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
6679 tree vdef
= gimple_vdef (stmt
);
6680 tree vuse
= gimple_vuse (stmt
);
6681 propagate_tree_value_into_stmt (gsi
, sprime
);
6682 stmt
= gsi_stmt (*gsi
);
6684 /* In case the VDEF on the original stmt was released, value-number
6685 it to the VUSE. This is to make vuse_ssa_val able to skip
6686 released virtual operands. */
6687 if (vdef
!= gimple_vdef (stmt
))
6689 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef
));
6690 VN_INFO (vdef
)->valnum
= vuse
;
6693 /* If we removed EH side-effects from the statement, clean
6694 its EH information. */
6695 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
6697 bitmap_set_bit (need_eh_cleanup
,
6698 gimple_bb (stmt
)->index
);
6699 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6700 fprintf (dump_file
, " Removed EH side-effects.\n");
6703 /* Likewise for AB side-effects. */
6704 if (can_make_abnormal_goto
6705 && !stmt_can_make_abnormal_goto (stmt
))
6707 bitmap_set_bit (need_ab_cleanup
,
6708 gimple_bb (stmt
)->index
);
6709 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6710 fprintf (dump_file
, " Removed AB side-effects.\n");
6717 /* If the statement is a scalar store, see if the expression
6718 has the same value number as its rhs. If so, the store is
6720 if (gimple_assign_single_p (stmt
)
6721 && !gimple_has_volatile_ops (stmt
)
6722 && !is_gimple_reg (gimple_assign_lhs (stmt
))
6723 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
6724 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
6726 tree rhs
= gimple_assign_rhs1 (stmt
);
6727 vn_reference_t vnresult
;
6728 /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
6729 typed load of a byte known to be 0x11 as 1 so a store of
6730 a boolean 1 is detected as redundant. Because of this we
6731 have to make sure to lookup with a ref where its size
6732 matches the precision. */
6733 tree lookup_lhs
= lhs
;
6734 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6735 && (TREE_CODE (lhs
) != COMPONENT_REF
6736 || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs
, 1)))
6737 && !type_has_mode_precision_p (TREE_TYPE (lhs
)))
6739 if (TREE_CODE (lhs
) == COMPONENT_REF
6740 || TREE_CODE (lhs
) == MEM_REF
)
6742 tree ltype
= build_nonstandard_integer_type
6743 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs
))),
6744 TYPE_UNSIGNED (TREE_TYPE (lhs
)));
6745 if (TREE_CODE (lhs
) == COMPONENT_REF
)
6747 tree foff
= component_ref_field_offset (lhs
);
6748 tree f
= TREE_OPERAND (lhs
, 1);
6749 if (!poly_int_tree_p (foff
))
6750 lookup_lhs
= NULL_TREE
;
6752 lookup_lhs
= build3 (BIT_FIELD_REF
, ltype
,
6753 TREE_OPERAND (lhs
, 0),
6754 TYPE_SIZE (TREE_TYPE (lhs
)),
6756 (foff
, DECL_FIELD_BIT_OFFSET (f
)));
6759 lookup_lhs
= build2 (MEM_REF
, ltype
,
6760 TREE_OPERAND (lhs
, 0),
6761 TREE_OPERAND (lhs
, 1));
6764 lookup_lhs
= NULL_TREE
;
6766 tree val
= NULL_TREE
;
6768 val
= vn_reference_lookup (lookup_lhs
, gimple_vuse (stmt
),
6769 VN_WALKREWRITE
, &vnresult
, false,
6770 NULL
, NULL_TREE
, true);
6771 if (TREE_CODE (rhs
) == SSA_NAME
)
6772 rhs
= VN_INFO (rhs
)->valnum
;
6774 && (operand_equal_p (val
, rhs
, 0)
6775 /* Due to the bitfield lookups above we can get bit
6776 interpretations of the same RHS as values here. Those
6777 are redundant as well. */
6778 || (TREE_CODE (val
) == SSA_NAME
6779 && gimple_assign_single_p (SSA_NAME_DEF_STMT (val
))
6780 && (val
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val
)))
6781 && TREE_CODE (val
) == VIEW_CONVERT_EXPR
6782 && TREE_OPERAND (val
, 0) == rhs
)))
6784 /* We can only remove the later store if the former aliases
6785 at least all accesses the later one does or if the store
6786 was to readonly memory storing the same value. */
6788 ao_ref_init (&lhs_ref
, lhs
);
6789 alias_set_type set
= ao_ref_alias_set (&lhs_ref
);
6790 alias_set_type base_set
= ao_ref_base_alias_set (&lhs_ref
);
6792 || ((vnresult
->set
== set
6793 || alias_set_subset_of (set
, vnresult
->set
))
6794 && (vnresult
->base_set
== base_set
6795 || alias_set_subset_of (base_set
, vnresult
->base_set
))))
6797 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6799 fprintf (dump_file
, "Deleted redundant store ");
6800 print_gimple_stmt (dump_file
, stmt
, 0);
6803 /* Queue stmt for removal. */
6804 to_remove
.safe_push (stmt
);
6810 /* If this is a control statement value numbering left edges
6811 unexecuted on force the condition in a way consistent with
6813 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
6815 if ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
)
6816 ^ (EDGE_SUCC (b
, 1)->flags
& EDGE_EXECUTABLE
))
6818 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6820 fprintf (dump_file
, "Removing unexecutable edge from ");
6821 print_gimple_stmt (dump_file
, stmt
, 0);
6823 if (((EDGE_SUCC (b
, 0)->flags
& EDGE_TRUE_VALUE
) != 0)
6824 == ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
) != 0))
6825 gimple_cond_make_true (cond
);
6827 gimple_cond_make_false (cond
);
6829 el_todo
|= TODO_cleanup_cfg
;
6834 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
6835 bool was_noreturn
= (is_gimple_call (stmt
)
6836 && gimple_call_noreturn_p (stmt
));
6837 tree vdef
= gimple_vdef (stmt
);
6838 tree vuse
= gimple_vuse (stmt
);
6840 /* If we didn't replace the whole stmt (or propagate the result
6841 into all uses), replace all uses on this stmt with their
6843 bool modified
= false;
6844 use_operand_p use_p
;
6846 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
6848 tree use
= USE_FROM_PTR (use_p
);
6849 /* ??? The call code above leaves stmt operands un-updated. */
6850 if (TREE_CODE (use
) != SSA_NAME
)
6853 if (SSA_NAME_IS_DEFAULT_DEF (use
))
6854 /* ??? For default defs BB shouldn't matter, but we have to
6855 solve the inconsistency between rpo eliminate and
6856 dom eliminate avail valueization first. */
6857 sprime
= eliminate_avail (b
, use
);
6859 /* Look for sth available at the definition block of the argument.
6860 This avoids inconsistencies between availability there which
6861 decides if the stmt can be removed and availability at the
6862 use site. The SSA property ensures that things available
6863 at the definition are also available at uses. */
6864 sprime
= eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use
)), use
);
6865 if (sprime
&& sprime
!= use
6866 && may_propagate_copy (use
, sprime
, true)
6867 /* We substitute into debug stmts to avoid excessive
6868 debug temporaries created by removed stmts, but we need
6869 to avoid doing so for inserted sprimes as we never want
6870 to create debug temporaries for them. */
6872 || TREE_CODE (sprime
) != SSA_NAME
6873 || !is_gimple_debug (stmt
)
6874 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
6876 propagate_value (use_p
, sprime
);
6881 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
6882 into which is a requirement for the IPA devirt machinery. */
6883 gimple
*old_stmt
= stmt
;
6886 /* If a formerly non-invariant ADDR_EXPR is turned into an
6887 invariant one it was on a separate stmt. */
6888 if (gimple_assign_single_p (stmt
)
6889 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
6890 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
6891 gimple_stmt_iterator prev
= *gsi
;
6893 if (fold_stmt (gsi
, follow_all_ssa_edges
))
6895 /* fold_stmt may have created new stmts inbetween
6896 the previous stmt and the folded stmt. Mark
6897 all defs created there as varying to not confuse
6898 the SCCVN machinery as we're using that even during
6900 if (gsi_end_p (prev
))
6901 prev
= gsi_start_bb (b
);
6904 if (gsi_stmt (prev
) != gsi_stmt (*gsi
))
6909 FOR_EACH_SSA_TREE_OPERAND (def
, gsi_stmt (prev
),
6910 dit
, SSA_OP_ALL_DEFS
)
6911 /* As existing DEFs may move between stmts
6912 only process new ones. */
6913 if (! has_VN_INFO (def
))
6915 vn_ssa_aux_t vn_info
= VN_INFO (def
);
6916 vn_info
->valnum
= def
;
6917 vn_info
->visited
= true;
6919 if (gsi_stmt (prev
) == gsi_stmt (*gsi
))
6925 stmt
= gsi_stmt (*gsi
);
6926 /* In case we folded the stmt away schedule the NOP for removal. */
6927 if (gimple_nop_p (stmt
))
6928 to_remove
.safe_push (stmt
);
6931 /* Visit indirect calls and turn them into direct calls if
6932 possible using the devirtualization machinery. Do this before
6933 checking for required EH/abnormal/noreturn cleanup as devird
6934 may expose more of those. */
6935 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
6937 tree fn
= gimple_call_fn (call_stmt
);
6939 && flag_devirtualize
6940 && virtual_method_call_p (fn
))
6942 tree otr_type
= obj_type_ref_class (fn
);
6943 unsigned HOST_WIDE_INT otr_tok
6944 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn
));
6946 ipa_polymorphic_call_context
context (current_function_decl
,
6947 fn
, stmt
, &instance
);
6948 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
),
6949 otr_type
, stmt
, NULL
);
6951 vec
<cgraph_node
*> targets
6952 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
6953 otr_tok
, context
, &final
);
6955 dump_possible_polymorphic_call_targets (dump_file
,
6956 obj_type_ref_class (fn
),
6958 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
6961 if (targets
.length () == 1)
6962 fn
= targets
[0]->decl
;
6964 fn
= builtin_decl_unreachable ();
6965 if (dump_enabled_p ())
6967 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
6968 "converting indirect call to "
6970 lang_hooks
.decl_printable_name (fn
, 2));
6972 gimple_call_set_fndecl (call_stmt
, fn
);
6973 /* If changing the call to __builtin_unreachable
6974 or similar noreturn function, adjust gimple_call_fntype
6976 if (gimple_call_noreturn_p (call_stmt
)
6977 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn
)))
6978 && TYPE_ARG_TYPES (TREE_TYPE (fn
))
6979 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn
)))
6981 gimple_call_set_fntype (call_stmt
, TREE_TYPE (fn
));
6982 maybe_remove_unused_call_args (cfun
, call_stmt
);
6990 /* When changing a call into a noreturn call, cfg cleanup
6991 is needed to fix up the noreturn call. */
6993 && is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
))
6994 to_fixup
.safe_push (stmt
);
6995 /* When changing a condition or switch into one we know what
6996 edge will be executed, schedule a cfg cleanup. */
6997 if ((gimple_code (stmt
) == GIMPLE_COND
6998 && (gimple_cond_true_p (as_a
<gcond
*> (stmt
))
6999 || gimple_cond_false_p (as_a
<gcond
*> (stmt
))))
7000 || (gimple_code (stmt
) == GIMPLE_SWITCH
7001 && TREE_CODE (gimple_switch_index
7002 (as_a
<gswitch
*> (stmt
))) == INTEGER_CST
))
7003 el_todo
|= TODO_cleanup_cfg
;
7004 /* If we removed EH side-effects from the statement, clean
7005 its EH information. */
7006 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
7008 bitmap_set_bit (need_eh_cleanup
,
7009 gimple_bb (stmt
)->index
);
7010 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7011 fprintf (dump_file
, " Removed EH side-effects.\n");
7013 /* Likewise for AB side-effects. */
7014 if (can_make_abnormal_goto
7015 && !stmt_can_make_abnormal_goto (stmt
))
7017 bitmap_set_bit (need_ab_cleanup
,
7018 gimple_bb (stmt
)->index
);
7019 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7020 fprintf (dump_file
, " Removed AB side-effects.\n");
7023 /* In case the VDEF on the original stmt was released, value-number
7024 it to the VUSE. This is to make vuse_ssa_val able to skip
7025 released virtual operands. */
7026 if (vdef
&& SSA_NAME_IN_FREE_LIST (vdef
))
7027 VN_INFO (vdef
)->valnum
= vuse
;
7030 /* Make new values available - for fully redundant LHS we
7031 continue with the next stmt above and skip this. */
7033 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
7034 eliminate_push_avail (b
, DEF_FROM_PTR (defp
));
7037 /* Perform elimination for the basic-block B during the domwalk. */
7040 eliminate_dom_walker::before_dom_children (basic_block b
)
7043 avail_stack
.safe_push (NULL_TREE
);
7045 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7046 if (!(b
->flags
& BB_EXECUTABLE
))
7051 for (gphi_iterator gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
7053 gphi
*phi
= gsi
.phi ();
7054 tree res
= PHI_RESULT (phi
);
7056 if (virtual_operand_p (res
))
7062 tree sprime
= eliminate_avail (b
, res
);
7066 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7068 fprintf (dump_file
, "Replaced redundant PHI node defining ");
7069 print_generic_expr (dump_file
, res
);
7070 fprintf (dump_file
, " with ");
7071 print_generic_expr (dump_file
, sprime
);
7072 fprintf (dump_file
, "\n");
7075 /* If we inserted this PHI node ourself, it's not an elimination. */
7076 if (! inserted_exprs
7077 || ! bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
7080 /* If we will propagate into all uses don't bother to do
7082 if (may_propagate_copy (res
, sprime
))
7084 /* Mark the PHI for removal. */
7085 to_remove
.safe_push (phi
);
7090 remove_phi_node (&gsi
, false);
7092 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
7093 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
7094 gimple
*stmt
= gimple_build_assign (res
, sprime
);
7095 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
7096 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
7100 eliminate_push_avail (b
, res
);
7104 for (gimple_stmt_iterator gsi
= gsi_start_bb (b
);
7107 eliminate_stmt (b
, &gsi
);
7109 /* Replace destination PHI arguments. */
7112 FOR_EACH_EDGE (e
, ei
, b
->succs
)
7113 if (e
->flags
& EDGE_EXECUTABLE
)
7114 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
7118 gphi
*phi
= gsi
.phi ();
7119 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
7120 tree arg
= USE_FROM_PTR (use_p
);
7121 if (TREE_CODE (arg
) != SSA_NAME
7122 || virtual_operand_p (arg
))
7124 tree sprime
= eliminate_avail (b
, arg
);
7125 if (sprime
&& may_propagate_copy (arg
, sprime
))
7126 propagate_value (use_p
, sprime
);
7129 vn_context_bb
= NULL
;
7134 /* Make no longer available leaders no longer available. */
7137 eliminate_dom_walker::after_dom_children (basic_block
)
7140 while ((entry
= avail_stack
.pop ()) != NULL_TREE
)
7142 tree valnum
= VN_INFO (entry
)->valnum
;
7143 tree old
= avail
[SSA_NAME_VERSION (valnum
)];
7145 avail
[SSA_NAME_VERSION (valnum
)] = NULL_TREE
;
7147 avail
[SSA_NAME_VERSION (valnum
)] = entry
;
7151 /* Remove queued stmts and perform delayed cleanups. */
7154 eliminate_dom_walker::eliminate_cleanup (bool region_p
)
7156 statistics_counter_event (cfun
, "Eliminated", eliminations
);
7157 statistics_counter_event (cfun
, "Insertions", insertions
);
7159 /* We cannot remove stmts during BB walk, especially not release SSA
7160 names there as this confuses the VN machinery. The stmts ending
7161 up in to_remove are either stores or simple copies.
7162 Remove stmts in reverse order to make debug stmt creation possible. */
7163 while (!to_remove
.is_empty ())
7165 bool do_release_defs
= true;
7166 gimple
*stmt
= to_remove
.pop ();
7168 /* When we are value-numbering a region we do not require exit PHIs to
7169 be present so we have to make sure to deal with uses outside of the
7170 region of stmts that we thought are eliminated.
7171 ??? Note we may be confused by uses in dead regions we didn't run
7172 elimination on. Rather than checking individual uses we accept
7173 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7174 contains such example). */
7177 if (gphi
*phi
= dyn_cast
<gphi
*> (stmt
))
7179 tree lhs
= gimple_phi_result (phi
);
7180 if (!has_zero_uses (lhs
))
7182 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7183 fprintf (dump_file
, "Keeping eliminated stmt live "
7184 "as copy because of out-of-region uses\n");
7185 tree sprime
= eliminate_avail (gimple_bb (stmt
), lhs
);
7186 gimple
*copy
= gimple_build_assign (lhs
, sprime
);
7187 gimple_stmt_iterator gsi
7188 = gsi_after_labels (gimple_bb (stmt
));
7189 gsi_insert_before (&gsi
, copy
, GSI_SAME_STMT
);
7190 do_release_defs
= false;
7193 else if (tree lhs
= gimple_get_lhs (stmt
))
7194 if (TREE_CODE (lhs
) == SSA_NAME
7195 && !has_zero_uses (lhs
))
7197 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7198 fprintf (dump_file
, "Keeping eliminated stmt live "
7199 "as copy because of out-of-region uses\n");
7200 tree sprime
= eliminate_avail (gimple_bb (stmt
), lhs
);
7201 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
7202 if (is_gimple_assign (stmt
))
7204 gimple_assign_set_rhs_from_tree (&gsi
, sprime
);
7205 stmt
= gsi_stmt (gsi
);
7207 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
))
7208 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
7213 gimple
*copy
= gimple_build_assign (lhs
, sprime
);
7214 gsi_insert_before (&gsi
, copy
, GSI_SAME_STMT
);
7215 do_release_defs
= false;
7220 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7222 fprintf (dump_file
, "Removing dead stmt ");
7223 print_gimple_stmt (dump_file
, stmt
, 0, TDF_NONE
);
7226 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
7227 if (gimple_code (stmt
) == GIMPLE_PHI
)
7228 remove_phi_node (&gsi
, do_release_defs
);
7231 basic_block bb
= gimple_bb (stmt
);
7232 unlink_stmt_vdef (stmt
);
7233 if (gsi_remove (&gsi
, true))
7234 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
7235 if (is_gimple_call (stmt
) && stmt_can_make_abnormal_goto (stmt
))
7236 bitmap_set_bit (need_ab_cleanup
, bb
->index
);
7237 if (do_release_defs
)
7238 release_defs (stmt
);
7241 /* Removing a stmt may expose a forwarder block. */
7242 el_todo
|= TODO_cleanup_cfg
;
7245 /* Fixup stmts that became noreturn calls. This may require splitting
7246 blocks and thus isn't possible during the dominator walk. Do this
7247 in reverse order so we don't inadvertedly remove a stmt we want to
7248 fixup by visiting a dominating now noreturn call first. */
7249 while (!to_fixup
.is_empty ())
7251 gimple
*stmt
= to_fixup
.pop ();
7253 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7255 fprintf (dump_file
, "Fixing up noreturn call ");
7256 print_gimple_stmt (dump_file
, stmt
, 0);
7259 if (fixup_noreturn_call (stmt
))
7260 el_todo
|= TODO_cleanup_cfg
;
7263 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
7264 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
7267 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
7270 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
7272 if (do_eh_cleanup
|| do_ab_cleanup
)
7273 el_todo
|= TODO_cleanup_cfg
;
7278 /* Eliminate fully redundant computations. */
7281 eliminate_with_rpo_vn (bitmap inserted_exprs
)
7283 eliminate_dom_walker
walker (CDI_DOMINATORS
, inserted_exprs
);
7285 eliminate_dom_walker
*saved_rpo_avail
= rpo_avail
;
7286 rpo_avail
= &walker
;
7287 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
7288 rpo_avail
= saved_rpo_avail
;
7290 return walker
.eliminate_cleanup ();
7294 do_rpo_vn_1 (function
*fn
, edge entry
, bitmap exit_bbs
,
7295 bool iterate
, bool eliminate
, vn_lookup_kind kind
);
7298 run_rpo_vn (vn_lookup_kind kind
)
7300 do_rpo_vn_1 (cfun
, NULL
, NULL
, true, false, kind
);
7302 /* ??? Prune requirement of these. */
7303 constant_to_value_id
= new hash_table
<vn_constant_hasher
> (23);
7305 /* Initialize the value ids and prune out remaining VN_TOPs
7309 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7311 vn_ssa_aux_t info
= VN_INFO (name
);
7313 || info
->valnum
== VN_TOP
)
7314 info
->valnum
= name
;
7315 if (info
->valnum
== name
)
7316 info
->value_id
= get_next_value_id ();
7317 else if (is_gimple_min_invariant (info
->valnum
))
7318 info
->value_id
= get_or_alloc_constant_value_id (info
->valnum
);
7322 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7324 vn_ssa_aux_t info
= VN_INFO (name
);
7325 if (TREE_CODE (info
->valnum
) == SSA_NAME
7326 && info
->valnum
!= name
7327 && info
->value_id
!= VN_INFO (info
->valnum
)->value_id
)
7328 info
->value_id
= VN_INFO (info
->valnum
)->value_id
;
7331 set_hashtable_value_ids ();
7333 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7335 fprintf (dump_file
, "Value numbers:\n");
7336 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7338 if (VN_INFO (name
)->visited
7339 && SSA_VAL (name
) != name
)
7341 print_generic_expr (dump_file
, name
);
7342 fprintf (dump_file
, " = ");
7343 print_generic_expr (dump_file
, SSA_VAL (name
));
7344 fprintf (dump_file
, " (%04d)\n", VN_INFO (name
)->value_id
);
7350 /* Free VN associated data structures. */
7355 free_vn_table (valid_info
);
7356 XDELETE (valid_info
);
7357 obstack_free (&vn_tables_obstack
, NULL
);
7358 obstack_free (&vn_tables_insert_obstack
, NULL
);
7360 vn_ssa_aux_iterator_type it
;
7362 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash
, info
, vn_ssa_aux_t
, it
)
7363 if (info
->needs_insertion
)
7364 release_ssa_name (info
->name
);
7365 obstack_free (&vn_ssa_aux_obstack
, NULL
);
7366 delete vn_ssa_aux_hash
;
7368 delete constant_to_value_id
;
7369 constant_to_value_id
= NULL
;
7372 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7375 vn_lookup_simplify_result (gimple_match_op
*res_op
)
7377 if (!res_op
->code
.is_tree_code ())
7379 tree
*ops
= res_op
->ops
;
7380 unsigned int length
= res_op
->num_ops
;
7381 if (res_op
->code
== CONSTRUCTOR
7382 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7383 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7384 && TREE_CODE (res_op
->ops
[0]) == CONSTRUCTOR
)
7386 length
= CONSTRUCTOR_NELTS (res_op
->ops
[0]);
7387 ops
= XALLOCAVEC (tree
, length
);
7388 for (unsigned i
= 0; i
< length
; ++i
)
7389 ops
[i
] = CONSTRUCTOR_ELT (res_op
->ops
[0], i
)->value
;
7391 vn_nary_op_t vnresult
= NULL
;
7392 tree res
= vn_nary_op_lookup_pieces (length
, (tree_code
) res_op
->code
,
7393 res_op
->type
, ops
, &vnresult
);
7394 /* If this is used from expression simplification make sure to
7395 return an available expression. */
7396 if (res
&& TREE_CODE (res
) == SSA_NAME
&& mprts_hook
&& rpo_avail
)
7397 res
= rpo_avail
->eliminate_avail (vn_context_bb
, res
);
7401 /* Return a leader for OPs value that is valid at BB. */
7404 rpo_elim::eliminate_avail (basic_block bb
, tree op
)
7407 tree valnum
= SSA_VAL (op
, &visited
);
7408 /* If we didn't visit OP then it must be defined outside of the
7409 region we process and also dominate it. So it is available. */
7412 if (TREE_CODE (valnum
) == SSA_NAME
)
7414 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
7416 vn_avail
*av
= VN_INFO (valnum
)->avail
;
7419 if (av
->location
== bb
->index
)
7420 /* On tramp3d 90% of the cases are here. */
7421 return ssa_name (av
->leader
);
7424 basic_block abb
= BASIC_BLOCK_FOR_FN (cfun
, av
->location
);
7425 /* ??? During elimination we have to use availability at the
7426 definition site of a use we try to replace. This
7427 is required to not run into inconsistencies because
7428 of dominated_by_p_w_unex behavior and removing a definition
7429 while not replacing all uses.
7430 ??? We could try to consistently walk dominators
7431 ignoring non-executable regions. The nearest common
7432 dominator of bb and abb is where we can stop walking. We
7433 may also be able to "pre-compute" (bits of) the next immediate
7434 (non-)dominator during the RPO walk when marking edges as
7436 if (dominated_by_p_w_unex (bb
, abb
, true))
7438 tree leader
= ssa_name (av
->leader
);
7439 /* Prevent eliminations that break loop-closed SSA. */
7440 if (loops_state_satisfies_p (LOOP_CLOSED_SSA
)
7441 && ! SSA_NAME_IS_DEFAULT_DEF (leader
)
7442 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7443 (leader
))->loop_father
,
7446 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7448 print_generic_expr (dump_file
, leader
);
7449 fprintf (dump_file
, " is available for ");
7450 print_generic_expr (dump_file
, valnum
);
7451 fprintf (dump_file
, "\n");
7453 /* On tramp3d 99% of the _remaining_ cases succeed at
7457 /* ??? Can we somehow skip to the immediate dominator
7458 RPO index (bb_to_rpo)? Again, maybe not worth, on
7459 tramp3d the worst number of elements in the vector is 9. */
7464 else if (valnum
!= VN_TOP
)
7465 /* valnum is is_gimple_min_invariant. */
7470 /* Make LEADER a leader for its value at BB. */
7473 rpo_elim::eliminate_push_avail (basic_block bb
, tree leader
)
7475 tree valnum
= VN_INFO (leader
)->valnum
;
7476 if (valnum
== VN_TOP
7477 || is_gimple_min_invariant (valnum
))
7479 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7481 fprintf (dump_file
, "Making available beyond BB%d ", bb
->index
);
7482 print_generic_expr (dump_file
, leader
);
7483 fprintf (dump_file
, " for value ");
7484 print_generic_expr (dump_file
, valnum
);
7485 fprintf (dump_file
, "\n");
7487 vn_ssa_aux_t value
= VN_INFO (valnum
);
7489 if (m_avail_freelist
)
7491 av
= m_avail_freelist
;
7492 m_avail_freelist
= m_avail_freelist
->next
;
7495 av
= XOBNEW (&vn_ssa_aux_obstack
, vn_avail
);
7496 av
->location
= bb
->index
;
7497 av
->leader
= SSA_NAME_VERSION (leader
);
7498 av
->next
= value
->avail
;
7499 av
->next_undo
= last_pushed_avail
;
7500 last_pushed_avail
= value
;
7504 /* Valueization hook for RPO VN plus required state. */
7507 rpo_vn_valueize (tree name
)
7509 if (TREE_CODE (name
) == SSA_NAME
)
7511 vn_ssa_aux_t val
= VN_INFO (name
);
7514 tree tem
= val
->valnum
;
7515 if (tem
!= VN_TOP
&& tem
!= name
)
7517 if (TREE_CODE (tem
) != SSA_NAME
)
7519 /* For all values we only valueize to an available leader
7520 which means we can use SSA name info without restriction. */
7521 tem
= rpo_avail
->eliminate_avail (vn_context_bb
, tem
);
7530 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7531 inverted condition. */
7534 insert_related_predicates_on_edge (enum tree_code code
, tree
*ops
, edge pred_e
)
7539 /* a < b -> a {!,<}= b */
7540 vn_nary_op_insert_pieces_predicated (2, NE_EXPR
, boolean_type_node
,
7541 ops
, boolean_true_node
, 0, pred_e
);
7542 vn_nary_op_insert_pieces_predicated (2, LE_EXPR
, boolean_type_node
,
7543 ops
, boolean_true_node
, 0, pred_e
);
7544 /* a < b -> ! a {>,=} b */
7545 vn_nary_op_insert_pieces_predicated (2, GT_EXPR
, boolean_type_node
,
7546 ops
, boolean_false_node
, 0, pred_e
);
7547 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR
, boolean_type_node
,
7548 ops
, boolean_false_node
, 0, pred_e
);
7551 /* a > b -> a {!,>}= b */
7552 vn_nary_op_insert_pieces_predicated (2, NE_EXPR
, boolean_type_node
,
7553 ops
, boolean_true_node
, 0, pred_e
);
7554 vn_nary_op_insert_pieces_predicated (2, GE_EXPR
, boolean_type_node
,
7555 ops
, boolean_true_node
, 0, pred_e
);
7556 /* a > b -> ! a {<,=} b */
7557 vn_nary_op_insert_pieces_predicated (2, LT_EXPR
, boolean_type_node
,
7558 ops
, boolean_false_node
, 0, pred_e
);
7559 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR
, boolean_type_node
,
7560 ops
, boolean_false_node
, 0, pred_e
);
7563 /* a == b -> ! a {<,>} b */
7564 vn_nary_op_insert_pieces_predicated (2, LT_EXPR
, boolean_type_node
,
7565 ops
, boolean_false_node
, 0, pred_e
);
7566 vn_nary_op_insert_pieces_predicated (2, GT_EXPR
, boolean_type_node
,
7567 ops
, boolean_false_node
, 0, pred_e
);
7572 /* Nothing besides inverted condition. */
7578 /* Main stmt worker for RPO VN, process BB. */
7581 process_bb (rpo_elim
&avail
, basic_block bb
,
7582 bool bb_visited
, bool iterate_phis
, bool iterate
, bool eliminate
,
7583 bool do_region
, bitmap exit_bbs
, bool skip_phis
)
7591 /* If we are in loop-closed SSA preserve this state. This is
7592 relevant when called on regions from outside of FRE/PRE. */
7593 bool lc_phi_nodes
= false;
7595 && loops_state_satisfies_p (LOOP_CLOSED_SSA
))
7596 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
7597 if (e
->src
->loop_father
!= e
->dest
->loop_father
7598 && flow_loop_nested_p (e
->dest
->loop_father
,
7599 e
->src
->loop_father
))
7601 lc_phi_nodes
= true;
7605 /* When we visit a loop header substitute into loop info. */
7606 if (!iterate
&& eliminate
&& bb
->loop_father
->header
== bb
)
7608 /* Keep fields in sync with substitute_in_loop_info. */
7609 if (bb
->loop_father
->nb_iterations
)
7610 bb
->loop_father
->nb_iterations
7611 = simplify_replace_tree (bb
->loop_father
->nb_iterations
,
7612 NULL_TREE
, NULL_TREE
, &vn_valueize_for_srt
);
7615 /* Value-number all defs in the basic-block. */
7617 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7620 gphi
*phi
= gsi
.phi ();
7621 tree res
= PHI_RESULT (phi
);
7622 vn_ssa_aux_t res_info
= VN_INFO (res
);
7625 gcc_assert (!res_info
->visited
);
7626 res_info
->valnum
= VN_TOP
;
7627 res_info
->visited
= true;
7630 /* When not iterating force backedge values to varying. */
7631 visit_stmt (phi
, !iterate_phis
);
7632 if (virtual_operand_p (res
))
7636 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
7637 how we handle backedges and availability.
7638 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
7639 tree val
= res_info
->valnum
;
7640 if (res
!= val
&& !iterate
&& eliminate
)
7642 if (tree leader
= avail
.eliminate_avail (bb
, res
))
7645 /* Preserve loop-closed SSA form. */
7647 || is_gimple_min_invariant (leader
)))
7649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7651 fprintf (dump_file
, "Replaced redundant PHI node "
7653 print_generic_expr (dump_file
, res
);
7654 fprintf (dump_file
, " with ");
7655 print_generic_expr (dump_file
, leader
);
7656 fprintf (dump_file
, "\n");
7658 avail
.eliminations
++;
7660 if (may_propagate_copy (res
, leader
))
7662 /* Schedule for removal. */
7663 avail
.to_remove
.safe_push (phi
);
7666 /* ??? Else generate a copy stmt. */
7670 /* Only make defs available that not already are. But make
7671 sure loop-closed SSA PHI node defs are picked up for
7675 || ! avail
.eliminate_avail (bb
, res
))
7676 avail
.eliminate_push_avail (bb
, res
);
7679 /* For empty BBs mark outgoing edges executable. For non-empty BBs
7680 we do this when processing the last stmt as we have to do this
7681 before elimination which otherwise forces GIMPLE_CONDs to
7682 if (1 != 0) style when seeing non-executable edges. */
7683 if (gsi_end_p (gsi_start_bb (bb
)))
7685 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
7687 if (!(e
->flags
& EDGE_EXECUTABLE
))
7689 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7691 "marking outgoing edge %d -> %d executable\n",
7692 e
->src
->index
, e
->dest
->index
);
7693 e
->flags
|= EDGE_EXECUTABLE
;
7694 e
->dest
->flags
|= BB_EXECUTABLE
;
7696 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
7698 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7700 "marking destination block %d reachable\n",
7702 e
->dest
->flags
|= BB_EXECUTABLE
;
7706 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
7707 !gsi_end_p (gsi
); gsi_next (&gsi
))
7713 FOR_EACH_SSA_TREE_OPERAND (op
, gsi_stmt (gsi
), i
, SSA_OP_ALL_DEFS
)
7715 vn_ssa_aux_t op_info
= VN_INFO (op
);
7716 gcc_assert (!op_info
->visited
);
7717 op_info
->valnum
= VN_TOP
;
7718 op_info
->visited
= true;
7721 /* We somehow have to deal with uses that are not defined
7722 in the processed region. Forcing unvisited uses to
7723 varying here doesn't play well with def-use following during
7724 expression simplification, so we deal with this by checking
7725 the visited flag in SSA_VAL. */
7728 visit_stmt (gsi_stmt (gsi
));
7730 gimple
*last
= gsi_stmt (gsi
);
7732 switch (gimple_code (last
))
7735 e
= find_taken_edge (bb
, vn_valueize (gimple_switch_index
7736 (as_a
<gswitch
*> (last
))));
7740 tree lhs
= vn_valueize (gimple_cond_lhs (last
));
7741 tree rhs
= vn_valueize (gimple_cond_rhs (last
));
7742 tree val
= gimple_simplify (gimple_cond_code (last
),
7743 boolean_type_node
, lhs
, rhs
,
7745 /* If the condition didn't simplfy see if we have recorded
7746 an expression from sofar taken edges. */
7747 if (! val
|| TREE_CODE (val
) != INTEGER_CST
)
7749 vn_nary_op_t vnresult
;
7753 val
= vn_nary_op_lookup_pieces (2, gimple_cond_code (last
),
7754 boolean_type_node
, ops
,
7756 /* Did we get a predicated value? */
7757 if (! val
&& vnresult
&& vnresult
->predicated_values
)
7759 val
= vn_nary_op_get_predicated_value (vnresult
, bb
);
7760 if (val
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
7762 fprintf (dump_file
, "Got predicated value ");
7763 print_generic_expr (dump_file
, val
, TDF_NONE
);
7764 fprintf (dump_file
, " for ");
7765 print_gimple_stmt (dump_file
, last
, TDF_SLIM
);
7770 e
= find_taken_edge (bb
, val
);
7773 /* If we didn't manage to compute the taken edge then
7774 push predicated expressions for the condition itself
7775 and related conditions to the hashtables. This allows
7776 simplification of redundant conditions which is
7777 important as early cleanup. */
7778 edge true_e
, false_e
;
7779 extract_true_false_edges_from_block (bb
, &true_e
, &false_e
);
7780 enum tree_code code
= gimple_cond_code (last
);
7781 enum tree_code icode
7782 = invert_tree_comparison (code
, HONOR_NANS (lhs
));
7787 && bitmap_bit_p (exit_bbs
, true_e
->dest
->index
))
7790 && bitmap_bit_p (exit_bbs
, false_e
->dest
->index
))
7793 vn_nary_op_insert_pieces_predicated
7794 (2, code
, boolean_type_node
, ops
,
7795 boolean_true_node
, 0, true_e
);
7797 vn_nary_op_insert_pieces_predicated
7798 (2, code
, boolean_type_node
, ops
,
7799 boolean_false_node
, 0, false_e
);
7800 if (icode
!= ERROR_MARK
)
7803 vn_nary_op_insert_pieces_predicated
7804 (2, icode
, boolean_type_node
, ops
,
7805 boolean_false_node
, 0, true_e
);
7807 vn_nary_op_insert_pieces_predicated
7808 (2, icode
, boolean_type_node
, ops
,
7809 boolean_true_node
, 0, false_e
);
7811 /* Relax for non-integers, inverted condition handled
7813 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
7816 insert_related_predicates_on_edge (code
, ops
, true_e
);
7818 insert_related_predicates_on_edge (icode
, ops
, false_e
);
7824 e
= find_taken_edge (bb
, vn_valueize (gimple_goto_dest (last
)));
7831 todo
= TODO_cleanup_cfg
;
7832 if (!(e
->flags
& EDGE_EXECUTABLE
))
7834 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7836 "marking known outgoing %sedge %d -> %d executable\n",
7837 e
->flags
& EDGE_DFS_BACK
? "back-" : "",
7838 e
->src
->index
, e
->dest
->index
);
7839 e
->flags
|= EDGE_EXECUTABLE
;
7840 e
->dest
->flags
|= BB_EXECUTABLE
;
7842 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
7844 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7846 "marking destination block %d reachable\n",
7848 e
->dest
->flags
|= BB_EXECUTABLE
;
7851 else if (gsi_one_before_end_p (gsi
))
7853 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
7855 if (!(e
->flags
& EDGE_EXECUTABLE
))
7857 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7859 "marking outgoing edge %d -> %d executable\n",
7860 e
->src
->index
, e
->dest
->index
);
7861 e
->flags
|= EDGE_EXECUTABLE
;
7862 e
->dest
->flags
|= BB_EXECUTABLE
;
7864 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
7866 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7868 "marking destination block %d reachable\n",
7870 e
->dest
->flags
|= BB_EXECUTABLE
;
7875 /* Eliminate. That also pushes to avail. */
7876 if (eliminate
&& ! iterate
)
7877 avail
.eliminate_stmt (bb
, &gsi
);
7879 /* If not eliminating, make all not already available defs
7881 FOR_EACH_SSA_TREE_OPERAND (op
, gsi_stmt (gsi
), i
, SSA_OP_DEF
)
7882 if (! avail
.eliminate_avail (bb
, op
))
7883 avail
.eliminate_push_avail (bb
, op
);
7886 /* Eliminate in destination PHI arguments. Always substitute in dest
7887 PHIs, even for non-executable edges. This handles region
7889 if (!iterate
&& eliminate
)
7890 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
7891 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
7892 !gsi_end_p (gsi
); gsi_next (&gsi
))
7894 gphi
*phi
= gsi
.phi ();
7895 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
7896 tree arg
= USE_FROM_PTR (use_p
);
7897 if (TREE_CODE (arg
) != SSA_NAME
7898 || virtual_operand_p (arg
))
7901 if (SSA_NAME_IS_DEFAULT_DEF (arg
))
7903 sprime
= SSA_VAL (arg
);
7904 gcc_assert (TREE_CODE (sprime
) != SSA_NAME
7905 || SSA_NAME_IS_DEFAULT_DEF (sprime
));
7908 /* Look for sth available at the definition block of the argument.
7909 This avoids inconsistencies between availability there which
7910 decides if the stmt can be removed and availability at the
7911 use site. The SSA property ensures that things available
7912 at the definition are also available at uses. */
7913 sprime
= avail
.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg
)),
7917 && may_propagate_copy (arg
, sprime
))
7918 propagate_value (use_p
, sprime
);
7921 vn_context_bb
= NULL
;
7925 /* Unwind state per basic-block. */
7929 /* Times this block has been visited. */
7931 /* Whether to handle this as iteration point or whether to treat
7932 incoming backedge PHI values as varying. */
7934 /* Maximum RPO index this block is reachable from. */
7938 vn_reference_t ref_top
;
7940 vn_nary_op_t nary_top
;
7941 vn_avail
*avail_top
;
7944 /* Unwind the RPO VN state for iteration. */
7947 do_unwind (unwind_state
*to
, rpo_elim
&avail
)
7949 gcc_assert (to
->iterate
);
7950 for (; last_inserted_nary
!= to
->nary_top
;
7951 last_inserted_nary
= last_inserted_nary
->next
)
7954 slot
= valid_info
->nary
->find_slot_with_hash
7955 (last_inserted_nary
, last_inserted_nary
->hashcode
, NO_INSERT
);
7956 /* Predication causes the need to restore previous state. */
7957 if ((*slot
)->unwind_to
)
7958 *slot
= (*slot
)->unwind_to
;
7960 valid_info
->nary
->clear_slot (slot
);
7962 for (; last_inserted_phi
!= to
->phi_top
;
7963 last_inserted_phi
= last_inserted_phi
->next
)
7966 slot
= valid_info
->phis
->find_slot_with_hash
7967 (last_inserted_phi
, last_inserted_phi
->hashcode
, NO_INSERT
);
7968 valid_info
->phis
->clear_slot (slot
);
7970 for (; last_inserted_ref
!= to
->ref_top
;
7971 last_inserted_ref
= last_inserted_ref
->next
)
7973 vn_reference_t
*slot
;
7974 slot
= valid_info
->references
->find_slot_with_hash
7975 (last_inserted_ref
, last_inserted_ref
->hashcode
, NO_INSERT
);
7976 (*slot
)->operands
.release ();
7977 valid_info
->references
->clear_slot (slot
);
7979 obstack_free (&vn_tables_obstack
, to
->ob_top
);
7981 /* Prune [rpo_idx, ] from avail. */
7982 for (; last_pushed_avail
&& last_pushed_avail
->avail
!= to
->avail_top
;)
7984 vn_ssa_aux_t val
= last_pushed_avail
;
7985 vn_avail
*av
= val
->avail
;
7986 val
->avail
= av
->next
;
7987 last_pushed_avail
= av
->next_undo
;
7988 av
->next
= avail
.m_avail_freelist
;
7989 avail
.m_avail_freelist
= av
;
7993 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
7994 If ITERATE is true then treat backedges optimistically as not
7995 executed and iterate. If ELIMINATE is true then perform
7996 elimination, otherwise leave that to the caller. */
7999 do_rpo_vn_1 (function
*fn
, edge entry
, bitmap exit_bbs
,
8000 bool iterate
, bool eliminate
, vn_lookup_kind kind
)
8003 default_vn_walk_kind
= kind
;
8005 /* We currently do not support region-based iteration when
8006 elimination is requested. */
8007 gcc_assert (!entry
|| !iterate
|| !eliminate
);
8008 /* When iterating we need loop info up-to-date. */
8009 gcc_assert (!iterate
|| !loops_state_satisfies_p (LOOPS_NEED_FIXUP
));
8011 bool do_region
= entry
!= NULL
;
8014 entry
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn
));
8015 exit_bbs
= BITMAP_ALLOC (NULL
);
8016 bitmap_set_bit (exit_bbs
, EXIT_BLOCK
);
8019 /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8020 re-mark those that are contained in the region. */
8023 FOR_EACH_EDGE (e
, ei
, entry
->dest
->preds
)
8024 e
->flags
&= ~EDGE_DFS_BACK
;
8026 int *rpo
= XNEWVEC (int, n_basic_blocks_for_fn (fn
) - NUM_FIXED_BLOCKS
);
8027 auto_vec
<std::pair
<int, int> > toplevel_scc_extents
;
8028 int n
= rev_post_order_and_mark_dfs_back_seme
8029 (fn
, entry
, exit_bbs
, true, rpo
, !iterate
? &toplevel_scc_extents
: NULL
);
8032 BITMAP_FREE (exit_bbs
);
8034 /* If there are any non-DFS_BACK edges into entry->dest skip
8035 processing PHI nodes for that block. This supports
8036 value-numbering loop bodies w/o the actual loop. */
8037 FOR_EACH_EDGE (e
, ei
, entry
->dest
->preds
)
8039 && !(e
->flags
& EDGE_DFS_BACK
))
8041 bool skip_entry_phis
= e
!= NULL
;
8042 if (skip_entry_phis
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
8043 fprintf (dump_file
, "Region does not contain all edges into "
8044 "the entry block, skipping its PHIs.\n");
8046 int *bb_to_rpo
= XNEWVEC (int, last_basic_block_for_fn (fn
));
8047 for (int i
= 0; i
< n
; ++i
)
8048 bb_to_rpo
[rpo
[i
]] = i
;
8050 unwind_state
*rpo_state
= XNEWVEC (unwind_state
, n
);
8052 rpo_elim
avail (entry
->dest
);
8055 /* Verify we have no extra entries into the region. */
8056 if (flag_checking
&& do_region
)
8058 auto_bb_flag
bb_in_region (fn
);
8059 for (int i
= 0; i
< n
; ++i
)
8061 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8062 bb
->flags
|= bb_in_region
;
8064 /* We can't merge the first two loops because we cannot rely
8065 on EDGE_DFS_BACK for edges not within the region. But if
8066 we decide to always have the bb_in_region flag we can
8067 do the checking during the RPO walk itself (but then it's
8068 also easy to handle MEME conservatively). */
8069 for (int i
= 0; i
< n
; ++i
)
8071 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8074 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8075 gcc_assert (e
== entry
8076 || (skip_entry_phis
&& bb
== entry
->dest
)
8077 || (e
->src
->flags
& bb_in_region
));
8079 for (int i
= 0; i
< n
; ++i
)
8081 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8082 bb
->flags
&= ~bb_in_region
;
8086 /* Create the VN state. For the initial size of the various hashtables
8087 use a heuristic based on region size and number of SSA names. */
8088 unsigned region_size
= (((unsigned HOST_WIDE_INT
)n
* num_ssa_names
)
8089 / (n_basic_blocks_for_fn (fn
) - NUM_FIXED_BLOCKS
));
8090 VN_TOP
= create_tmp_var_raw (void_type_node
, "vn_top");
8092 next_constant_value_id
= -1;
8094 vn_ssa_aux_hash
= new hash_table
<vn_ssa_aux_hasher
> (region_size
* 2);
8095 gcc_obstack_init (&vn_ssa_aux_obstack
);
8097 gcc_obstack_init (&vn_tables_obstack
);
8098 gcc_obstack_init (&vn_tables_insert_obstack
);
8099 valid_info
= XCNEW (struct vn_tables_s
);
8100 allocate_vn_table (valid_info
, region_size
);
8101 last_inserted_ref
= NULL
;
8102 last_inserted_phi
= NULL
;
8103 last_inserted_nary
= NULL
;
8104 last_pushed_avail
= NULL
;
8106 vn_valueize
= rpo_vn_valueize
;
8108 /* Initialize the unwind state and edge/BB executable state. */
8109 unsigned curr_scc
= 0;
8110 for (int i
= 0; i
< n
; ++i
)
8112 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8113 rpo_state
[i
].visited
= 0;
8114 rpo_state
[i
].max_rpo
= i
;
8115 if (!iterate
&& curr_scc
< toplevel_scc_extents
.length ())
8117 if (i
>= toplevel_scc_extents
[curr_scc
].first
8118 && i
<= toplevel_scc_extents
[curr_scc
].second
)
8119 rpo_state
[i
].max_rpo
= toplevel_scc_extents
[curr_scc
].second
;
8120 if (i
== toplevel_scc_extents
[curr_scc
].second
)
8123 bb
->flags
&= ~BB_EXECUTABLE
;
8124 bool has_backedges
= false;
8127 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8129 if (e
->flags
& EDGE_DFS_BACK
)
8130 has_backedges
= true;
8131 e
->flags
&= ~EDGE_EXECUTABLE
;
8132 if (iterate
|| e
== entry
|| (skip_entry_phis
&& bb
== entry
->dest
))
8135 rpo_state
[i
].iterate
= iterate
&& has_backedges
;
8137 entry
->flags
|= EDGE_EXECUTABLE
;
8138 entry
->dest
->flags
|= BB_EXECUTABLE
;
8140 /* As heuristic to improve compile-time we handle only the N innermost
8141 loops and the outermost one optimistically. */
8144 unsigned max_depth
= param_rpo_vn_max_loop_depth
;
8145 for (auto loop
: loops_list (cfun
, LI_ONLY_INNERMOST
))
8146 if (loop_depth (loop
) > max_depth
)
8147 for (unsigned i
= 2;
8148 i
< loop_depth (loop
) - max_depth
; ++i
)
8150 basic_block header
= superloop_at_depth (loop
, i
)->header
;
8151 bool non_latch_backedge
= false;
8154 FOR_EACH_EDGE (e
, ei
, header
->preds
)
8155 if (e
->flags
& EDGE_DFS_BACK
)
8157 /* There can be a non-latch backedge into the header
8158 which is part of an outer irreducible region. We
8159 cannot avoid iterating this block then. */
8160 if (!dominated_by_p (CDI_DOMINATORS
,
8163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8164 fprintf (dump_file
, "non-latch backedge %d -> %d "
8165 "forces iteration of loop %d\n",
8166 e
->src
->index
, e
->dest
->index
, loop
->num
);
8167 non_latch_backedge
= true;
8170 e
->flags
|= EDGE_EXECUTABLE
;
8172 rpo_state
[bb_to_rpo
[header
->index
]].iterate
= non_latch_backedge
;
8179 /* Go and process all blocks, iterating as necessary. */
8182 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[idx
]);
8184 /* If the block has incoming backedges remember unwind state. This
8185 is required even for non-executable blocks since in irreducible
8186 regions we might reach them via the backedge and re-start iterating
8188 Note we can individually mark blocks with incoming backedges to
8189 not iterate where we then handle PHIs conservatively. We do that
8190 heuristically to reduce compile-time for degenerate cases. */
8191 if (rpo_state
[idx
].iterate
)
8193 rpo_state
[idx
].ob_top
= obstack_alloc (&vn_tables_obstack
, 0);
8194 rpo_state
[idx
].ref_top
= last_inserted_ref
;
8195 rpo_state
[idx
].phi_top
= last_inserted_phi
;
8196 rpo_state
[idx
].nary_top
= last_inserted_nary
;
8197 rpo_state
[idx
].avail_top
8198 = last_pushed_avail
? last_pushed_avail
->avail
: NULL
;
8201 if (!(bb
->flags
& BB_EXECUTABLE
))
8203 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8204 fprintf (dump_file
, "Block %d: BB%d found not executable\n",
8210 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8211 fprintf (dump_file
, "Processing block %d: BB%d\n", idx
, bb
->index
);
8213 todo
|= process_bb (avail
, bb
,
8214 rpo_state
[idx
].visited
!= 0,
8215 rpo_state
[idx
].iterate
,
8216 iterate
, eliminate
, do_region
, exit_bbs
, false);
8217 rpo_state
[idx
].visited
++;
8219 /* Verify if changed values flow over executable outgoing backedges
8220 and those change destination PHI values (that's the thing we
8221 can easily verify). Reduce over all such edges to the farthest
8223 int iterate_to
= -1;
8226 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8227 if ((e
->flags
& (EDGE_DFS_BACK
|EDGE_EXECUTABLE
))
8228 == (EDGE_DFS_BACK
|EDGE_EXECUTABLE
)
8229 && rpo_state
[bb_to_rpo
[e
->dest
->index
]].iterate
)
8231 int destidx
= bb_to_rpo
[e
->dest
->index
];
8232 if (!rpo_state
[destidx
].visited
)
8234 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8235 fprintf (dump_file
, "Unvisited destination %d\n",
8237 if (iterate_to
== -1 || destidx
< iterate_to
)
8238 iterate_to
= destidx
;
8241 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8242 fprintf (dump_file
, "Looking for changed values of backedge"
8243 " %d->%d destination PHIs\n",
8244 e
->src
->index
, e
->dest
->index
);
8245 vn_context_bb
= e
->dest
;
8247 for (gsi
= gsi_start_phis (e
->dest
);
8248 !gsi_end_p (gsi
); gsi_next (&gsi
))
8250 bool inserted
= false;
8251 /* While we'd ideally just iterate on value changes
8252 we CSE PHIs and do that even across basic-block
8253 boundaries. So even hashtable state changes can
8254 be important (which is roughly equivalent to
8255 PHI argument value changes). To not excessively
8256 iterate because of that we track whether a PHI
8257 was CSEd to with GF_PLF_1. */
8258 bool phival_changed
;
8259 if ((phival_changed
= visit_phi (gsi
.phi (),
8261 || (inserted
&& gimple_plf (gsi
.phi (), GF_PLF_1
)))
8264 && dump_file
&& (dump_flags
& TDF_DETAILS
))
8265 fprintf (dump_file
, "PHI was CSEd and hashtable "
8266 "state (changed)\n");
8267 if (iterate_to
== -1 || destidx
< iterate_to
)
8268 iterate_to
= destidx
;
8272 vn_context_bb
= NULL
;
8274 if (iterate_to
!= -1)
8276 do_unwind (&rpo_state
[iterate_to
], avail
);
8278 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8279 fprintf (dump_file
, "Iterating to %d BB%d\n",
8280 iterate_to
, rpo
[iterate_to
]);
8290 /* Process all blocks greedily with a worklist that enforces RPO
8291 processing of reachable blocks. */
8292 auto_bitmap worklist
;
8293 bitmap_set_bit (worklist
, 0);
8294 while (!bitmap_empty_p (worklist
))
8296 int idx
= bitmap_first_set_bit (worklist
);
8297 bitmap_clear_bit (worklist
, idx
);
8298 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[idx
]);
8299 gcc_assert ((bb
->flags
& BB_EXECUTABLE
)
8300 && !rpo_state
[idx
].visited
);
8302 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8303 fprintf (dump_file
, "Processing block %d: BB%d\n", idx
, bb
->index
);
8305 /* When we run into predecessor edges where we cannot trust its
8306 executable state mark them executable so PHI processing will
8308 ??? Do we need to force arguments flowing over that edge
8309 to be varying or will they even always be? */
8312 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8313 if (!(e
->flags
& EDGE_EXECUTABLE
)
8314 && (bb
== entry
->dest
8315 || (!rpo_state
[bb_to_rpo
[e
->src
->index
]].visited
8316 && (rpo_state
[bb_to_rpo
[e
->src
->index
]].max_rpo
8319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8320 fprintf (dump_file
, "Cannot trust state of predecessor "
8321 "edge %d -> %d, marking executable\n",
8322 e
->src
->index
, e
->dest
->index
);
8323 e
->flags
|= EDGE_EXECUTABLE
;
8327 todo
|= process_bb (avail
, bb
, false, false, false, eliminate
,
8328 do_region
, exit_bbs
,
8329 skip_entry_phis
&& bb
== entry
->dest
);
8330 rpo_state
[idx
].visited
++;
8332 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8333 if ((e
->flags
& EDGE_EXECUTABLE
)
8334 && e
->dest
->index
!= EXIT_BLOCK
8335 && (!do_region
|| !bitmap_bit_p (exit_bbs
, e
->dest
->index
))
8336 && !rpo_state
[bb_to_rpo
[e
->dest
->index
]].visited
)
8337 bitmap_set_bit (worklist
, bb_to_rpo
[e
->dest
->index
]);
8341 /* If statistics or dump file active. */
8343 unsigned max_visited
= 1;
8344 for (int i
= 0; i
< n
; ++i
)
8346 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8347 if (bb
->flags
& BB_EXECUTABLE
)
8349 statistics_histogram_event (cfun
, "RPO block visited times",
8350 rpo_state
[i
].visited
);
8351 if (rpo_state
[i
].visited
> max_visited
)
8352 max_visited
= rpo_state
[i
].visited
;
8354 unsigned nvalues
= 0, navail
= 0;
8355 for (hash_table
<vn_ssa_aux_hasher
>::iterator i
= vn_ssa_aux_hash
->begin ();
8356 i
!= vn_ssa_aux_hash
->end (); ++i
)
8359 vn_avail
*av
= (*i
)->avail
;
8366 statistics_counter_event (cfun
, "RPO blocks", n
);
8367 statistics_counter_event (cfun
, "RPO blocks visited", nblk
);
8368 statistics_counter_event (cfun
, "RPO blocks executable", nex
);
8369 statistics_histogram_event (cfun
, "RPO iterations", 10*nblk
/ nex
);
8370 statistics_histogram_event (cfun
, "RPO num values", nvalues
);
8371 statistics_histogram_event (cfun
, "RPO num avail", navail
);
8372 statistics_histogram_event (cfun
, "RPO num lattice",
8373 vn_ssa_aux_hash
->elements ());
8374 if (dump_file
&& (dump_flags
& (TDF_DETAILS
|TDF_STATS
)))
8376 fprintf (dump_file
, "RPO iteration over %d blocks visited %" PRIu64
8377 " blocks in total discovering %d executable blocks iterating "
8378 "%d.%d times, a block was visited max. %u times\n",
8380 (int)((10*nblk
/ nex
)/10), (int)((10*nblk
/ nex
)%10),
8382 fprintf (dump_file
, "RPO tracked %d values available at %d locations "
8383 "and %" PRIu64
" lattice elements\n",
8384 nvalues
, navail
, (uint64_t) vn_ssa_aux_hash
->elements ());
8389 /* When !iterate we already performed elimination during the RPO
8393 /* Elimination for region-based VN needs to be done within the
8395 gcc_assert (! do_region
);
8396 /* Note we can't use avail.walk here because that gets confused
8397 by the existing availability and it will be less efficient
8399 todo
|= eliminate_with_rpo_vn (NULL
);
8402 todo
|= avail
.eliminate_cleanup (do_region
);
8408 XDELETEVEC (bb_to_rpo
);
8410 XDELETEVEC (rpo_state
);
8415 /* Region-based entry for RPO VN. Performs value-numbering and elimination
8416 on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
8417 the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
8419 If ITERATE is true then treat backedges optimistically as not
8420 executed and iterate. If ELIMINATE is true then perform
8421 elimination, otherwise leave that to the caller.
8422 KIND specifies the amount of work done for handling memory operations. */
8425 do_rpo_vn (function
*fn
, edge entry
, bitmap exit_bbs
,
8426 bool iterate
, bool eliminate
, vn_lookup_kind kind
)
8428 auto_timevar
tv (TV_TREE_RPO_VN
);
8429 unsigned todo
= do_rpo_vn_1 (fn
, entry
, exit_bbs
, iterate
, eliminate
, kind
);
8437 const pass_data pass_data_fre
=
8439 GIMPLE_PASS
, /* type */
8441 OPTGROUP_NONE
, /* optinfo_flags */
8442 TV_TREE_FRE
, /* tv_id */
8443 ( PROP_cfg
| PROP_ssa
), /* properties_required */
8444 0, /* properties_provided */
8445 0, /* properties_destroyed */
8446 0, /* todo_flags_start */
8447 0, /* todo_flags_finish */
8450 class pass_fre
: public gimple_opt_pass
8453 pass_fre (gcc::context
*ctxt
)
8454 : gimple_opt_pass (pass_data_fre
, ctxt
), may_iterate (true)
8457 /* opt_pass methods: */
8458 opt_pass
* clone () final override
{ return new pass_fre (m_ctxt
); }
8459 void set_pass_param (unsigned int n
, bool param
) final override
8461 gcc_assert (n
== 0);
8462 may_iterate
= param
;
8464 bool gate (function
*) final override
8466 return flag_tree_fre
!= 0 && (may_iterate
|| optimize
> 1);
8468 unsigned int execute (function
*) final override
;
8472 }; // class pass_fre
8475 pass_fre::execute (function
*fun
)
8479 /* At -O[1g] use the cheap non-iterating mode. */
8480 bool iterate_p
= may_iterate
&& (optimize
> 1);
8481 calculate_dominance_info (CDI_DOMINATORS
);
8483 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
8485 todo
= do_rpo_vn_1 (fun
, NULL
, NULL
, iterate_p
, true, VN_WALKREWRITE
);
8489 loop_optimizer_finalize ();
8491 if (scev_initialized_p ())
8494 /* For late FRE after IVOPTs and unrolling, see if we can
8495 remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8497 todo
|= TODO_update_address_taken
;
8505 make_pass_fre (gcc::context
*ctxt
)
8507 return new pass_fre (ctxt
);
8510 #undef BB_EXECUTABLE