1 /* SCC value numbering for trees
2 Copyright (C) 2006-2024 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"
30 #include "insn-config.h"
34 #include "gimple-pretty-print.h"
35 #include "splay-tree-utils.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"
77 #include "alloc-pool.h"
78 #include "symbol-summary.h"
84 /* This algorithm is based on the SCC algorithm presented by Keith
85 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
86 (http://citeseer.ist.psu.edu/41805.html). In
87 straight line code, it is equivalent to a regular hash based value
88 numbering that is performed in reverse postorder.
90 For code with cycles, there are two alternatives, both of which
91 require keeping the hashtables separate from the actual list of
92 value numbers for SSA names.
94 1. Iterate value numbering in an RPO walk of the blocks, removing
95 all the entries from the hashtable after each iteration (but
96 keeping the SSA name->value number mapping between iterations).
97 Iterate until it does not change.
99 2. Perform value numbering as part of an SCC walk on the SSA graph,
100 iterating only the cycles in the SSA graph until they do not change
101 (using a separate, optimistic hashtable for value numbering the SCC
104 The second is not just faster in practice (because most SSA graph
105 cycles do not involve all the variables in the graph), it also has
106 some nice properties.
108 One of these nice properties is that when we pop an SCC off the
109 stack, we are guaranteed to have processed all the operands coming from
110 *outside of that SCC*, so we do not need to do anything special to
111 ensure they have value numbers.
113 Another nice property is that the SCC walk is done as part of a DFS
114 of the SSA graph, which makes it easy to perform combining and
115 simplifying operations at the same time.
117 The code below is deliberately written in a way that makes it easy
118 to separate the SCC walk from the other work it does.
120 In order to propagate constants through the code, we track which
121 expressions contain constants, and use those while folding. In
122 theory, we could also track expressions whose value numbers are
123 replaced, in case we end up folding based on expression
126 In order to value number memory, we assign value numbers to vuses.
127 This enables us to note that, for example, stores to the same
128 address of the same value from the same starting memory states are
132 1. We can iterate only the changing portions of the SCC's, but
133 I have not seen an SCC big enough for this to be a win.
134 2. If you differentiate between phi nodes for loops and phi nodes
135 for if-then-else, you can properly consider phi nodes in different
136 blocks for equivalence.
137 3. We could value number vuses in more cases, particularly, whole
141 /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
142 #define BB_EXECUTABLE BB_VISITED
144 static vn_lookup_kind default_vn_walk_kind
;
146 /* vn_nary_op hashtable helpers. */
148 struct vn_nary_op_hasher
: nofree_ptr_hash
<vn_nary_op_s
>
150 typedef vn_nary_op_s
*compare_type
;
151 static inline hashval_t
hash (const vn_nary_op_s
*);
152 static inline bool equal (const vn_nary_op_s
*, const vn_nary_op_s
*);
155 /* Return the computed hashcode for nary operation P1. */
158 vn_nary_op_hasher::hash (const vn_nary_op_s
*vno1
)
160 return vno1
->hashcode
;
163 /* Compare nary operations P1 and P2 and return true if they are
167 vn_nary_op_hasher::equal (const vn_nary_op_s
*vno1
, const vn_nary_op_s
*vno2
)
169 return vno1
== vno2
|| vn_nary_op_eq (vno1
, vno2
);
172 typedef hash_table
<vn_nary_op_hasher
> vn_nary_op_table_type
;
173 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type
;
176 /* vn_phi hashtable helpers. */
179 vn_phi_eq (const_vn_phi_t
const vp1
, const_vn_phi_t
const vp2
);
181 struct vn_phi_hasher
: nofree_ptr_hash
<vn_phi_s
>
183 static inline hashval_t
hash (const vn_phi_s
*);
184 static inline bool equal (const vn_phi_s
*, const vn_phi_s
*);
187 /* Return the computed hashcode for phi operation P1. */
190 vn_phi_hasher::hash (const vn_phi_s
*vp1
)
192 return vp1
->hashcode
;
195 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
198 vn_phi_hasher::equal (const vn_phi_s
*vp1
, const vn_phi_s
*vp2
)
200 return vp1
== vp2
|| vn_phi_eq (vp1
, vp2
);
203 typedef hash_table
<vn_phi_hasher
> vn_phi_table_type
;
204 typedef vn_phi_table_type::iterator vn_phi_iterator_type
;
207 /* Compare two reference operands P1 and P2 for equality. Return true if
208 they are equal, and false otherwise. */
211 vn_reference_op_eq (const void *p1
, const void *p2
)
213 const_vn_reference_op_t
const vro1
= (const_vn_reference_op_t
) p1
;
214 const_vn_reference_op_t
const vro2
= (const_vn_reference_op_t
) p2
;
216 return (vro1
->opcode
== vro2
->opcode
217 /* We do not care for differences in type qualification. */
218 && (vro1
->type
== vro2
->type
219 || (vro1
->type
&& vro2
->type
220 && types_compatible_p (TYPE_MAIN_VARIANT (vro1
->type
),
221 TYPE_MAIN_VARIANT (vro2
->type
))))
222 && expressions_equal_p (vro1
->op0
, vro2
->op0
)
223 && expressions_equal_p (vro1
->op1
, vro2
->op1
)
224 && expressions_equal_p (vro1
->op2
, vro2
->op2
)
225 && (vro1
->opcode
!= CALL_EXPR
|| vro1
->clique
== vro2
->clique
));
228 /* Free a reference operation structure VP. */
231 free_reference (vn_reference_s
*vr
)
233 vr
->operands
.release ();
237 /* vn_reference hashtable helpers. */
239 struct vn_reference_hasher
: nofree_ptr_hash
<vn_reference_s
>
241 static inline hashval_t
hash (const vn_reference_s
*);
242 static inline bool equal (const vn_reference_s
*, const vn_reference_s
*);
245 /* Return the hashcode for a given reference operation P1. */
248 vn_reference_hasher::hash (const vn_reference_s
*vr1
)
250 return vr1
->hashcode
;
254 vn_reference_hasher::equal (const vn_reference_s
*v
, const vn_reference_s
*c
)
256 return v
== c
|| vn_reference_eq (v
, c
);
259 typedef hash_table
<vn_reference_hasher
> vn_reference_table_type
;
260 typedef vn_reference_table_type::iterator vn_reference_iterator_type
;
262 /* Pretty-print OPS to OUTFILE. */
265 print_vn_reference_ops (FILE *outfile
, const vec
<vn_reference_op_s
> ops
)
267 vn_reference_op_t vro
;
269 fprintf (outfile
, "{");
270 for (i
= 0; ops
.iterate (i
, &vro
); i
++)
272 bool closebrace
= false;
273 if (vro
->opcode
!= SSA_NAME
274 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
276 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
277 if (vro
->op0
|| vro
->opcode
== CALL_EXPR
)
279 fprintf (outfile
, "<");
283 if (vro
->op0
|| vro
->opcode
== CALL_EXPR
)
286 fprintf (outfile
, internal_fn_name ((internal_fn
)vro
->clique
));
288 print_generic_expr (outfile
, vro
->op0
);
291 fprintf (outfile
, ",");
292 print_generic_expr (outfile
, vro
->op1
);
296 fprintf (outfile
, ",");
297 print_generic_expr (outfile
, vro
->op2
);
301 fprintf (outfile
, ">");
302 if (i
!= ops
.length () - 1)
303 fprintf (outfile
, ",");
305 fprintf (outfile
, "}");
309 debug_vn_reference_ops (const vec
<vn_reference_op_s
> ops
)
311 print_vn_reference_ops (stderr
, ops
);
312 fputc ('\n', stderr
);
315 /* The set of VN hashtables. */
317 typedef struct vn_tables_s
319 vn_nary_op_table_type
*nary
;
320 vn_phi_table_type
*phis
;
321 vn_reference_table_type
*references
;
325 /* vn_constant hashtable helpers. */
327 struct vn_constant_hasher
: free_ptr_hash
<vn_constant_s
>
329 static inline hashval_t
hash (const vn_constant_s
*);
330 static inline bool equal (const vn_constant_s
*, const vn_constant_s
*);
333 /* Hash table hash function for vn_constant_t. */
336 vn_constant_hasher::hash (const vn_constant_s
*vc1
)
338 return vc1
->hashcode
;
341 /* Hash table equality function for vn_constant_t. */
344 vn_constant_hasher::equal (const vn_constant_s
*vc1
, const vn_constant_s
*vc2
)
346 if (vc1
->hashcode
!= vc2
->hashcode
)
349 return vn_constant_eq_with_type (vc1
->constant
, vc2
->constant
);
352 static hash_table
<vn_constant_hasher
> *constant_to_value_id
;
355 /* Obstack we allocate the vn-tables elements from. */
356 static obstack vn_tables_obstack
;
357 /* Special obstack we never unwind. */
358 static obstack vn_tables_insert_obstack
;
360 static vn_reference_t last_inserted_ref
;
361 static vn_phi_t last_inserted_phi
;
362 static vn_nary_op_t last_inserted_nary
;
363 static vn_ssa_aux_t last_pushed_avail
;
365 /* Valid hashtables storing information we have proven to be
367 static vn_tables_t valid_info
;
370 /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
371 an SSA name, otherwise just return it. */
372 tree (*vn_valueize
) (tree
);
374 vn_valueize_for_srt (tree t
, void* context ATTRIBUTE_UNUSED
)
376 basic_block saved_vn_context_bb
= vn_context_bb
;
377 /* Look for sth available at the definition block of the argument.
378 This avoids inconsistencies between availability there which
379 decides if the stmt can be removed and availability at the
380 use site. The SSA property ensures that things available
381 at the definition are also available at uses. */
382 if (!SSA_NAME_IS_DEFAULT_DEF (t
))
383 vn_context_bb
= gimple_bb (SSA_NAME_DEF_STMT (t
));
384 tree res
= vn_valueize (t
);
385 vn_context_bb
= saved_vn_context_bb
;
390 /* This represents the top of the VN lattice, which is the universal
395 /* Unique counter for our value ids. */
397 static unsigned int next_value_id
;
398 static int next_constant_value_id
;
401 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
402 are allocated on an obstack for locality reasons, and to free them
403 without looping over the vec. */
405 struct vn_ssa_aux_hasher
: typed_noop_remove
<vn_ssa_aux_t
>
407 typedef vn_ssa_aux_t value_type
;
408 typedef tree compare_type
;
409 static inline hashval_t
hash (const value_type
&);
410 static inline bool equal (const value_type
&, const compare_type
&);
411 static inline void mark_deleted (value_type
&) {}
412 static const bool empty_zero_p
= true;
413 static inline void mark_empty (value_type
&e
) { e
= NULL
; }
414 static inline bool is_deleted (value_type
&) { return false; }
415 static inline bool is_empty (value_type
&e
) { return e
== NULL
; }
419 vn_ssa_aux_hasher::hash (const value_type
&entry
)
421 return SSA_NAME_VERSION (entry
->name
);
425 vn_ssa_aux_hasher::equal (const value_type
&entry
, const compare_type
&name
)
427 return name
== entry
->name
;
430 static hash_table
<vn_ssa_aux_hasher
> *vn_ssa_aux_hash
;
431 typedef hash_table
<vn_ssa_aux_hasher
>::iterator vn_ssa_aux_iterator_type
;
432 static struct obstack vn_ssa_aux_obstack
;
434 static vn_nary_op_t
vn_nary_op_insert_stmt (gimple
*, tree
);
435 static vn_nary_op_t
vn_nary_op_insert_into (vn_nary_op_t
,
436 vn_nary_op_table_type
*);
437 static void init_vn_nary_op_from_pieces (vn_nary_op_t
, unsigned int,
438 enum tree_code
, tree
, tree
*);
439 static tree
vn_lookup_simplify_result (gimple_match_op
*);
440 static vn_reference_t vn_reference_lookup_or_insert_for_pieces
441 (tree
, alias_set_type
, alias_set_type
, poly_int64
, poly_int64
, tree
,
442 vec
<vn_reference_op_s
, va_heap
>, tree
);
444 /* Return whether there is value numbering information for a given SSA name. */
447 has_VN_INFO (tree name
)
449 return vn_ssa_aux_hash
->find_with_hash (name
, SSA_NAME_VERSION (name
));
456 = vn_ssa_aux_hash
->find_slot_with_hash (name
, SSA_NAME_VERSION (name
),
461 vn_ssa_aux_t newinfo
= *res
= XOBNEW (&vn_ssa_aux_obstack
, struct vn_ssa_aux
);
462 memset (newinfo
, 0, sizeof (struct vn_ssa_aux
));
463 newinfo
->name
= name
;
464 newinfo
->valnum
= VN_TOP
;
465 /* We are using the visited flag to handle uses with defs not within the
466 region being value-numbered. */
467 newinfo
->visited
= false;
469 /* Given we create the VN_INFOs on-demand now we have to do initialization
470 different than VN_TOP here. */
471 if (SSA_NAME_IS_DEFAULT_DEF (name
))
472 switch (TREE_CODE (SSA_NAME_VAR (name
)))
475 /* All undefined vars are VARYING. */
476 newinfo
->valnum
= name
;
477 newinfo
->visited
= true;
481 /* Parameters are VARYING but we can record a condition
482 if we know it is a non-NULL pointer. */
483 newinfo
->visited
= true;
484 newinfo
->valnum
= name
;
485 if (POINTER_TYPE_P (TREE_TYPE (name
))
486 && nonnull_arg_p (SSA_NAME_VAR (name
)))
490 ops
[1] = build_int_cst (TREE_TYPE (name
), 0);
492 /* Allocate from non-unwinding stack. */
493 nary
= alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack
);
494 init_vn_nary_op_from_pieces (nary
, 2, NE_EXPR
,
495 boolean_type_node
, ops
);
496 nary
->predicated_values
= 0;
497 nary
->u
.result
= boolean_true_node
;
498 vn_nary_op_insert_into (nary
, valid_info
->nary
);
499 gcc_assert (nary
->unwind_to
== NULL
);
500 /* Also do not link it into the undo chain. */
501 last_inserted_nary
= nary
->next
;
502 nary
->next
= (vn_nary_op_t
)(void *)-1;
503 nary
= alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack
);
504 init_vn_nary_op_from_pieces (nary
, 2, EQ_EXPR
,
505 boolean_type_node
, ops
);
506 nary
->predicated_values
= 0;
507 nary
->u
.result
= boolean_false_node
;
508 vn_nary_op_insert_into (nary
, valid_info
->nary
);
509 gcc_assert (nary
->unwind_to
== NULL
);
510 last_inserted_nary
= nary
->next
;
511 nary
->next
= (vn_nary_op_t
)(void *)-1;
512 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
514 fprintf (dump_file
, "Recording ");
515 print_generic_expr (dump_file
, name
, TDF_SLIM
);
516 fprintf (dump_file
, " != 0\n");
522 /* If the result is passed by invisible reference the default
523 def is initialized, otherwise it's uninitialized. Still
524 undefined is varying. */
525 newinfo
->visited
= true;
526 newinfo
->valnum
= name
;
535 /* Return the SSA value of X. */
538 SSA_VAL (tree x
, bool *visited
= NULL
)
540 vn_ssa_aux_t tem
= vn_ssa_aux_hash
->find_with_hash (x
, SSA_NAME_VERSION (x
));
542 *visited
= tem
&& tem
->visited
;
543 return tem
&& tem
->visited
? tem
->valnum
: x
;
546 /* Return the SSA value of the VUSE x, supporting released VDEFs
547 during elimination which will value-number the VDEF to the
548 associated VUSE (but not substitute in the whole lattice). */
551 vuse_ssa_val (tree x
)
559 gcc_assert (x
!= VN_TOP
);
561 while (SSA_NAME_IN_FREE_LIST (x
));
566 /* Similar to the above but used as callback for walk_non_aliased_vuses
567 and thus should stop at unvisited VUSE to not walk across region
571 vuse_valueize (tree vuse
)
576 vuse
= SSA_VAL (vuse
, &visited
);
579 gcc_assert (vuse
!= VN_TOP
);
581 while (SSA_NAME_IN_FREE_LIST (vuse
));
586 /* Return the vn_kind the expression computed by the stmt should be
590 vn_get_stmt_kind (gimple
*stmt
)
592 switch (gimple_code (stmt
))
600 enum tree_code code
= gimple_assign_rhs_code (stmt
);
601 tree rhs1
= gimple_assign_rhs1 (stmt
);
602 switch (get_gimple_rhs_class (code
))
604 case GIMPLE_UNARY_RHS
:
605 case GIMPLE_BINARY_RHS
:
606 case GIMPLE_TERNARY_RHS
:
608 case GIMPLE_SINGLE_RHS
:
609 switch (TREE_CODE_CLASS (code
))
612 /* VOP-less references can go through unary case. */
613 if ((code
== REALPART_EXPR
614 || code
== IMAGPART_EXPR
615 || code
== VIEW_CONVERT_EXPR
616 || code
== BIT_FIELD_REF
)
617 && (TREE_CODE (TREE_OPERAND (rhs1
, 0)) == SSA_NAME
618 || is_gimple_min_invariant (TREE_OPERAND (rhs1
, 0))))
622 case tcc_declaration
:
629 if (code
== ADDR_EXPR
)
630 return (is_gimple_min_invariant (rhs1
)
631 ? VN_CONSTANT
: VN_REFERENCE
);
632 else if (code
== CONSTRUCTOR
)
645 /* Lookup a value id for CONSTANT and return it. If it does not
649 get_constant_value_id (tree constant
)
651 vn_constant_s
**slot
;
652 struct vn_constant_s vc
;
654 vc
.hashcode
= vn_hash_constant_with_type (constant
);
655 vc
.constant
= constant
;
656 slot
= constant_to_value_id
->find_slot (&vc
, NO_INSERT
);
658 return (*slot
)->value_id
;
662 /* Lookup a value id for CONSTANT, and if it does not exist, create a
663 new one and return it. If it does exist, return it. */
666 get_or_alloc_constant_value_id (tree constant
)
668 vn_constant_s
**slot
;
669 struct vn_constant_s vc
;
672 /* If the hashtable isn't initialized we're not running from PRE and thus
673 do not need value-ids. */
674 if (!constant_to_value_id
)
677 vc
.hashcode
= vn_hash_constant_with_type (constant
);
678 vc
.constant
= constant
;
679 slot
= constant_to_value_id
->find_slot (&vc
, INSERT
);
681 return (*slot
)->value_id
;
683 vcp
= XNEW (struct vn_constant_s
);
684 vcp
->hashcode
= vc
.hashcode
;
685 vcp
->constant
= constant
;
686 vcp
->value_id
= get_next_constant_value_id ();
688 return vcp
->value_id
;
691 /* Compute the hash for a reference operand VRO1. */
694 vn_reference_op_compute_hash (const vn_reference_op_t vro1
, inchash::hash
&hstate
)
696 hstate
.add_int (vro1
->opcode
);
697 if (vro1
->opcode
== CALL_EXPR
&& !vro1
->op0
)
698 hstate
.add_int (vro1
->clique
);
700 inchash::add_expr (vro1
->op0
, hstate
);
702 inchash::add_expr (vro1
->op1
, hstate
);
704 inchash::add_expr (vro1
->op2
, hstate
);
707 /* Compute a hash for the reference operation VR1 and return it. */
710 vn_reference_compute_hash (const vn_reference_t vr1
)
712 inchash::hash hstate
;
715 vn_reference_op_t vro
;
719 FOR_EACH_VEC_ELT (vr1
->operands
, i
, vro
)
721 if (vro
->opcode
== MEM_REF
)
723 else if (vro
->opcode
!= ADDR_EXPR
)
725 if (maybe_ne (vro
->off
, -1))
727 if (known_eq (off
, -1))
733 if (maybe_ne (off
, -1)
734 && maybe_ne (off
, 0))
735 hstate
.add_poly_int (off
);
738 && vro
->opcode
== ADDR_EXPR
)
742 tree op
= TREE_OPERAND (vro
->op0
, 0);
743 hstate
.add_int (TREE_CODE (op
));
744 inchash::add_expr (op
, hstate
);
748 vn_reference_op_compute_hash (vro
, hstate
);
751 /* Do not hash vr1->offset or vr1->max_size, we want to get collisions
752 to be able to identify compatible results. */
753 result
= hstate
.end ();
754 /* ??? We would ICE later if we hash instead of adding that in. */
756 result
+= SSA_NAME_VERSION (vr1
->vuse
);
761 /* Return true if reference operations VR1 and VR2 are equivalent. This
762 means they have the same set of operands and vuses. */
765 vn_reference_eq (const_vn_reference_t
const vr1
, const_vn_reference_t
const vr2
)
769 /* Early out if this is not a hash collision. */
770 if (vr1
->hashcode
!= vr2
->hashcode
)
773 /* The VOP needs to be the same. */
774 if (vr1
->vuse
!= vr2
->vuse
)
777 /* The offset/max_size used for the ao_ref during lookup has to be
779 if (maybe_ne (vr1
->offset
, vr2
->offset
)
780 || maybe_ne (vr1
->max_size
, vr2
->max_size
))
782 /* But nothing known in the prevailing entry is OK to be used. */
783 if (maybe_ne (vr1
->offset
, 0) || known_size_p (vr1
->max_size
))
787 /* If the operands are the same we are done. */
788 if (vr1
->operands
== vr2
->operands
)
791 if (!vr1
->type
|| !vr2
->type
)
793 if (vr1
->type
!= vr2
->type
)
796 else if (vr1
->type
== vr2
->type
)
798 else if (COMPLETE_TYPE_P (vr1
->type
) != COMPLETE_TYPE_P (vr2
->type
)
799 || (COMPLETE_TYPE_P (vr1
->type
)
800 && !expressions_equal_p (TYPE_SIZE (vr1
->type
),
801 TYPE_SIZE (vr2
->type
))))
803 else if (vr1
->operands
[0].opcode
== CALL_EXPR
804 && !types_compatible_p (vr1
->type
, vr2
->type
))
806 else if (INTEGRAL_TYPE_P (vr1
->type
)
807 && INTEGRAL_TYPE_P (vr2
->type
))
809 if (TYPE_PRECISION (vr1
->type
) != TYPE_PRECISION (vr2
->type
))
812 else if (INTEGRAL_TYPE_P (vr1
->type
)
813 && (TYPE_PRECISION (vr1
->type
)
814 != TREE_INT_CST_LOW (TYPE_SIZE (vr1
->type
))))
816 else if (INTEGRAL_TYPE_P (vr2
->type
)
817 && (TYPE_PRECISION (vr2
->type
)
818 != TREE_INT_CST_LOW (TYPE_SIZE (vr2
->type
))))
820 else if (VECTOR_BOOLEAN_TYPE_P (vr1
->type
)
821 && VECTOR_BOOLEAN_TYPE_P (vr2
->type
))
823 /* Vector boolean types can have padding, verify we are dealing with
824 the same number of elements, aka the precision of the types.
825 For example, In most architecture the precision_size of vbool*_t
826 types are caculated like below:
827 precision_size = type_size * 8
829 Unfortunately, the RISC-V will adjust the precision_size for the
830 vbool*_t in order to align the ISA as below:
831 type_size = [1, 1, 1, 1, 2, 4, 8]
832 precision_size = [1, 2, 4, 8, 16, 32, 64]
834 Then the precision_size of RISC-V vbool*_t will not be the multiple
835 of the type_size. We take care of this case consolidated here. */
836 if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1
->type
),
837 TYPE_VECTOR_SUBPARTS (vr2
->type
)))
840 else if (TYPE_MODE (vr1
->type
) != TYPE_MODE (vr2
->type
)
841 && (!mode_can_transfer_bits (TYPE_MODE (vr1
->type
))
842 || !mode_can_transfer_bits (TYPE_MODE (vr2
->type
))))
849 poly_int64 off1
= 0, off2
= 0;
850 vn_reference_op_t vro1
, vro2
;
851 vn_reference_op_s tem1
, tem2
;
852 bool deref1
= false, deref2
= false;
853 bool reverse1
= false, reverse2
= false;
854 for (; vr1
->operands
.iterate (i
, &vro1
); i
++)
856 if (vro1
->opcode
== MEM_REF
)
858 /* Do not look through a storage order barrier. */
859 else if (vro1
->opcode
== VIEW_CONVERT_EXPR
&& vro1
->reverse
)
861 reverse1
|= vro1
->reverse
;
862 if (known_eq (vro1
->off
, -1))
866 for (; vr2
->operands
.iterate (j
, &vro2
); j
++)
868 if (vro2
->opcode
== MEM_REF
)
870 /* Do not look through a storage order barrier. */
871 else if (vro2
->opcode
== VIEW_CONVERT_EXPR
&& vro2
->reverse
)
873 reverse2
|= vro2
->reverse
;
874 if (known_eq (vro2
->off
, -1))
878 if (maybe_ne (off1
, off2
) || reverse1
!= reverse2
)
880 if (deref1
&& vro1
->opcode
== ADDR_EXPR
)
882 memset (&tem1
, 0, sizeof (tem1
));
883 tem1
.op0
= TREE_OPERAND (vro1
->op0
, 0);
884 tem1
.type
= TREE_TYPE (tem1
.op0
);
885 tem1
.opcode
= TREE_CODE (tem1
.op0
);
889 if (deref2
&& vro2
->opcode
== ADDR_EXPR
)
891 memset (&tem2
, 0, sizeof (tem2
));
892 tem2
.op0
= TREE_OPERAND (vro2
->op0
, 0);
893 tem2
.type
= TREE_TYPE (tem2
.op0
);
894 tem2
.opcode
= TREE_CODE (tem2
.op0
);
898 if (deref1
!= deref2
)
900 if (!vn_reference_op_eq (vro1
, vro2
))
905 while (vr1
->operands
.length () != i
906 || vr2
->operands
.length () != j
);
911 /* Copy the operations present in load/store REF into RESULT, a vector of
912 vn_reference_op_s's. */
915 copy_reference_ops_from_ref (tree ref
, vec
<vn_reference_op_s
> *result
)
917 /* For non-calls, store the information that makes up the address. */
921 vn_reference_op_s temp
;
923 memset (&temp
, 0, sizeof (temp
));
924 temp
.type
= TREE_TYPE (ref
);
925 temp
.opcode
= TREE_CODE (ref
);
931 temp
.op0
= TREE_OPERAND (ref
, 1);
934 temp
.op0
= TREE_OPERAND (ref
, 1);
938 /* The base address gets its own vn_reference_op_s structure. */
939 temp
.op0
= TREE_OPERAND (ref
, 1);
940 if (!mem_ref_offset (ref
).to_shwi (&temp
.off
))
942 temp
.clique
= MR_DEPENDENCE_CLIQUE (ref
);
943 temp
.base
= MR_DEPENDENCE_BASE (ref
);
944 temp
.reverse
= REF_REVERSE_STORAGE_ORDER (ref
);
947 /* The base address gets its own vn_reference_op_s structure. */
948 temp
.op0
= TMR_INDEX (ref
);
949 temp
.op1
= TMR_STEP (ref
);
950 temp
.op2
= TMR_OFFSET (ref
);
951 temp
.clique
= MR_DEPENDENCE_CLIQUE (ref
);
952 temp
.base
= MR_DEPENDENCE_BASE (ref
);
953 result
->safe_push (temp
);
954 memset (&temp
, 0, sizeof (temp
));
955 temp
.type
= NULL_TREE
;
956 temp
.opcode
= ERROR_MARK
;
957 temp
.op0
= TMR_INDEX2 (ref
);
961 /* Record bits, position and storage order. */
962 temp
.op0
= TREE_OPERAND (ref
, 1);
963 temp
.op1
= TREE_OPERAND (ref
, 2);
964 if (!multiple_p (bit_field_offset (ref
), BITS_PER_UNIT
, &temp
.off
))
966 temp
.reverse
= REF_REVERSE_STORAGE_ORDER (ref
);
969 /* The field decl is enough to unambiguously specify the field,
970 so use its type here. */
971 temp
.type
= TREE_TYPE (TREE_OPERAND (ref
, 1));
972 temp
.op0
= TREE_OPERAND (ref
, 1);
973 temp
.op1
= TREE_OPERAND (ref
, 2);
974 temp
.reverse
= (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref
, 0)))
975 && TYPE_REVERSE_STORAGE_ORDER
976 (TREE_TYPE (TREE_OPERAND (ref
, 0))));
978 tree this_offset
= component_ref_field_offset (ref
);
980 && poly_int_tree_p (this_offset
))
982 tree bit_offset
= DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref
, 1));
983 if (TREE_INT_CST_LOW (bit_offset
) % BITS_PER_UNIT
== 0)
986 = (wi::to_poly_offset (this_offset
)
987 + (wi::to_offset (bit_offset
) >> LOG2_BITS_PER_UNIT
));
988 /* Probibit value-numbering zero offset components
989 of addresses the same before the pass folding
990 __builtin_object_size had a chance to run. */
991 if (TREE_CODE (orig
) != ADDR_EXPR
993 || (cfun
->curr_properties
& PROP_objsz
))
994 off
.to_shwi (&temp
.off
);
999 case ARRAY_RANGE_REF
:
1002 tree eltype
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref
, 0)));
1003 /* Record index as operand. */
1004 temp
.op0
= TREE_OPERAND (ref
, 1);
1005 /* Always record lower bounds and element size. */
1006 temp
.op1
= array_ref_low_bound (ref
);
1007 /* But record element size in units of the type alignment. */
1008 temp
.op2
= TREE_OPERAND (ref
, 3);
1009 temp
.align
= eltype
->type_common
.align
;
1011 temp
.op2
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (eltype
),
1012 size_int (TYPE_ALIGN_UNIT (eltype
)));
1013 if (poly_int_tree_p (temp
.op0
)
1014 && poly_int_tree_p (temp
.op1
)
1015 && TREE_CODE (temp
.op2
) == INTEGER_CST
)
1017 poly_offset_int off
= ((wi::to_poly_offset (temp
.op0
)
1018 - wi::to_poly_offset (temp
.op1
))
1019 * wi::to_offset (temp
.op2
)
1020 * vn_ref_op_align_unit (&temp
));
1021 off
.to_shwi (&temp
.off
);
1023 temp
.reverse
= (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref
, 0)))
1024 && TYPE_REVERSE_STORAGE_ORDER
1025 (TREE_TYPE (TREE_OPERAND (ref
, 0))));
1029 if (DECL_HARD_REGISTER (ref
))
1038 /* Canonicalize decls to MEM[&decl] which is what we end up with
1039 when valueizing MEM[ptr] with ptr = &decl. */
1040 temp
.opcode
= MEM_REF
;
1041 temp
.op0
= build_int_cst (build_pointer_type (TREE_TYPE (ref
)), 0);
1043 result
->safe_push (temp
);
1044 temp
.opcode
= ADDR_EXPR
;
1045 temp
.op0
= build1 (ADDR_EXPR
, TREE_TYPE (temp
.op0
), ref
);
1046 temp
.type
= TREE_TYPE (temp
.op0
);
1061 if (is_gimple_min_invariant (ref
))
1067 /* These are only interesting for their operands, their
1068 existence, and their type. They will never be the last
1069 ref in the chain of references (IE they require an
1070 operand), so we don't have to put anything
1071 for op* as it will be handled by the iteration */
1075 case VIEW_CONVERT_EXPR
:
1077 temp
.reverse
= storage_order_barrier_p (ref
);
1080 /* This is only interesting for its constant offset. */
1081 temp
.off
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref
)));
1086 result
->safe_push (temp
);
1088 if (REFERENCE_CLASS_P (ref
)
1089 || TREE_CODE (ref
) == MODIFY_EXPR
1090 || TREE_CODE (ref
) == WITH_SIZE_EXPR
1091 || (TREE_CODE (ref
) == ADDR_EXPR
1092 && !is_gimple_min_invariant (ref
)))
1093 ref
= TREE_OPERAND (ref
, 0);
1099 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1100 operands in *OPS, the reference alias set SET and the reference type TYPE.
1101 Return true if something useful was produced. */
1104 ao_ref_init_from_vn_reference (ao_ref
*ref
,
1105 alias_set_type set
, alias_set_type base_set
,
1106 tree type
, const vec
<vn_reference_op_s
> &ops
)
1109 tree base
= NULL_TREE
;
1110 tree
*op0_p
= &base
;
1111 poly_offset_int offset
= 0;
1112 poly_offset_int max_size
;
1113 poly_offset_int size
= -1;
1114 tree size_tree
= NULL_TREE
;
1116 /* We don't handle calls. */
1120 machine_mode mode
= TYPE_MODE (type
);
1121 if (mode
== BLKmode
)
1122 size_tree
= TYPE_SIZE (type
);
1124 size
= GET_MODE_BITSIZE (mode
);
1125 if (size_tree
!= NULL_TREE
1126 && poly_int_tree_p (size_tree
))
1127 size
= wi::to_poly_offset (size_tree
);
1129 /* Lower the final access size from the outermost expression. */
1130 const_vn_reference_op_t cst_op
= &ops
[0];
1131 /* Cast away constness for the sake of the const-unsafe
1132 FOR_EACH_VEC_ELT(). */
1133 vn_reference_op_t op
= const_cast<vn_reference_op_t
>(cst_op
);
1134 size_tree
= NULL_TREE
;
1135 if (op
->opcode
== COMPONENT_REF
)
1136 size_tree
= DECL_SIZE (op
->op0
);
1137 else if (op
->opcode
== BIT_FIELD_REF
)
1138 size_tree
= op
->op0
;
1139 if (size_tree
!= NULL_TREE
1140 && poly_int_tree_p (size_tree
)
1141 && (!known_size_p (size
)
1142 || known_lt (wi::to_poly_offset (size_tree
), size
)))
1143 size
= wi::to_poly_offset (size_tree
);
1145 /* Initially, maxsize is the same as the accessed element size.
1146 In the following it will only grow (or become -1). */
1149 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1150 and find the ultimate containing object. */
1151 FOR_EACH_VEC_ELT (ops
, i
, op
)
1158 /* Record the base objects. */
1160 *op0_p
= build2 (MEM_REF
, op
->type
,
1161 NULL_TREE
, op
->op0
);
1162 MR_DEPENDENCE_CLIQUE (*op0_p
) = op
->clique
;
1163 MR_DEPENDENCE_BASE (*op0_p
) = op
->base
;
1164 op0_p
= &TREE_OPERAND (*op0_p
, 0);
1167 case TARGET_MEM_REF
:
1168 *op0_p
= build5 (TARGET_MEM_REF
, op
->type
,
1169 NULL_TREE
, op
->op2
, op
->op0
,
1170 op
->op1
, ops
[i
+1].op0
);
1171 MR_DEPENDENCE_CLIQUE (*op0_p
) = op
->clique
;
1172 MR_DEPENDENCE_BASE (*op0_p
) = op
->base
;
1173 op0_p
= &TREE_OPERAND (*op0_p
, 0);
1177 /* Unwrap some of the wrapped decls. */
1179 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1180 if (base
!= NULL_TREE
1181 && TREE_CODE (base
) == MEM_REF
1183 && DECL_P (TREE_OPERAND (op
->op0
, 0)))
1185 const_vn_reference_op_t pop
= &ops
[i
-1];
1186 base
= TREE_OPERAND (op
->op0
, 0);
1187 if (known_eq (pop
->off
, -1))
1193 offset
+= pop
->off
* BITS_PER_UNIT
;
1201 /* ??? We shouldn't see these, but un-canonicalize what
1202 copy_reference_ops_from_ref does when visiting MEM_REF. */
1204 /* ??? And for this only have DECL_HARD_REGISTER. */
1206 /* This can show up in ARRAY_REF bases. */
1213 /* And now the usual component-reference style ops. */
1215 offset
+= wi::to_poly_offset (op
->op1
);
1220 tree field
= op
->op0
;
1221 /* We do not have a complete COMPONENT_REF tree here so we
1222 cannot use component_ref_field_offset. Do the interesting
1224 tree this_offset
= DECL_FIELD_OFFSET (field
);
1226 if (op
->op1
|| !poly_int_tree_p (this_offset
))
1230 poly_offset_int woffset
= (wi::to_poly_offset (this_offset
)
1231 << LOG2_BITS_PER_UNIT
);
1232 woffset
+= wi::to_offset (DECL_FIELD_BIT_OFFSET (field
));
1238 case ARRAY_RANGE_REF
:
1240 /* Use the recorded constant offset. */
1241 if (maybe_eq (op
->off
, -1))
1244 offset
+= op
->off
* BITS_PER_UNIT
;
1254 case VIEW_CONVERT_EXPR
:
1270 if (base
== NULL_TREE
)
1273 ref
->ref
= NULL_TREE
;
1275 ref
->ref_alias_set
= set
;
1276 ref
->base_alias_set
= base_set
;
1277 /* We discount volatiles from value-numbering elsewhere. */
1278 ref
->volatile_p
= false;
1280 if (!size
.to_shwi (&ref
->size
) || maybe_lt (ref
->size
, 0))
1288 if (!offset
.to_shwi (&ref
->offset
))
1295 if (!max_size
.to_shwi (&ref
->max_size
) || maybe_lt (ref
->max_size
, 0))
1301 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1302 vn_reference_op_s's. */
1305 copy_reference_ops_from_call (gcall
*call
,
1306 vec
<vn_reference_op_s
> *result
)
1308 vn_reference_op_s temp
;
1310 tree lhs
= gimple_call_lhs (call
);
1313 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1314 different. By adding the lhs here in the vector, we ensure that the
1315 hashcode is different, guaranteeing a different value number. */
1316 if (lhs
&& TREE_CODE (lhs
) != SSA_NAME
)
1318 memset (&temp
, 0, sizeof (temp
));
1319 temp
.opcode
= MODIFY_EXPR
;
1320 temp
.type
= TREE_TYPE (lhs
);
1323 result
->safe_push (temp
);
1326 /* Copy the type, opcode, function, static chain and EH region, if any. */
1327 memset (&temp
, 0, sizeof (temp
));
1328 temp
.type
= gimple_call_fntype (call
);
1329 temp
.opcode
= CALL_EXPR
;
1330 temp
.op0
= gimple_call_fn (call
);
1331 if (gimple_call_internal_p (call
))
1332 temp
.clique
= gimple_call_internal_fn (call
);
1333 temp
.op1
= gimple_call_chain (call
);
1334 if (stmt_could_throw_p (cfun
, call
) && (lr
= lookup_stmt_eh_lp (call
)) > 0)
1335 temp
.op2
= size_int (lr
);
1337 result
->safe_push (temp
);
1339 /* Copy the call arguments. As they can be references as well,
1340 just chain them together. */
1341 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
1343 tree callarg
= gimple_call_arg (call
, i
);
1344 copy_reference_ops_from_ref (callarg
, result
);
1348 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1349 *I_P to point to the last element of the replacement. */
1351 vn_reference_fold_indirect (vec
<vn_reference_op_s
> *ops
,
1354 unsigned int i
= *i_p
;
1355 vn_reference_op_t op
= &(*ops
)[i
];
1356 vn_reference_op_t mem_op
= &(*ops
)[i
- 1];
1358 poly_int64 addr_offset
= 0;
1360 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1361 from .foo.bar to the preceding MEM_REF offset and replace the
1362 address with &OBJ. */
1363 addr_base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (op
->op0
, 0),
1364 &addr_offset
, vn_valueize
);
1365 gcc_checking_assert (addr_base
&& TREE_CODE (addr_base
) != MEM_REF
);
1366 if (addr_base
!= TREE_OPERAND (op
->op0
, 0))
1369 = (poly_offset_int::from (wi::to_poly_wide (mem_op
->op0
),
1372 mem_op
->op0
= wide_int_to_tree (TREE_TYPE (mem_op
->op0
), off
);
1373 op
->op0
= build_fold_addr_expr (addr_base
);
1374 if (tree_fits_shwi_p (mem_op
->op0
))
1375 mem_op
->off
= tree_to_shwi (mem_op
->op0
);
1383 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1384 *I_P to point to the last element of the replacement. */
1386 vn_reference_maybe_forwprop_address (vec
<vn_reference_op_s
> *ops
,
1389 bool changed
= false;
1390 vn_reference_op_t op
;
1394 unsigned int i
= *i_p
;
1396 vn_reference_op_t mem_op
= &(*ops
)[i
- 1];
1398 enum tree_code code
;
1399 poly_offset_int off
;
1401 def_stmt
= SSA_NAME_DEF_STMT (op
->op0
);
1402 if (!is_gimple_assign (def_stmt
))
1405 code
= gimple_assign_rhs_code (def_stmt
);
1406 if (code
!= ADDR_EXPR
1407 && code
!= POINTER_PLUS_EXPR
)
1410 off
= poly_offset_int::from (wi::to_poly_wide (mem_op
->op0
), SIGNED
);
1412 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1413 from .foo.bar to the preceding MEM_REF offset and replace the
1414 address with &OBJ. */
1415 if (code
== ADDR_EXPR
)
1417 tree addr
, addr_base
;
1418 poly_int64 addr_offset
;
1420 addr
= gimple_assign_rhs1 (def_stmt
);
1421 addr_base
= get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr
, 0),
1424 /* If that didn't work because the address isn't invariant propagate
1425 the reference tree from the address operation in case the current
1426 dereference isn't offsetted. */
1428 && *i_p
== ops
->length () - 1
1429 && known_eq (off
, 0)
1430 /* This makes us disable this transform for PRE where the
1431 reference ops might be also used for code insertion which
1433 && default_vn_walk_kind
== VN_WALKREWRITE
)
1435 auto_vec
<vn_reference_op_s
, 32> tem
;
1436 copy_reference_ops_from_ref (TREE_OPERAND (addr
, 0), &tem
);
1437 /* Make sure to preserve TBAA info. The only objects not
1438 wrapped in MEM_REFs that can have their address taken are
1440 if (tem
.length () >= 2
1441 && tem
[tem
.length () - 2].opcode
== MEM_REF
)
1443 vn_reference_op_t new_mem_op
= &tem
[tem
.length () - 2];
1445 = wide_int_to_tree (TREE_TYPE (mem_op
->op0
),
1446 wi::to_poly_wide (new_mem_op
->op0
));
1449 gcc_assert (tem
.last ().opcode
== STRING_CST
);
1452 ops
->safe_splice (tem
);
1457 || TREE_CODE (addr_base
) != MEM_REF
1458 || (TREE_CODE (TREE_OPERAND (addr_base
, 0)) == SSA_NAME
1459 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base
,
1464 off
+= mem_ref_offset (addr_base
);
1465 op
->op0
= TREE_OPERAND (addr_base
, 0);
1470 ptr
= gimple_assign_rhs1 (def_stmt
);
1471 ptroff
= gimple_assign_rhs2 (def_stmt
);
1472 if (TREE_CODE (ptr
) != SSA_NAME
1473 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr
)
1474 /* Make sure to not endlessly recurse.
1475 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1476 happen when we value-number a PHI to its backedge value. */
1477 || SSA_VAL (ptr
) == op
->op0
1478 || !poly_int_tree_p (ptroff
))
1481 off
+= wi::to_poly_offset (ptroff
);
1485 mem_op
->op0
= wide_int_to_tree (TREE_TYPE (mem_op
->op0
), off
);
1486 if (tree_fits_shwi_p (mem_op
->op0
))
1487 mem_op
->off
= tree_to_shwi (mem_op
->op0
);
1490 /* ??? Can end up with endless recursion here!?
1491 gcc.c-torture/execute/strcmp-1.c */
1492 if (TREE_CODE (op
->op0
) == SSA_NAME
)
1493 op
->op0
= SSA_VAL (op
->op0
);
1494 if (TREE_CODE (op
->op0
) != SSA_NAME
)
1495 op
->opcode
= TREE_CODE (op
->op0
);
1500 while (TREE_CODE (op
->op0
) == SSA_NAME
);
1502 /* Fold a remaining *&. */
1503 if (TREE_CODE (op
->op0
) == ADDR_EXPR
)
1504 vn_reference_fold_indirect (ops
, i_p
);
1509 /* Optimize the reference REF to a constant if possible or return
1510 NULL_TREE if not. */
1513 fully_constant_vn_reference_p (vn_reference_t ref
)
1515 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1516 vn_reference_op_t op
;
1518 /* Try to simplify the translated expression if it is
1519 a call to a builtin function with at most two arguments. */
1521 if (op
->opcode
== CALL_EXPR
1523 || (TREE_CODE (op
->op0
) == ADDR_EXPR
1524 && TREE_CODE (TREE_OPERAND (op
->op0
, 0)) == FUNCTION_DECL
1525 && fndecl_built_in_p (TREE_OPERAND (op
->op0
, 0),
1527 && operands
.length () >= 2
1528 && operands
.length () <= 3)
1530 vn_reference_op_t arg0
, arg1
= NULL
;
1531 bool anyconst
= false;
1532 arg0
= &operands
[1];
1533 if (operands
.length () > 2)
1534 arg1
= &operands
[2];
1535 if (TREE_CODE_CLASS (arg0
->opcode
) == tcc_constant
1536 || (arg0
->opcode
== ADDR_EXPR
1537 && is_gimple_min_invariant (arg0
->op0
)))
1540 && (TREE_CODE_CLASS (arg1
->opcode
) == tcc_constant
1541 || (arg1
->opcode
== ADDR_EXPR
1542 && is_gimple_min_invariant (arg1
->op0
))))
1548 fn
= as_combined_fn (DECL_FUNCTION_CODE
1549 (TREE_OPERAND (op
->op0
, 0)));
1551 fn
= as_combined_fn ((internal_fn
) op
->clique
);
1554 folded
= fold_const_call (fn
, ref
->type
, arg0
->op0
, arg1
->op0
);
1556 folded
= fold_const_call (fn
, ref
->type
, arg0
->op0
);
1558 && is_gimple_min_invariant (folded
))
1563 /* Simplify reads from constants or constant initializers. */
1564 else if (BITS_PER_UNIT
== 8
1566 && COMPLETE_TYPE_P (ref
->type
)
1567 && is_gimple_reg_type (ref
->type
))
1571 if (INTEGRAL_TYPE_P (ref
->type
))
1572 size
= TYPE_PRECISION (ref
->type
);
1573 else if (tree_fits_shwi_p (TYPE_SIZE (ref
->type
)))
1574 size
= tree_to_shwi (TYPE_SIZE (ref
->type
));
1577 if (size
% BITS_PER_UNIT
!= 0
1578 || size
> MAX_BITSIZE_MODE_ANY_MODE
)
1580 size
/= BITS_PER_UNIT
;
1582 for (i
= 0; i
< operands
.length (); ++i
)
1584 if (TREE_CODE_CLASS (operands
[i
].opcode
) == tcc_constant
)
1589 if (known_eq (operands
[i
].off
, -1))
1591 off
+= operands
[i
].off
;
1592 if (operands
[i
].opcode
== MEM_REF
)
1598 vn_reference_op_t base
= &operands
[--i
];
1599 tree ctor
= error_mark_node
;
1600 tree decl
= NULL_TREE
;
1601 if (TREE_CODE_CLASS (base
->opcode
) == tcc_constant
)
1603 else if (base
->opcode
== MEM_REF
1604 && base
[1].opcode
== ADDR_EXPR
1605 && (VAR_P (TREE_OPERAND (base
[1].op0
, 0))
1606 || TREE_CODE (TREE_OPERAND (base
[1].op0
, 0)) == CONST_DECL
1607 || TREE_CODE (TREE_OPERAND (base
[1].op0
, 0)) == STRING_CST
))
1609 decl
= TREE_OPERAND (base
[1].op0
, 0);
1610 if (TREE_CODE (decl
) == STRING_CST
)
1613 ctor
= ctor_for_folding (decl
);
1615 if (ctor
== NULL_TREE
)
1616 return build_zero_cst (ref
->type
);
1617 else if (ctor
!= error_mark_node
)
1619 HOST_WIDE_INT const_off
;
1622 tree res
= fold_ctor_reference (ref
->type
, ctor
,
1623 off
* BITS_PER_UNIT
,
1624 size
* BITS_PER_UNIT
, decl
);
1627 STRIP_USELESS_TYPE_CONVERSION (res
);
1628 if (is_gimple_min_invariant (res
))
1632 else if (off
.is_constant (&const_off
))
1634 unsigned char buf
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
1635 int len
= native_encode_expr (ctor
, buf
, size
, const_off
);
1637 return native_interpret_expr (ref
->type
, buf
, len
);
1645 /* Return true if OPS contain a storage order barrier. */
1648 contains_storage_order_barrier_p (vec
<vn_reference_op_s
> ops
)
1650 vn_reference_op_t op
;
1653 FOR_EACH_VEC_ELT (ops
, i
, op
)
1654 if (op
->opcode
== VIEW_CONVERT_EXPR
&& op
->reverse
)
1660 /* Return true if OPS represent an access with reverse storage order. */
1663 reverse_storage_order_for_component_p (vec
<vn_reference_op_s
> ops
)
1666 if (ops
[i
].opcode
== REALPART_EXPR
|| ops
[i
].opcode
== IMAGPART_EXPR
)
1668 switch (ops
[i
].opcode
)
1674 return ops
[i
].reverse
;
1680 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1681 structures into their value numbers. This is done in-place, and
1682 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1683 whether any operands were valueized. */
1686 valueize_refs_1 (vec
<vn_reference_op_s
> *orig
, bool *valueized_anything
,
1687 bool with_avail
= false)
1689 *valueized_anything
= false;
1691 for (unsigned i
= 0; i
< orig
->length (); ++i
)
1694 vn_reference_op_t vro
= &(*orig
)[i
];
1695 if (vro
->opcode
== SSA_NAME
1696 || (vro
->op0
&& TREE_CODE (vro
->op0
) == SSA_NAME
))
1698 tree tem
= with_avail
? vn_valueize (vro
->op0
) : SSA_VAL (vro
->op0
);
1699 if (tem
!= vro
->op0
)
1701 *valueized_anything
= true;
1704 /* If it transforms from an SSA_NAME to a constant, update
1706 if (TREE_CODE (vro
->op0
) != SSA_NAME
&& vro
->opcode
== SSA_NAME
)
1707 vro
->opcode
= TREE_CODE (vro
->op0
);
1709 if (vro
->op1
&& TREE_CODE (vro
->op1
) == SSA_NAME
)
1711 tree tem
= with_avail
? vn_valueize (vro
->op1
) : SSA_VAL (vro
->op1
);
1712 if (tem
!= vro
->op1
)
1714 *valueized_anything
= true;
1718 if (vro
->op2
&& TREE_CODE (vro
->op2
) == SSA_NAME
)
1720 tree tem
= with_avail
? vn_valueize (vro
->op2
) : SSA_VAL (vro
->op2
);
1721 if (tem
!= vro
->op2
)
1723 *valueized_anything
= true;
1727 /* If it transforms from an SSA_NAME to an address, fold with
1728 a preceding indirect reference. */
1731 && TREE_CODE (vro
->op0
) == ADDR_EXPR
1732 && (*orig
)[i
- 1].opcode
== MEM_REF
)
1734 if (vn_reference_fold_indirect (orig
, &i
))
1735 *valueized_anything
= true;
1738 && vro
->opcode
== SSA_NAME
1739 && (*orig
)[i
- 1].opcode
== MEM_REF
)
1741 if (vn_reference_maybe_forwprop_address (orig
, &i
))
1743 *valueized_anything
= true;
1744 /* Re-valueize the current operand. */
1748 /* If it transforms a non-constant ARRAY_REF into a constant
1749 one, adjust the constant offset. */
1750 else if ((vro
->opcode
== ARRAY_REF
1751 || vro
->opcode
== ARRAY_RANGE_REF
)
1752 && known_eq (vro
->off
, -1)
1753 && poly_int_tree_p (vro
->op0
)
1754 && poly_int_tree_p (vro
->op1
)
1755 && TREE_CODE (vro
->op2
) == INTEGER_CST
)
1757 poly_offset_int off
= ((wi::to_poly_offset (vro
->op0
)
1758 - wi::to_poly_offset (vro
->op1
))
1759 * wi::to_offset (vro
->op2
)
1760 * vn_ref_op_align_unit (vro
));
1761 off
.to_shwi (&vro
->off
);
1767 valueize_refs (vec
<vn_reference_op_s
> *orig
)
1770 valueize_refs_1 (orig
, &tem
);
1773 static vec
<vn_reference_op_s
> shared_lookup_references
;
1775 /* Create a vector of vn_reference_op_s structures from REF, a
1776 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1777 this function. *VALUEIZED_ANYTHING will specify whether any
1778 operands were valueized. */
1780 static vec
<vn_reference_op_s
>
1781 valueize_shared_reference_ops_from_ref (tree ref
, bool *valueized_anything
)
1785 shared_lookup_references
.truncate (0);
1786 copy_reference_ops_from_ref (ref
, &shared_lookup_references
);
1787 valueize_refs_1 (&shared_lookup_references
, valueized_anything
);
1788 return shared_lookup_references
;
1791 /* Create a vector of vn_reference_op_s structures from CALL, a
1792 call statement. The vector is shared among all callers of
1795 static vec
<vn_reference_op_s
>
1796 valueize_shared_reference_ops_from_call (gcall
*call
)
1800 shared_lookup_references
.truncate (0);
1801 copy_reference_ops_from_call (call
, &shared_lookup_references
);
1802 valueize_refs (&shared_lookup_references
);
1803 return shared_lookup_references
;
1806 /* Lookup a SCCVN reference operation VR in the current hash table.
1807 Returns the resulting value number if it exists in the hash table,
1808 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1809 vn_reference_t stored in the hashtable if something is found. */
1812 vn_reference_lookup_1 (vn_reference_t vr
, vn_reference_t
*vnresult
)
1814 vn_reference_s
**slot
;
1817 hash
= vr
->hashcode
;
1818 slot
= valid_info
->references
->find_slot_with_hash (vr
, hash
, NO_INSERT
);
1822 *vnresult
= (vn_reference_t
)*slot
;
1823 return ((vn_reference_t
)*slot
)->result
;
1830 /* Partial definition tracking support. */
1834 HOST_WIDE_INT offset
;
1836 pd_range
*m_children
[2];
1842 HOST_WIDE_INT rhs_off
;
1843 HOST_WIDE_INT offset
;
1847 /* Context for alias walking. */
1849 struct vn_walk_cb_data
1851 vn_walk_cb_data (vn_reference_t vr_
, tree orig_ref_
, tree
*last_vuse_ptr_
,
1852 vn_lookup_kind vn_walk_kind_
, bool tbaa_p_
, tree mask_
,
1853 bool redundant_store_removal_p_
)
1854 : vr (vr_
), last_vuse_ptr (last_vuse_ptr_
), last_vuse (NULL_TREE
),
1855 mask (mask_
), masked_result (NULL_TREE
), same_val (NULL_TREE
),
1856 vn_walk_kind (vn_walk_kind_
),
1857 tbaa_p (tbaa_p_
), redundant_store_removal_p (redundant_store_removal_p_
),
1858 saved_operands (vNULL
), first_range (), first_set (-2),
1862 last_vuse_ptr
= &last_vuse
;
1863 ao_ref_init (&orig_ref
, orig_ref_
);
1866 wide_int w
= wi::to_wide (mask
);
1867 unsigned int pos
= 0, prec
= w
.get_precision ();
1869 pd
.rhs
= build_constructor (NULL_TREE
, NULL
);
1871 /* When bitwise and with a constant is done on a memory load,
1872 we don't really need all the bits to be defined or defined
1873 to constants, we don't really care what is in the position
1874 corresponding to 0 bits in the mask.
1875 So, push the ranges of those 0 bits in the mask as artificial
1876 zero stores and let the partial def handling code do the
1880 int tz
= wi::ctz (w
);
1881 if (pos
+ tz
> prec
)
1885 if (BYTES_BIG_ENDIAN
)
1886 pd
.offset
= prec
- pos
- tz
;
1890 void *r
= push_partial_def (pd
, 0, 0, 0, prec
);
1891 gcc_assert (r
== NULL_TREE
);
1896 w
= wi::lrshift (w
, tz
);
1897 tz
= wi::ctz (wi::bit_not (w
));
1898 if (pos
+ tz
> prec
)
1901 w
= wi::lrshift (w
, tz
);
1905 ~vn_walk_cb_data ();
1906 void *finish (alias_set_type
, alias_set_type
, tree
);
1907 void *push_partial_def (pd_data pd
,
1908 alias_set_type
, alias_set_type
, HOST_WIDE_INT
,
1913 tree
*last_vuse_ptr
;
1918 vn_lookup_kind vn_walk_kind
;
1920 bool redundant_store_removal_p
;
1921 vec
<vn_reference_op_s
> saved_operands
;
1923 /* The VDEFs of partial defs we come along. */
1924 auto_vec
<pd_data
, 2> partial_defs
;
1925 /* The first defs range to avoid splay tree setup in most cases. */
1926 pd_range first_range
;
1927 alias_set_type first_set
;
1928 alias_set_type first_base_set
;
1929 default_splay_tree
<pd_range
*> known_ranges
;
1930 obstack ranges_obstack
;
1931 static constexpr HOST_WIDE_INT bufsize
= 64;
1934 vn_walk_cb_data::~vn_walk_cb_data ()
1937 obstack_free (&ranges_obstack
, NULL
);
1938 saved_operands
.release ();
1942 vn_walk_cb_data::finish (alias_set_type set
, alias_set_type base_set
, tree val
)
1944 if (first_set
!= -2)
1947 base_set
= first_base_set
;
1951 masked_result
= val
;
1954 if (same_val
&& !operand_equal_p (val
, same_val
))
1956 vec
<vn_reference_op_s
> &operands
1957 = saved_operands
.exists () ? saved_operands
: vr
->operands
;
1958 return vn_reference_lookup_or_insert_for_pieces (last_vuse
, set
, base_set
,
1959 vr
->offset
, vr
->max_size
,
1960 vr
->type
, operands
, val
);
1963 /* Push PD to the vector of partial definitions returning a
1964 value when we are ready to combine things with VUSE, SET and MAXSIZEI,
1965 NULL when we want to continue looking for partial defs or -1
1969 vn_walk_cb_data::push_partial_def (pd_data pd
,
1970 alias_set_type set
, alias_set_type base_set
,
1971 HOST_WIDE_INT offseti
,
1972 HOST_WIDE_INT maxsizei
)
1974 /* We're using a fixed buffer for encoding so fail early if the object
1975 we want to interpret is bigger. */
1976 if (maxsizei
> bufsize
* BITS_PER_UNIT
1978 || BITS_PER_UNIT
!= 8
1979 /* Not prepared to handle PDP endian. */
1980 || BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
1983 /* Turn too large constant stores into non-constant stores. */
1984 if (CONSTANT_CLASS_P (pd
.rhs
) && pd
.size
> bufsize
* BITS_PER_UNIT
)
1985 pd
.rhs
= error_mark_node
;
1987 /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
1988 most a partial byte before and/or after the region. */
1989 if (!CONSTANT_CLASS_P (pd
.rhs
))
1991 if (pd
.offset
< offseti
)
1993 HOST_WIDE_INT o
= ROUND_DOWN (offseti
- pd
.offset
, BITS_PER_UNIT
);
1994 gcc_assert (pd
.size
> o
);
1998 if (pd
.size
> maxsizei
)
1999 pd
.size
= maxsizei
+ ((pd
.size
- maxsizei
) % BITS_PER_UNIT
);
2002 pd
.offset
-= offseti
;
2004 bool pd_constant_p
= (TREE_CODE (pd
.rhs
) == CONSTRUCTOR
2005 || CONSTANT_CLASS_P (pd
.rhs
));
2007 if (partial_defs
.is_empty ())
2009 /* If we get a clobber upfront, fail. */
2010 if (TREE_CLOBBER_P (pd
.rhs
))
2014 partial_defs
.safe_push (pd
);
2015 first_range
.offset
= pd
.offset
;
2016 first_range
.size
= pd
.size
;
2018 first_base_set
= base_set
;
2019 last_vuse_ptr
= NULL
;
2021 /* Go check if the first partial definition was a full one in case
2022 the caller didn't optimize for this. */
2028 /* ??? Optimize the case where the 2nd partial def completes
2030 gcc_obstack_init (&ranges_obstack
);
2031 known_ranges
.insert_max_node (&first_range
);
2033 /* Lookup the offset and see if we need to merge. */
2034 int comparison
= known_ranges
.lookup_le
2035 ([&] (pd_range
*r
) { return pd
.offset
< r
->offset
; },
2036 [&] (pd_range
*r
) { return pd
.offset
> r
->offset
; });
2037 r
= known_ranges
.root ();
2039 && ranges_known_overlap_p (r
->offset
, r
->size
+ 1,
2040 pd
.offset
, pd
.size
))
2042 /* Ignore partial defs already covered. Here we also drop shadowed
2043 clobbers arriving here at the floor. */
2044 if (known_subrange_p (pd
.offset
, pd
.size
, r
->offset
, r
->size
))
2046 r
->size
= MAX (r
->offset
+ r
->size
, pd
.offset
+ pd
.size
) - r
->offset
;
2050 /* pd.offset wasn't covered yet, insert the range. */
2051 void *addr
= XOBNEW (&ranges_obstack
, pd_range
);
2052 r
= new (addr
) pd_range
{ pd
.offset
, pd
.size
, {} };
2053 known_ranges
.insert_relative (comparison
, r
);
2055 /* Merge r which now contains pd's range and is a member of the splay
2056 tree with adjacent overlapping ranges. */
2057 if (known_ranges
.splay_next_node ())
2060 pd_range
*rafter
= known_ranges
.root ();
2061 if (!ranges_known_overlap_p (r
->offset
, r
->size
+ 1,
2062 rafter
->offset
, rafter
->size
))
2064 r
->size
= MAX (r
->offset
+ r
->size
,
2065 rafter
->offset
+ rafter
->size
) - r
->offset
;
2067 while (known_ranges
.remove_root_and_splay_next ());
2068 /* If we get a clobber, fail. */
2069 if (TREE_CLOBBER_P (pd
.rhs
))
2071 /* Non-constants are OK as long as they are shadowed by a constant. */
2074 partial_defs
.safe_push (pd
);
2077 /* Now we have merged pd's range into the range tree. When we have covered
2078 [offseti, sizei] then the tree will contain exactly one node which has
2079 the desired properties and it will be 'r'. */
2080 if (!known_subrange_p (0, maxsizei
, r
->offset
, r
->size
))
2081 /* Continue looking for partial defs. */
2084 /* Now simply native encode all partial defs in reverse order. */
2085 unsigned ndefs
= partial_defs
.length ();
2086 /* We support up to 512-bit values (for V8DFmode). */
2087 unsigned char buffer
[bufsize
+ 1];
2088 unsigned char this_buffer
[bufsize
+ 1];
2091 memset (buffer
, 0, bufsize
+ 1);
2092 unsigned needed_len
= ROUND_UP (maxsizei
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
2093 while (!partial_defs
.is_empty ())
2095 pd_data pd
= partial_defs
.pop ();
2097 if (TREE_CODE (pd
.rhs
) == CONSTRUCTOR
)
2099 /* Empty CONSTRUCTOR. */
2100 if (pd
.size
>= needed_len
* BITS_PER_UNIT
)
2103 len
= ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
2104 memset (this_buffer
, 0, len
);
2106 else if (pd
.rhs_off
>= 0)
2108 len
= native_encode_expr (pd
.rhs
, this_buffer
, bufsize
,
2109 (MAX (0, -pd
.offset
)
2110 + pd
.rhs_off
) / BITS_PER_UNIT
);
2112 || len
< (ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
2113 - MAX (0, -pd
.offset
) / BITS_PER_UNIT
))
2115 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2116 fprintf (dump_file
, "Failed to encode %u "
2117 "partial definitions\n", ndefs
);
2121 else /* negative pd.rhs_off indicates we want to chop off first bits */
2123 if (-pd
.rhs_off
>= bufsize
)
2125 len
= native_encode_expr (pd
.rhs
,
2126 this_buffer
+ -pd
.rhs_off
/ BITS_PER_UNIT
,
2127 bufsize
- -pd
.rhs_off
/ BITS_PER_UNIT
,
2128 MAX (0, -pd
.offset
) / BITS_PER_UNIT
);
2130 || len
< (ROUND_UP (pd
.size
, BITS_PER_UNIT
) / BITS_PER_UNIT
2131 - MAX (0, -pd
.offset
) / BITS_PER_UNIT
))
2133 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2134 fprintf (dump_file
, "Failed to encode %u "
2135 "partial definitions\n", ndefs
);
2140 unsigned char *p
= buffer
;
2141 HOST_WIDE_INT size
= pd
.size
;
2143 size
-= ROUND_DOWN (-pd
.offset
, BITS_PER_UNIT
);
2144 this_buffer
[len
] = 0;
2145 if (BYTES_BIG_ENDIAN
)
2147 /* LSB of this_buffer[len - 1] byte should be at
2148 pd.offset + pd.size - 1 bits in buffer. */
2149 amnt
= ((unsigned HOST_WIDE_INT
) pd
.offset
2150 + pd
.size
) % BITS_PER_UNIT
;
2152 shift_bytes_in_array_right (this_buffer
, len
+ 1, amnt
);
2153 unsigned char *q
= this_buffer
;
2154 unsigned int off
= 0;
2158 off
= pd
.offset
/ BITS_PER_UNIT
;
2159 gcc_assert (off
< needed_len
);
2163 msk
= ((1 << size
) - 1) << (BITS_PER_UNIT
- amnt
);
2164 *p
= (*p
& ~msk
) | (this_buffer
[len
] & msk
);
2169 if (TREE_CODE (pd
.rhs
) != CONSTRUCTOR
)
2170 q
= (this_buffer
+ len
2171 - (ROUND_UP (size
- amnt
, BITS_PER_UNIT
)
2173 if (pd
.offset
% BITS_PER_UNIT
)
2175 msk
= -1U << (BITS_PER_UNIT
2176 - (pd
.offset
% BITS_PER_UNIT
));
2177 *p
= (*p
& msk
) | (*q
& ~msk
);
2181 size
-= BITS_PER_UNIT
- (pd
.offset
% BITS_PER_UNIT
);
2182 gcc_assert (size
>= 0);
2186 else if (TREE_CODE (pd
.rhs
) != CONSTRUCTOR
)
2188 q
= (this_buffer
+ len
2189 - (ROUND_UP (size
- amnt
, BITS_PER_UNIT
)
2191 if (pd
.offset
% BITS_PER_UNIT
)
2194 size
-= BITS_PER_UNIT
- ((unsigned HOST_WIDE_INT
) pd
.offset
2196 gcc_assert (size
>= 0);
2199 if ((unsigned HOST_WIDE_INT
) size
/ BITS_PER_UNIT
+ off
2201 size
= (needed_len
- off
) * BITS_PER_UNIT
;
2202 memcpy (p
, q
, size
/ BITS_PER_UNIT
);
2203 if (size
% BITS_PER_UNIT
)
2206 = -1U << (BITS_PER_UNIT
- (size
% BITS_PER_UNIT
));
2207 p
+= size
/ BITS_PER_UNIT
;
2208 q
+= size
/ BITS_PER_UNIT
;
2209 *p
= (*q
& msk
) | (*p
& ~msk
);
2216 /* LSB of this_buffer[0] byte should be at pd.offset bits
2219 size
= MIN (size
, (HOST_WIDE_INT
) needed_len
* BITS_PER_UNIT
);
2220 amnt
= pd
.offset
% BITS_PER_UNIT
;
2222 shift_bytes_in_array_left (this_buffer
, len
+ 1, amnt
);
2223 unsigned int off
= pd
.offset
/ BITS_PER_UNIT
;
2224 gcc_assert (off
< needed_len
);
2226 (HOST_WIDE_INT
) (needed_len
- off
) * BITS_PER_UNIT
);
2228 if (amnt
+ size
< BITS_PER_UNIT
)
2230 /* Low amnt bits come from *p, then size bits
2231 from this_buffer[0] and the remaining again from
2233 msk
= ((1 << size
) - 1) << amnt
;
2234 *p
= (*p
& ~msk
) | (this_buffer
[0] & msk
);
2240 *p
= (*p
& ~msk
) | (this_buffer
[0] & msk
);
2242 size
-= (BITS_PER_UNIT
- amnt
);
2247 amnt
= (unsigned HOST_WIDE_INT
) pd
.offset
% BITS_PER_UNIT
;
2249 size
-= BITS_PER_UNIT
- amnt
;
2250 size
= MIN (size
, (HOST_WIDE_INT
) needed_len
* BITS_PER_UNIT
);
2252 shift_bytes_in_array_left (this_buffer
, len
+ 1, amnt
);
2254 memcpy (p
, this_buffer
+ (amnt
!= 0), size
/ BITS_PER_UNIT
);
2255 p
+= size
/ BITS_PER_UNIT
;
2256 if (size
% BITS_PER_UNIT
)
2258 unsigned int msk
= -1U << (size
% BITS_PER_UNIT
);
2259 *p
= (this_buffer
[(amnt
!= 0) + size
/ BITS_PER_UNIT
]
2260 & ~msk
) | (*p
& msk
);
2265 tree type
= vr
->type
;
2266 /* Make sure to interpret in a type that has a range covering the whole
2268 if (INTEGRAL_TYPE_P (vr
->type
) && maxsizei
!= TYPE_PRECISION (vr
->type
))
2269 type
= build_nonstandard_integer_type (maxsizei
, TYPE_UNSIGNED (type
));
2271 if (BYTES_BIG_ENDIAN
)
2273 unsigned sz
= needed_len
;
2274 if (maxsizei
% BITS_PER_UNIT
)
2275 shift_bytes_in_array_right (buffer
, needed_len
,
2277 - (maxsizei
% BITS_PER_UNIT
));
2278 if (INTEGRAL_TYPE_P (type
))
2280 if (TYPE_MODE (type
) != BLKmode
)
2281 sz
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
2283 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
));
2285 if (sz
> needed_len
)
2287 memcpy (this_buffer
+ (sz
- needed_len
), buffer
, needed_len
);
2288 val
= native_interpret_expr (type
, this_buffer
, sz
);
2291 val
= native_interpret_expr (type
, buffer
, needed_len
);
2294 val
= native_interpret_expr (type
, buffer
, bufsize
);
2295 /* If we chop off bits because the types precision doesn't match the memory
2296 access size this is ok when optimizing reads but not when called from
2297 the DSE code during elimination. */
2298 if (val
&& type
!= vr
->type
)
2300 if (! int_fits_type_p (val
, vr
->type
))
2303 val
= fold_convert (vr
->type
, val
);
2308 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2310 "Successfully combined %u partial definitions\n", ndefs
);
2311 /* We are using the alias-set of the first store we encounter which
2312 should be appropriate here. */
2313 return finish (first_set
, first_base_set
, val
);
2317 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2319 "Failed to interpret %u encoded partial definitions\n", ndefs
);
2324 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2325 with the current VUSE and performs the expression lookup. */
2328 vn_reference_lookup_2 (ao_ref
*op
, tree vuse
, void *data_
)
2330 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
2331 vn_reference_t vr
= data
->vr
;
2332 vn_reference_s
**slot
;
2335 /* If we have partial definitions recorded we have to go through
2336 vn_reference_lookup_3. */
2337 if (!data
->partial_defs
.is_empty ())
2340 if (data
->last_vuse_ptr
)
2342 *data
->last_vuse_ptr
= vuse
;
2343 data
->last_vuse
= vuse
;
2346 /* Fixup vuse and hash. */
2348 vr
->hashcode
= vr
->hashcode
- SSA_NAME_VERSION (vr
->vuse
);
2349 vr
->vuse
= vuse_ssa_val (vuse
);
2351 vr
->hashcode
= vr
->hashcode
+ SSA_NAME_VERSION (vr
->vuse
);
2353 hash
= vr
->hashcode
;
2354 slot
= valid_info
->references
->find_slot_with_hash (vr
, hash
, NO_INSERT
);
2357 if ((*slot
)->result
&& data
->saved_operands
.exists ())
2358 return data
->finish (vr
->set
, vr
->base_set
, (*slot
)->result
);
2362 if (SSA_NAME_IS_DEFAULT_DEF (vuse
))
2364 HOST_WIDE_INT op_offset
, op_size
;
2366 tree base
= ao_ref_base (op
);
2369 && op
->offset
.is_constant (&op_offset
)
2370 && op
->size
.is_constant (&op_size
)
2371 && op
->max_size_known_p ()
2372 && known_eq (op
->size
, op
->max_size
))
2374 if (TREE_CODE (base
) == PARM_DECL
)
2375 v
= ipcp_get_aggregate_const (cfun
, base
, false, op_offset
,
2377 else if (TREE_CODE (base
) == MEM_REF
2378 && integer_zerop (TREE_OPERAND (base
, 1))
2379 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
2380 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base
, 0))
2381 && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base
, 0)))
2383 v
= ipcp_get_aggregate_const (cfun
,
2384 SSA_NAME_VAR (TREE_OPERAND (base
, 0)),
2385 true, op_offset
, op_size
);
2388 return data
->finish (vr
->set
, vr
->base_set
, v
);
2394 /* Lookup an existing or insert a new vn_reference entry into the
2395 value table for the VUSE, SET, TYPE, OPERANDS reference which
2396 has the value VALUE which is either a constant or an SSA name. */
2398 static vn_reference_t
2399 vn_reference_lookup_or_insert_for_pieces (tree vuse
,
2401 alias_set_type base_set
,
2403 poly_int64 max_size
,
2405 vec
<vn_reference_op_s
,
2410 vn_reference_t result
;
2412 vr1
.vuse
= vuse
? SSA_VAL (vuse
) : NULL_TREE
;
2413 vr1
.operands
= operands
;
2416 vr1
.base_set
= base_set
;
2417 vr1
.offset
= offset
;
2418 vr1
.max_size
= max_size
;
2419 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
2420 if (vn_reference_lookup_1 (&vr1
, &result
))
2423 if (TREE_CODE (value
) == SSA_NAME
)
2424 value_id
= VN_INFO (value
)->value_id
;
2426 value_id
= get_or_alloc_constant_value_id (value
);
2427 return vn_reference_insert_pieces (vuse
, set
, base_set
, offset
, max_size
,
2428 type
, operands
.copy (), value
, value_id
);
2431 /* Return a value-number for RCODE OPS... either by looking up an existing
2432 value-number for the possibly simplified result or by inserting the
2433 operation if INSERT is true. If SIMPLIFY is false, return a value
2434 number for the unsimplified expression. */
2437 vn_nary_build_or_lookup_1 (gimple_match_op
*res_op
, bool insert
,
2440 tree result
= NULL_TREE
;
2441 /* We will be creating a value number for
2443 So first simplify and lookup this expression to see if it
2444 is already available. */
2445 /* For simplification valueize. */
2448 for (i
= 0; i
< res_op
->num_ops
; ++i
)
2449 if (TREE_CODE (res_op
->ops
[i
]) == SSA_NAME
)
2451 tree tem
= vn_valueize (res_op
->ops
[i
]);
2454 res_op
->ops
[i
] = tem
;
2456 /* If valueization of an operand fails (it is not available), skip
2459 if (i
== res_op
->num_ops
)
2461 mprts_hook
= vn_lookup_simplify_result
;
2462 res
= res_op
->resimplify (NULL
, vn_valueize
);
2465 gimple
*new_stmt
= NULL
;
2467 && gimple_simplified_result_is_gimple_val (res_op
))
2469 /* The expression is already available. */
2470 result
= res_op
->ops
[0];
2471 /* Valueize it, simplification returns sth in AVAIL only. */
2472 if (TREE_CODE (result
) == SSA_NAME
)
2473 result
= SSA_VAL (result
);
2477 tree val
= vn_lookup_simplify_result (res_op
);
2480 gimple_seq stmts
= NULL
;
2481 result
= maybe_push_res_to_seq (res_op
, &stmts
);
2484 gcc_assert (gimple_seq_singleton_p (stmts
));
2485 new_stmt
= gimple_seq_first_stmt (stmts
);
2489 /* The expression is already available. */
2494 /* The expression is not yet available, value-number lhs to
2495 the new SSA_NAME we created. */
2496 /* Initialize value-number information properly. */
2497 vn_ssa_aux_t result_info
= VN_INFO (result
);
2498 result_info
->valnum
= result
;
2499 result_info
->value_id
= get_next_value_id ();
2500 result_info
->visited
= 1;
2501 gimple_seq_add_stmt_without_update (&VN_INFO (result
)->expr
,
2503 result_info
->needs_insertion
= true;
2504 /* ??? PRE phi-translation inserts NARYs without corresponding
2505 SSA name result. Re-use those but set their result according
2506 to the stmt we just built. */
2507 vn_nary_op_t nary
= NULL
;
2508 vn_nary_op_lookup_stmt (new_stmt
, &nary
);
2511 gcc_assert (! nary
->predicated_values
&& nary
->u
.result
== NULL_TREE
);
2512 nary
->u
.result
= gimple_assign_lhs (new_stmt
);
2514 /* As all "inserted" statements are singleton SCCs, insert
2515 to the valid table. This is strictly needed to
2516 avoid re-generating new value SSA_NAMEs for the same
2517 expression during SCC iteration over and over (the
2518 optimistic table gets cleared after each iteration).
2519 We do not need to insert into the optimistic table, as
2520 lookups there will fall back to the valid table. */
2523 unsigned int length
= vn_nary_length_from_stmt (new_stmt
);
2525 = alloc_vn_nary_op_noinit (length
, &vn_tables_insert_obstack
);
2526 vno1
->value_id
= result_info
->value_id
;
2527 vno1
->length
= length
;
2528 vno1
->predicated_values
= 0;
2529 vno1
->u
.result
= result
;
2530 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (new_stmt
));
2531 vn_nary_op_insert_into (vno1
, valid_info
->nary
);
2532 /* Also do not link it into the undo chain. */
2533 last_inserted_nary
= vno1
->next
;
2534 vno1
->next
= (vn_nary_op_t
)(void *)-1;
2536 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2538 fprintf (dump_file
, "Inserting name ");
2539 print_generic_expr (dump_file
, result
);
2540 fprintf (dump_file
, " for expression ");
2541 print_gimple_expr (dump_file
, new_stmt
, 0, TDF_SLIM
);
2542 fprintf (dump_file
, "\n");
2548 /* Return a value-number for RCODE OPS... either by looking up an existing
2549 value-number for the simplified result or by inserting the operation. */
2552 vn_nary_build_or_lookup (gimple_match_op
*res_op
)
2554 return vn_nary_build_or_lookup_1 (res_op
, true, true);
2557 /* Try to simplify the expression RCODE OPS... of type TYPE and return
2558 its value if present. */
2561 vn_nary_simplify (vn_nary_op_t nary
)
2563 if (nary
->length
> gimple_match_op::MAX_NUM_OPS
)
2565 gimple_match_op
op (gimple_match_cond::UNCOND
, nary
->opcode
,
2566 nary
->type
, nary
->length
);
2567 memcpy (op
.ops
, nary
->op
, sizeof (tree
) * nary
->length
);
2568 return vn_nary_build_or_lookup_1 (&op
, false, true);
2571 /* Elimination engine. */
2573 class eliminate_dom_walker
: public dom_walker
2576 eliminate_dom_walker (cdi_direction
, bitmap
);
2577 ~eliminate_dom_walker ();
2579 edge
before_dom_children (basic_block
) final override
;
2580 void after_dom_children (basic_block
) final override
;
2582 virtual tree
eliminate_avail (basic_block
, tree op
);
2583 virtual void eliminate_push_avail (basic_block
, tree op
);
2584 tree
eliminate_insert (basic_block
, gimple_stmt_iterator
*gsi
, tree val
);
2586 void eliminate_stmt (basic_block
, gimple_stmt_iterator
*);
2588 unsigned eliminate_cleanup (bool region_p
= false);
2591 unsigned int el_todo
;
2592 unsigned int eliminations
;
2593 unsigned int insertions
;
2595 /* SSA names that had their defs inserted by PRE if do_pre. */
2596 bitmap inserted_exprs
;
2598 /* Blocks with statements that have had their EH properties changed. */
2599 bitmap need_eh_cleanup
;
2601 /* Blocks with statements that have had their AB properties changed. */
2602 bitmap need_ab_cleanup
;
2604 /* Local state for the eliminate domwalk. */
2605 auto_vec
<gimple
*> to_remove
;
2606 auto_vec
<gimple
*> to_fixup
;
2607 auto_vec
<tree
> avail
;
2608 auto_vec
<tree
> avail_stack
;
2611 /* Adaptor to the elimination engine using RPO availability. */
2613 class rpo_elim
: public eliminate_dom_walker
2616 rpo_elim(basic_block entry_
)
2617 : eliminate_dom_walker (CDI_DOMINATORS
, NULL
), entry (entry_
),
2618 m_avail_freelist (NULL
) {}
2620 tree
eliminate_avail (basic_block
, tree op
) final override
;
2622 void eliminate_push_avail (basic_block
, tree
) final override
;
2625 /* Freelist of avail entries which are allocated from the vn_ssa_aux
2627 vn_avail
*m_avail_freelist
;
2630 /* Global RPO state for access from hooks. */
2631 static eliminate_dom_walker
*rpo_avail
;
2632 basic_block vn_context_bb
;
2634 /* Return true if BASE1 and BASE2 can be adjusted so they have the
2635 same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2636 Otherwise return false. */
2639 adjust_offsets_for_equal_base_address (tree base1
, poly_int64
*offset1
,
2640 tree base2
, poly_int64
*offset2
)
2643 if (TREE_CODE (base1
) == MEM_REF
2644 && TREE_CODE (base2
) == MEM_REF
)
2646 if (mem_ref_offset (base1
).to_shwi (&soff
))
2648 base1
= TREE_OPERAND (base1
, 0);
2649 *offset1
+= soff
* BITS_PER_UNIT
;
2651 if (mem_ref_offset (base2
).to_shwi (&soff
))
2653 base2
= TREE_OPERAND (base2
, 0);
2654 *offset2
+= soff
* BITS_PER_UNIT
;
2656 return operand_equal_p (base1
, base2
, 0);
2658 return operand_equal_p (base1
, base2
, OEP_ADDRESS_OF
);
2661 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2662 from the statement defining VUSE and if not successful tries to
2663 translate *REFP and VR_ through an aggregate copy at the definition
2664 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2665 of *REF and *VR. If only disambiguation was performed then
2666 *DISAMBIGUATE_ONLY is set to true. */
2669 vn_reference_lookup_3 (ao_ref
*ref
, tree vuse
, void *data_
,
2670 translate_flags
*disambiguate_only
)
2672 vn_walk_cb_data
*data
= (vn_walk_cb_data
*)data_
;
2673 vn_reference_t vr
= data
->vr
;
2674 gimple
*def_stmt
= SSA_NAME_DEF_STMT (vuse
);
2675 tree base
= ao_ref_base (ref
);
2676 HOST_WIDE_INT offseti
= 0, maxsizei
, sizei
= 0;
2677 static vec
<vn_reference_op_s
> lhs_ops
;
2679 bool lhs_ref_ok
= false;
2680 poly_int64 copy_size
;
2682 /* First try to disambiguate after value-replacing in the definitions LHS. */
2683 if (is_gimple_assign (def_stmt
))
2685 tree lhs
= gimple_assign_lhs (def_stmt
);
2686 bool valueized_anything
= false;
2687 /* Avoid re-allocation overhead. */
2688 lhs_ops
.truncate (0);
2689 basic_block saved_rpo_bb
= vn_context_bb
;
2690 vn_context_bb
= gimple_bb (def_stmt
);
2691 if (*disambiguate_only
<= TR_VALUEIZE_AND_DISAMBIGUATE
)
2693 copy_reference_ops_from_ref (lhs
, &lhs_ops
);
2694 valueize_refs_1 (&lhs_ops
, &valueized_anything
, true);
2696 vn_context_bb
= saved_rpo_bb
;
2697 ao_ref_init (&lhs_ref
, lhs
);
2699 if (valueized_anything
2700 && ao_ref_init_from_vn_reference
2701 (&lhs_ref
, ao_ref_alias_set (&lhs_ref
),
2702 ao_ref_base_alias_set (&lhs_ref
), TREE_TYPE (lhs
), lhs_ops
)
2703 && !refs_may_alias_p_1 (ref
, &lhs_ref
, data
->tbaa_p
))
2705 *disambiguate_only
= TR_VALUEIZE_AND_DISAMBIGUATE
;
2709 /* When the def is a CLOBBER we can optimistically disambiguate
2710 against it since any overlap it would be undefined behavior.
2711 Avoid this for obvious must aliases to save compile-time though.
2712 We also may not do this when the query is used for redundant
2714 if (!data
->redundant_store_removal_p
2715 && gimple_clobber_p (def_stmt
)
2716 && !operand_equal_p (ao_ref_base (&lhs_ref
), base
, OEP_ADDRESS_OF
))
2718 *disambiguate_only
= TR_DISAMBIGUATE
;
2722 /* Besides valueizing the LHS we can also use access-path based
2723 disambiguation on the original non-valueized ref. */
2726 && data
->orig_ref
.ref
)
2728 /* We want to use the non-valueized LHS for this, but avoid redundant
2730 ao_ref
*lref
= &lhs_ref
;
2732 if (valueized_anything
)
2734 ao_ref_init (&lref_alt
, lhs
);
2737 if (!refs_may_alias_p_1 (&data
->orig_ref
, lref
, data
->tbaa_p
))
2739 *disambiguate_only
= (valueized_anything
2740 ? TR_VALUEIZE_AND_DISAMBIGUATE
2746 /* If we reach a clobbering statement try to skip it and see if
2747 we find a VN result with exactly the same value as the
2748 possible clobber. In this case we can ignore the clobber
2749 and return the found value. */
2750 if (is_gimple_reg_type (TREE_TYPE (lhs
))
2751 && types_compatible_p (TREE_TYPE (lhs
), vr
->type
)
2752 && (ref
->ref
|| data
->orig_ref
.ref
)
2754 && data
->partial_defs
.is_empty ()
2755 && multiple_p (get_object_alignment
2756 (ref
->ref
? ref
->ref
: data
->orig_ref
.ref
),
2758 && multiple_p (get_object_alignment (lhs
), ref
->size
))
2760 tree rhs
= gimple_assign_rhs1 (def_stmt
);
2761 /* ??? We may not compare to ahead values which might be from
2762 a different loop iteration but only to loop invariants. Use
2763 CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2764 The one-hop lookup below doesn't have this issue since there's
2765 a virtual PHI before we ever reach a backedge to cross.
2766 We can skip multiple defs as long as they are from the same
2769 && !operand_equal_p (data
->same_val
, rhs
))
2771 else if (CONSTANT_CLASS_P (rhs
))
2773 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2776 "Skipping possible redundant definition ");
2777 print_gimple_stmt (dump_file
, def_stmt
, 0);
2779 /* Delay the actual compare of the values to the end of the walk
2780 but do not update last_vuse from here. */
2781 data
->last_vuse_ptr
= NULL
;
2782 data
->same_val
= rhs
;
2787 tree saved_vuse
= vr
->vuse
;
2788 hashval_t saved_hashcode
= vr
->hashcode
;
2790 vr
->hashcode
= vr
->hashcode
- SSA_NAME_VERSION (vr
->vuse
);
2791 vr
->vuse
= vuse_ssa_val (gimple_vuse (def_stmt
));
2793 vr
->hashcode
= vr
->hashcode
+ SSA_NAME_VERSION (vr
->vuse
);
2794 vn_reference_t vnresult
= NULL
;
2795 /* Do not use vn_reference_lookup_2 since that might perform
2796 expression hashtable insertion but this lookup crosses
2797 a possible may-alias making such insertion conditionally
2799 vn_reference_lookup_1 (vr
, &vnresult
);
2800 /* Need to restore vr->vuse and vr->hashcode. */
2801 vr
->vuse
= saved_vuse
;
2802 vr
->hashcode
= saved_hashcode
;
2805 if (TREE_CODE (rhs
) == SSA_NAME
)
2806 rhs
= SSA_VAL (rhs
);
2807 if (vnresult
->result
2808 && operand_equal_p (vnresult
->result
, rhs
, 0))
2814 else if (*disambiguate_only
<= TR_VALUEIZE_AND_DISAMBIGUATE
2815 && gimple_call_builtin_p (def_stmt
, BUILT_IN_NORMAL
)
2816 && gimple_call_num_args (def_stmt
) <= 4)
2818 /* For builtin calls valueize its arguments and call the
2819 alias oracle again. Valueization may improve points-to
2820 info of pointers and constify size and position arguments.
2821 Originally this was motivated by PR61034 which has
2822 conditional calls to free falsely clobbering ref because
2823 of imprecise points-to info of the argument. */
2825 bool valueized_anything
= false;
2826 for (unsigned i
= 0; i
< gimple_call_num_args (def_stmt
); ++i
)
2828 oldargs
[i
] = gimple_call_arg (def_stmt
, i
);
2829 tree val
= vn_valueize (oldargs
[i
]);
2830 if (val
!= oldargs
[i
])
2832 gimple_call_set_arg (def_stmt
, i
, val
);
2833 valueized_anything
= true;
2836 if (valueized_anything
)
2838 bool res
= call_may_clobber_ref_p_1 (as_a
<gcall
*> (def_stmt
),
2840 for (unsigned i
= 0; i
< gimple_call_num_args (def_stmt
); ++i
)
2841 gimple_call_set_arg (def_stmt
, i
, oldargs
[i
]);
2844 *disambiguate_only
= TR_VALUEIZE_AND_DISAMBIGUATE
;
2850 if (*disambiguate_only
> TR_TRANSLATE
)
2853 /* If we cannot constrain the size of the reference we cannot
2854 test if anything kills it. */
2855 if (!ref
->max_size_known_p ())
2858 poly_int64 offset
= ref
->offset
;
2859 poly_int64 maxsize
= ref
->max_size
;
2861 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2862 from that definition.
2864 if (is_gimple_reg_type (vr
->type
)
2865 && (gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMSET
)
2866 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMSET_CHK
))
2867 && (integer_zerop (gimple_call_arg (def_stmt
, 1))
2868 || ((TREE_CODE (gimple_call_arg (def_stmt
, 1)) == INTEGER_CST
2869 || (INTEGRAL_TYPE_P (vr
->type
) && known_eq (ref
->size
, 8)))
2871 && BITS_PER_UNIT
== 8
2872 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
2873 && offset
.is_constant (&offseti
)
2874 && ref
->size
.is_constant (&sizei
)
2875 && (offseti
% BITS_PER_UNIT
== 0
2876 || TREE_CODE (gimple_call_arg (def_stmt
, 1)) == INTEGER_CST
)))
2877 && (poly_int_tree_p (gimple_call_arg (def_stmt
, 2))
2878 || (TREE_CODE (gimple_call_arg (def_stmt
, 2)) == SSA_NAME
2879 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt
, 2)))))
2880 && (TREE_CODE (gimple_call_arg (def_stmt
, 0)) == ADDR_EXPR
2881 || TREE_CODE (gimple_call_arg (def_stmt
, 0)) == SSA_NAME
))
2884 poly_int64 offset2
, size2
, maxsize2
;
2886 tree ref2
= gimple_call_arg (def_stmt
, 0);
2887 if (TREE_CODE (ref2
) == SSA_NAME
)
2889 ref2
= SSA_VAL (ref2
);
2890 if (TREE_CODE (ref2
) == SSA_NAME
2891 && (TREE_CODE (base
) != MEM_REF
2892 || TREE_OPERAND (base
, 0) != ref2
))
2894 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref2
);
2895 if (gimple_assign_single_p (def_stmt
)
2896 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
2897 ref2
= gimple_assign_rhs1 (def_stmt
);
2900 if (TREE_CODE (ref2
) == ADDR_EXPR
)
2902 ref2
= TREE_OPERAND (ref2
, 0);
2903 base2
= get_ref_base_and_extent (ref2
, &offset2
, &size2
, &maxsize2
,
2905 if (!known_size_p (maxsize2
)
2906 || !known_eq (maxsize2
, size2
)
2907 || !operand_equal_p (base
, base2
, OEP_ADDRESS_OF
))
2910 else if (TREE_CODE (ref2
) == SSA_NAME
)
2913 if (TREE_CODE (base
) != MEM_REF
2914 || !(mem_ref_offset (base
)
2915 << LOG2_BITS_PER_UNIT
).to_shwi (&soff
))
2919 if (TREE_OPERAND (base
, 0) != ref2
)
2921 gimple
*def
= SSA_NAME_DEF_STMT (ref2
);
2922 if (is_gimple_assign (def
)
2923 && gimple_assign_rhs_code (def
) == POINTER_PLUS_EXPR
2924 && gimple_assign_rhs1 (def
) == TREE_OPERAND (base
, 0)
2925 && poly_int_tree_p (gimple_assign_rhs2 (def
)))
2927 tree rhs2
= gimple_assign_rhs2 (def
);
2928 if (!(poly_offset_int::from (wi::to_poly_wide (rhs2
),
2930 << LOG2_BITS_PER_UNIT
).to_shwi (&offset2
))
2932 ref2
= gimple_assign_rhs1 (def
);
2933 if (TREE_CODE (ref2
) == SSA_NAME
)
2934 ref2
= SSA_VAL (ref2
);
2942 tree len
= gimple_call_arg (def_stmt
, 2);
2943 HOST_WIDE_INT leni
, offset2i
;
2944 if (TREE_CODE (len
) == SSA_NAME
)
2945 len
= SSA_VAL (len
);
2946 /* Sometimes the above trickery is smarter than alias analysis. Take
2947 advantage of that. */
2948 if (!ranges_maybe_overlap_p (offset
, maxsize
, offset2
,
2949 (wi::to_poly_offset (len
)
2950 << LOG2_BITS_PER_UNIT
)))
2952 if (data
->partial_defs
.is_empty ()
2953 && known_subrange_p (offset
, maxsize
, offset2
,
2954 wi::to_poly_offset (len
) << LOG2_BITS_PER_UNIT
))
2957 if (integer_zerop (gimple_call_arg (def_stmt
, 1)))
2958 val
= build_zero_cst (vr
->type
);
2959 else if (INTEGRAL_TYPE_P (vr
->type
)
2960 && known_eq (ref
->size
, 8)
2961 && offseti
% BITS_PER_UNIT
== 0)
2963 gimple_match_op
res_op (gimple_match_cond::UNCOND
, NOP_EXPR
,
2964 vr
->type
, gimple_call_arg (def_stmt
, 1));
2965 val
= vn_nary_build_or_lookup (&res_op
);
2967 || (TREE_CODE (val
) == SSA_NAME
2968 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
)))
2974 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr
->type
)) + 1;
2975 if (INTEGRAL_TYPE_P (vr
->type
)
2976 && TYPE_MODE (vr
->type
) != BLKmode
)
2977 buflen
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr
->type
)) + 1;
2978 unsigned char *buf
= XALLOCAVEC (unsigned char, buflen
);
2979 memset (buf
, TREE_INT_CST_LOW (gimple_call_arg (def_stmt
, 1)),
2981 if (BYTES_BIG_ENDIAN
)
2984 = (((unsigned HOST_WIDE_INT
) offseti
+ sizei
)
2988 shift_bytes_in_array_right (buf
, buflen
,
2989 BITS_PER_UNIT
- amnt
);
2994 else if (offseti
% BITS_PER_UNIT
!= 0)
2997 = BITS_PER_UNIT
- ((unsigned HOST_WIDE_INT
) offseti
2999 shift_bytes_in_array_left (buf
, buflen
, amnt
);
3003 val
= native_interpret_expr (vr
->type
, buf
, buflen
);
3007 return data
->finish (0, 0, val
);
3009 /* For now handle clearing memory with partial defs. */
3010 else if (known_eq (ref
->size
, maxsize
)
3011 && integer_zerop (gimple_call_arg (def_stmt
, 1))
3012 && tree_fits_poly_int64_p (len
)
3013 && tree_to_poly_int64 (len
).is_constant (&leni
)
3014 && leni
<= INTTYPE_MAXIMUM (HOST_WIDE_INT
) / BITS_PER_UNIT
3015 && offset
.is_constant (&offseti
)
3016 && offset2
.is_constant (&offset2i
)
3017 && maxsize
.is_constant (&maxsizei
)
3018 && ranges_known_overlap_p (offseti
, maxsizei
, offset2i
,
3019 leni
<< LOG2_BITS_PER_UNIT
))
3022 pd
.rhs
= build_constructor (NULL_TREE
, NULL
);
3024 pd
.offset
= offset2i
;
3025 pd
.size
= leni
<< LOG2_BITS_PER_UNIT
;
3026 return data
->push_partial_def (pd
, 0, 0, offseti
, maxsizei
);
3030 /* 2) Assignment from an empty CONSTRUCTOR. */
3031 else if (is_gimple_reg_type (vr
->type
)
3032 && gimple_assign_single_p (def_stmt
)
3033 && gimple_assign_rhs_code (def_stmt
) == CONSTRUCTOR
3034 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt
)) == 0)
3037 poly_int64 offset2
, size2
, maxsize2
;
3038 HOST_WIDE_INT offset2i
, size2i
;
3039 gcc_assert (lhs_ref_ok
);
3040 base2
= ao_ref_base (&lhs_ref
);
3041 offset2
= lhs_ref
.offset
;
3042 size2
= lhs_ref
.size
;
3043 maxsize2
= lhs_ref
.max_size
;
3044 if (known_size_p (maxsize2
)
3045 && known_eq (maxsize2
, size2
)
3046 && adjust_offsets_for_equal_base_address (base
, &offset
,
3049 if (data
->partial_defs
.is_empty ()
3050 && known_subrange_p (offset
, maxsize
, offset2
, size2
))
3052 /* While technically undefined behavior do not optimize
3053 a full read from a clobber. */
3054 if (gimple_clobber_p (def_stmt
))
3056 tree val
= build_zero_cst (vr
->type
);
3057 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3058 ao_ref_base_alias_set (&lhs_ref
), val
);
3060 else if (known_eq (ref
->size
, maxsize
)
3061 && maxsize
.is_constant (&maxsizei
)
3062 && offset
.is_constant (&offseti
)
3063 && offset2
.is_constant (&offset2i
)
3064 && size2
.is_constant (&size2i
)
3065 && ranges_known_overlap_p (offseti
, maxsizei
,
3068 /* Let clobbers be consumed by the partial-def tracker
3069 which can choose to ignore them if they are shadowed
3072 pd
.rhs
= gimple_assign_rhs1 (def_stmt
);
3074 pd
.offset
= offset2i
;
3076 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3077 ao_ref_base_alias_set (&lhs_ref
),
3083 /* 3) Assignment from a constant. We can use folds native encode/interpret
3084 routines to extract the assigned bits. */
3085 else if (known_eq (ref
->size
, maxsize
)
3086 && is_gimple_reg_type (vr
->type
)
3087 && !reverse_storage_order_for_component_p (vr
->operands
)
3088 && !contains_storage_order_barrier_p (vr
->operands
)
3089 && gimple_assign_single_p (def_stmt
)
3091 && BITS_PER_UNIT
== 8
3092 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
3093 /* native_encode and native_decode operate on arrays of bytes
3094 and so fundamentally need a compile-time size and offset. */
3095 && maxsize
.is_constant (&maxsizei
)
3096 && offset
.is_constant (&offseti
)
3097 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt
))
3098 || (TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
3099 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt
))))))
3101 tree lhs
= gimple_assign_lhs (def_stmt
);
3103 poly_int64 offset2
, size2
, maxsize2
;
3104 HOST_WIDE_INT offset2i
, size2i
;
3106 gcc_assert (lhs_ref_ok
);
3107 base2
= ao_ref_base (&lhs_ref
);
3108 offset2
= lhs_ref
.offset
;
3109 size2
= lhs_ref
.size
;
3110 maxsize2
= lhs_ref
.max_size
;
3111 reverse
= reverse_storage_order_for_component_p (lhs
);
3114 && !storage_order_barrier_p (lhs
)
3115 && known_eq (maxsize2
, size2
)
3116 && adjust_offsets_for_equal_base_address (base
, &offset
,
3118 && offset
.is_constant (&offseti
)
3119 && offset2
.is_constant (&offset2i
)
3120 && size2
.is_constant (&size2i
))
3122 if (data
->partial_defs
.is_empty ()
3123 && known_subrange_p (offseti
, maxsizei
, offset2
, size2
))
3125 /* We support up to 512-bit values (for V8DFmode). */
3126 unsigned char buffer
[65];
3129 tree rhs
= gimple_assign_rhs1 (def_stmt
);
3130 if (TREE_CODE (rhs
) == SSA_NAME
)
3131 rhs
= SSA_VAL (rhs
);
3132 len
= native_encode_expr (rhs
,
3133 buffer
, sizeof (buffer
) - 1,
3134 (offseti
- offset2i
) / BITS_PER_UNIT
);
3135 if (len
> 0 && len
* BITS_PER_UNIT
>= maxsizei
)
3137 tree type
= vr
->type
;
3138 unsigned char *buf
= buffer
;
3139 unsigned int amnt
= 0;
3140 /* Make sure to interpret in a type that has a range
3141 covering the whole access size. */
3142 if (INTEGRAL_TYPE_P (vr
->type
)
3143 && maxsizei
!= TYPE_PRECISION (vr
->type
))
3144 type
= build_nonstandard_integer_type (maxsizei
,
3145 TYPE_UNSIGNED (type
));
3146 if (BYTES_BIG_ENDIAN
)
3148 /* For big-endian native_encode_expr stored the rhs
3149 such that the LSB of it is the LSB of buffer[len - 1].
3150 That bit is stored into memory at position
3151 offset2 + size2 - 1, i.e. in byte
3152 base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3153 E.g. for offset2 1 and size2 14, rhs -1 and memory
3154 previously cleared that is:
3157 Now, if we want to extract offset 2 and size 12 from
3158 it using native_interpret_expr (which actually works
3159 for integral bitfield types in terms of byte size of
3160 the mode), the native_encode_expr stored the value
3163 and returned len 2 (the X bits are outside of
3165 Let sz be maxsize / BITS_PER_UNIT if not extracting
3166 a bitfield, and GET_MODE_SIZE otherwise.
3167 We need to align the LSB of the value we want to
3168 extract as the LSB of buf[sz - 1].
3169 The LSB from memory we need to read is at position
3170 offset + maxsize - 1. */
3171 HOST_WIDE_INT sz
= maxsizei
/ BITS_PER_UNIT
;
3172 if (INTEGRAL_TYPE_P (type
))
3174 if (TYPE_MODE (type
) != BLKmode
)
3175 sz
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
3177 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
));
3179 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
+ size2i
3180 - offseti
- maxsizei
) % BITS_PER_UNIT
;
3182 shift_bytes_in_array_right (buffer
, len
, amnt
);
3183 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
+ size2i
3184 - offseti
- maxsizei
- amnt
) / BITS_PER_UNIT
;
3185 if ((unsigned HOST_WIDE_INT
) sz
+ amnt
> (unsigned) len
)
3189 buf
= buffer
+ len
- sz
- amnt
;
3190 len
-= (buf
- buffer
);
3195 amnt
= ((unsigned HOST_WIDE_INT
) offset2i
3196 - offseti
) % BITS_PER_UNIT
;
3200 shift_bytes_in_array_left (buffer
, len
+ 1, amnt
);
3204 tree val
= native_interpret_expr (type
, buf
, len
);
3205 /* If we chop off bits because the types precision doesn't
3206 match the memory access size this is ok when optimizing
3207 reads but not when called from the DSE code during
3210 && type
!= vr
->type
)
3212 if (! int_fits_type_p (val
, vr
->type
))
3215 val
= fold_convert (vr
->type
, val
);
3219 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3220 ao_ref_base_alias_set (&lhs_ref
), val
);
3223 else if (ranges_known_overlap_p (offseti
, maxsizei
, offset2i
,
3227 tree rhs
= gimple_assign_rhs1 (def_stmt
);
3228 if (TREE_CODE (rhs
) == SSA_NAME
)
3229 rhs
= SSA_VAL (rhs
);
3232 pd
.offset
= offset2i
;
3234 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3235 ao_ref_base_alias_set (&lhs_ref
),
3241 /* 4) Assignment from an SSA name which definition we may be able
3242 to access pieces from or we can combine to a larger entity. */
3243 else if (known_eq (ref
->size
, maxsize
)
3244 && is_gimple_reg_type (vr
->type
)
3245 && !reverse_storage_order_for_component_p (vr
->operands
)
3246 && !contains_storage_order_barrier_p (vr
->operands
)
3247 && gimple_assign_single_p (def_stmt
)
3248 && TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
)
3250 tree lhs
= gimple_assign_lhs (def_stmt
);
3252 poly_int64 offset2
, size2
, maxsize2
;
3253 HOST_WIDE_INT offset2i
, size2i
, offseti
;
3255 gcc_assert (lhs_ref_ok
);
3256 base2
= ao_ref_base (&lhs_ref
);
3257 offset2
= lhs_ref
.offset
;
3258 size2
= lhs_ref
.size
;
3259 maxsize2
= lhs_ref
.max_size
;
3260 reverse
= reverse_storage_order_for_component_p (lhs
);
3261 tree def_rhs
= gimple_assign_rhs1 (def_stmt
);
3263 && !storage_order_barrier_p (lhs
)
3264 && known_size_p (maxsize2
)
3265 && known_eq (maxsize2
, size2
)
3266 && adjust_offsets_for_equal_base_address (base
, &offset
,
3269 if (data
->partial_defs
.is_empty ()
3270 && known_subrange_p (offset
, maxsize
, offset2
, size2
)
3271 /* ??? We can't handle bitfield precision extracts without
3272 either using an alternate type for the BIT_FIELD_REF and
3273 then doing a conversion or possibly adjusting the offset
3274 according to endianness. */
3275 && (! INTEGRAL_TYPE_P (vr
->type
)
3276 || known_eq (ref
->size
, TYPE_PRECISION (vr
->type
)))
3277 && multiple_p (ref
->size
, BITS_PER_UNIT
))
3279 tree val
= NULL_TREE
;
3280 if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs
))
3281 || type_has_mode_precision_p (TREE_TYPE (def_rhs
)))
3283 gimple_match_op
op (gimple_match_cond::UNCOND
,
3284 BIT_FIELD_REF
, vr
->type
,
3286 bitsize_int (ref
->size
),
3287 bitsize_int (offset
- offset2
));
3288 val
= vn_nary_build_or_lookup (&op
);
3290 else if (known_eq (ref
->size
, size2
))
3292 gimple_match_op
op (gimple_match_cond::UNCOND
,
3293 VIEW_CONVERT_EXPR
, vr
->type
,
3295 val
= vn_nary_build_or_lookup (&op
);
3298 && (TREE_CODE (val
) != SSA_NAME
3299 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val
)))
3300 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3301 ao_ref_base_alias_set (&lhs_ref
), val
);
3303 else if (maxsize
.is_constant (&maxsizei
)
3304 && offset
.is_constant (&offseti
)
3305 && offset2
.is_constant (&offset2i
)
3306 && size2
.is_constant (&size2i
)
3307 && ranges_known_overlap_p (offset
, maxsize
, offset2
, size2
))
3310 pd
.rhs
= SSA_VAL (def_rhs
);
3312 pd
.offset
= offset2i
;
3314 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3315 ao_ref_base_alias_set (&lhs_ref
),
3321 /* 4b) Assignment done via one of the vectorizer internal store
3322 functions where we may be able to access pieces from or we can
3323 combine to a larger entity. */
3324 else if (known_eq (ref
->size
, maxsize
)
3325 && is_gimple_reg_type (vr
->type
)
3326 && !reverse_storage_order_for_component_p (vr
->operands
)
3327 && !contains_storage_order_barrier_p (vr
->operands
)
3328 && is_gimple_call (def_stmt
)
3329 && gimple_call_internal_p (def_stmt
)
3330 && internal_store_fn_p (gimple_call_internal_fn (def_stmt
)))
3332 gcall
*call
= as_a
<gcall
*> (def_stmt
);
3333 internal_fn fn
= gimple_call_internal_fn (call
);
3335 tree mask
= NULL_TREE
, len
= NULL_TREE
, bias
= NULL_TREE
;
3338 case IFN_MASK_STORE
:
3339 mask
= gimple_call_arg (call
, internal_fn_mask_index (fn
));
3340 mask
= vn_valueize (mask
);
3341 if (TREE_CODE (mask
) != VECTOR_CST
)
3346 int len_index
= internal_fn_len_index (fn
);
3347 len
= gimple_call_arg (call
, len_index
);
3348 bias
= gimple_call_arg (call
, len_index
+ 1);
3349 if (!tree_fits_uhwi_p (len
) || !tree_fits_shwi_p (bias
))
3356 tree def_rhs
= gimple_call_arg (call
,
3357 internal_fn_stored_value_index (fn
));
3358 def_rhs
= vn_valueize (def_rhs
);
3359 if (TREE_CODE (def_rhs
) != VECTOR_CST
)
3362 ao_ref_init_from_ptr_and_size (&lhs_ref
,
3363 vn_valueize (gimple_call_arg (call
, 0)),
3364 TYPE_SIZE_UNIT (TREE_TYPE (def_rhs
)));
3366 poly_int64 offset2
, size2
, maxsize2
;
3367 HOST_WIDE_INT offset2i
, size2i
, offseti
;
3368 base2
= ao_ref_base (&lhs_ref
);
3369 offset2
= lhs_ref
.offset
;
3370 size2
= lhs_ref
.size
;
3371 maxsize2
= lhs_ref
.max_size
;
3372 if (known_size_p (maxsize2
)
3373 && known_eq (maxsize2
, size2
)
3374 && adjust_offsets_for_equal_base_address (base
, &offset
,
3376 && maxsize
.is_constant (&maxsizei
)
3377 && offset
.is_constant (&offseti
)
3378 && offset2
.is_constant (&offset2i
)
3379 && size2
.is_constant (&size2i
))
3381 if (!ranges_maybe_overlap_p (offset
, maxsize
, offset2
, size2
))
3382 /* Poor-mans disambiguation. */
3384 else if (ranges_known_overlap_p (offset
, maxsize
, offset2
, size2
))
3388 tree aa
= gimple_call_arg (call
, 1);
3389 alias_set_type set
= get_deref_alias_set (TREE_TYPE (aa
));
3390 tree vectype
= TREE_TYPE (def_rhs
);
3391 unsigned HOST_WIDE_INT elsz
3392 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype
)));
3395 HOST_WIDE_INT start
= 0, length
= 0;
3396 unsigned mask_idx
= 0;
3399 if (integer_zerop (VECTOR_CST_ELT (mask
, mask_idx
)))
3404 pd
.offset
= offset2i
+ start
;
3406 if (ranges_known_overlap_p
3407 (offset
, maxsize
, pd
.offset
, pd
.size
))
3409 void *res
= data
->push_partial_def
3410 (pd
, set
, set
, offseti
, maxsizei
);
3415 start
= (mask_idx
+ 1) * elsz
;
3422 while (known_lt (mask_idx
, TYPE_VECTOR_SUBPARTS (vectype
)));
3426 pd
.offset
= offset2i
+ start
;
3428 if (ranges_known_overlap_p (offset
, maxsize
,
3429 pd
.offset
, pd
.size
))
3430 return data
->push_partial_def (pd
, set
, set
,
3434 else if (fn
== IFN_LEN_STORE
)
3436 pd
.offset
= offset2i
;
3437 pd
.size
= (tree_to_uhwi (len
)
3438 + -tree_to_shwi (bias
)) * BITS_PER_UNIT
;
3439 if (BYTES_BIG_ENDIAN
)
3440 pd
.rhs_off
= pd
.size
- tree_to_uhwi (TYPE_SIZE (vectype
));
3443 if (ranges_known_overlap_p (offset
, maxsize
,
3444 pd
.offset
, pd
.size
))
3445 return data
->push_partial_def (pd
, set
, set
,
3455 /* 5) For aggregate copies translate the reference through them if
3456 the copy kills ref. */
3457 else if (data
->vn_walk_kind
== VN_WALKREWRITE
3458 && gimple_assign_single_p (def_stmt
)
3459 && (DECL_P (gimple_assign_rhs1 (def_stmt
))
3460 || TREE_CODE (gimple_assign_rhs1 (def_stmt
)) == MEM_REF
3461 || handled_component_p (gimple_assign_rhs1 (def_stmt
))))
3465 auto_vec
<vn_reference_op_s
> rhs
;
3466 vn_reference_op_t vro
;
3469 gcc_assert (lhs_ref_ok
);
3471 /* See if the assignment kills REF. */
3472 base2
= ao_ref_base (&lhs_ref
);
3473 if (!lhs_ref
.max_size_known_p ()
3475 && (TREE_CODE (base
) != MEM_REF
3476 || TREE_CODE (base2
) != MEM_REF
3477 || TREE_OPERAND (base
, 0) != TREE_OPERAND (base2
, 0)
3478 || !tree_int_cst_equal (TREE_OPERAND (base
, 1),
3479 TREE_OPERAND (base2
, 1))))
3480 || !stmt_kills_ref_p (def_stmt
, ref
))
3483 /* Find the common base of ref and the lhs. lhs_ops already
3484 contains valueized operands for the lhs. */
3485 i
= vr
->operands
.length () - 1;
3486 j
= lhs_ops
.length () - 1;
3487 while (j
>= 0 && i
>= 0
3488 && vn_reference_op_eq (&vr
->operands
[i
], &lhs_ops
[j
]))
3494 /* ??? The innermost op should always be a MEM_REF and we already
3495 checked that the assignment to the lhs kills vr. Thus for
3496 aggregate copies using char[] types the vn_reference_op_eq
3497 may fail when comparing types for compatibility. But we really
3498 don't care here - further lookups with the rewritten operands
3499 will simply fail if we messed up types too badly. */
3500 poly_int64 extra_off
= 0;
3501 if (j
== 0 && i
>= 0
3502 && lhs_ops
[0].opcode
== MEM_REF
3503 && maybe_ne (lhs_ops
[0].off
, -1))
3505 if (known_eq (lhs_ops
[0].off
, vr
->operands
[i
].off
))
3507 else if (vr
->operands
[i
].opcode
== MEM_REF
3508 && maybe_ne (vr
->operands
[i
].off
, -1))
3510 extra_off
= vr
->operands
[i
].off
- lhs_ops
[0].off
;
3515 /* i now points to the first additional op.
3516 ??? LHS may not be completely contained in VR, one or more
3517 VIEW_CONVERT_EXPRs could be in its way. We could at least
3518 try handling outermost VIEW_CONVERT_EXPRs. */
3522 /* Punt if the additional ops contain a storage order barrier. */
3523 for (k
= i
; k
>= 0; k
--)
3525 vro
= &vr
->operands
[k
];
3526 if (vro
->opcode
== VIEW_CONVERT_EXPR
&& vro
->reverse
)
3530 /* Now re-write REF to be based on the rhs of the assignment. */
3531 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
3532 copy_reference_ops_from_ref (rhs1
, &rhs
);
3534 /* Apply an extra offset to the inner MEM_REF of the RHS. */
3535 bool force_no_tbaa
= false;
3536 if (maybe_ne (extra_off
, 0))
3538 if (rhs
.length () < 2)
3540 int ix
= rhs
.length () - 2;
3541 if (rhs
[ix
].opcode
!= MEM_REF
3542 || known_eq (rhs
[ix
].off
, -1))
3544 rhs
[ix
].off
+= extra_off
;
3545 rhs
[ix
].op0
= int_const_binop (PLUS_EXPR
, rhs
[ix
].op0
,
3546 build_int_cst (TREE_TYPE (rhs
[ix
].op0
),
3548 /* When we have offsetted the RHS, reading only parts of it,
3549 we can no longer use the original TBAA type, force alias-set
3551 force_no_tbaa
= true;
3554 /* Save the operands since we need to use the original ones for
3555 the hash entry we use. */
3556 if (!data
->saved_operands
.exists ())
3557 data
->saved_operands
= vr
->operands
.copy ();
3559 /* We need to pre-pend vr->operands[0..i] to rhs. */
3560 vec
<vn_reference_op_s
> old
= vr
->operands
;
3561 if (i
+ 1 + rhs
.length () > vr
->operands
.length ())
3562 vr
->operands
.safe_grow (i
+ 1 + rhs
.length (), true);
3564 vr
->operands
.truncate (i
+ 1 + rhs
.length ());
3565 FOR_EACH_VEC_ELT (rhs
, j
, vro
)
3566 vr
->operands
[i
+ 1 + j
] = *vro
;
3567 valueize_refs (&vr
->operands
);
3568 if (old
== shared_lookup_references
)
3569 shared_lookup_references
= vr
->operands
;
3570 vr
->hashcode
= vn_reference_compute_hash (vr
);
3572 /* Try folding the new reference to a constant. */
3573 tree val
= fully_constant_vn_reference_p (vr
);
3576 if (data
->partial_defs
.is_empty ())
3577 return data
->finish (ao_ref_alias_set (&lhs_ref
),
3578 ao_ref_base_alias_set (&lhs_ref
), val
);
3579 /* This is the only interesting case for partial-def handling
3580 coming from targets that like to gimplify init-ctors as
3581 aggregate copies from constant data like aarch64 for
3583 if (maxsize
.is_constant (&maxsizei
) && known_eq (ref
->size
, maxsize
))
3590 return data
->push_partial_def (pd
, ao_ref_alias_set (&lhs_ref
),
3591 ao_ref_base_alias_set (&lhs_ref
),
3596 /* Continuing with partial defs isn't easily possible here, we
3597 have to find a full def from further lookups from here. Probably
3598 not worth the special-casing everywhere. */
3599 if (!data
->partial_defs
.is_empty ())
3602 /* Adjust *ref from the new operands. */
3604 ao_ref_init (&rhs1_ref
, rhs1
);
3605 if (!ao_ref_init_from_vn_reference (&r
,
3607 : ao_ref_alias_set (&rhs1_ref
),
3609 : ao_ref_base_alias_set (&rhs1_ref
),
3610 vr
->type
, vr
->operands
))
3612 /* This can happen with bitfields. */
3613 if (maybe_ne (ref
->size
, r
.size
))
3615 /* If the access lacks some subsetting simply apply that by
3616 shortening it. That in the end can only be successful
3617 if we can pun the lookup result which in turn requires
3619 if (known_eq (r
.size
, r
.max_size
)
3620 && known_lt (ref
->size
, r
.size
))
3621 r
.size
= r
.max_size
= ref
->size
;
3626 vr
->offset
= r
.offset
;
3627 vr
->max_size
= r
.max_size
;
3629 /* Do not update last seen VUSE after translating. */
3630 data
->last_vuse_ptr
= NULL
;
3631 /* Invalidate the original access path since it now contains
3633 data
->orig_ref
.ref
= NULL_TREE
;
3634 /* Use the alias-set of this LHS for recording an eventual result. */
3635 if (data
->first_set
== -2)
3637 data
->first_set
= ao_ref_alias_set (&lhs_ref
);
3638 data
->first_base_set
= ao_ref_base_alias_set (&lhs_ref
);
3641 /* Keep looking for the adjusted *REF / VR pair. */
3645 /* 6) For memcpy copies translate the reference through them if the copy
3646 kills ref. But we cannot (easily) do this translation if the memcpy is
3647 a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3648 can modify the storage order of objects (see storage_order_barrier_p). */
3649 else if (data
->vn_walk_kind
== VN_WALKREWRITE
3650 && is_gimple_reg_type (vr
->type
)
3651 /* ??? Handle BCOPY as well. */
3652 && (gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMCPY
)
3653 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMCPY_CHK
)
3654 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMPCPY
)
3655 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMPCPY_CHK
)
3656 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMMOVE
)
3657 || gimple_call_builtin_p (def_stmt
, BUILT_IN_MEMMOVE_CHK
))
3658 && (TREE_CODE (gimple_call_arg (def_stmt
, 0)) == ADDR_EXPR
3659 || TREE_CODE (gimple_call_arg (def_stmt
, 0)) == SSA_NAME
)
3660 && (TREE_CODE (gimple_call_arg (def_stmt
, 1)) == ADDR_EXPR
3661 || TREE_CODE (gimple_call_arg (def_stmt
, 1)) == SSA_NAME
)
3662 && (poly_int_tree_p (gimple_call_arg (def_stmt
, 2), ©_size
)
3663 || (TREE_CODE (gimple_call_arg (def_stmt
, 2)) == SSA_NAME
3664 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt
, 2)),
3666 /* Handling this is more complicated, give up for now. */
3667 && data
->partial_defs
.is_empty ())
3671 poly_int64 rhs_offset
, lhs_offset
;
3672 vn_reference_op_s op
;
3673 poly_uint64 mem_offset
;
3674 poly_int64 at
, byte_maxsize
;
3676 /* Only handle non-variable, addressable refs. */
3677 if (maybe_ne (ref
->size
, maxsize
)
3678 || !multiple_p (offset
, BITS_PER_UNIT
, &at
)
3679 || !multiple_p (maxsize
, BITS_PER_UNIT
, &byte_maxsize
))
3682 /* Extract a pointer base and an offset for the destination. */
3683 lhs
= gimple_call_arg (def_stmt
, 0);
3685 if (TREE_CODE (lhs
) == SSA_NAME
)
3687 lhs
= vn_valueize (lhs
);
3688 if (TREE_CODE (lhs
) == SSA_NAME
)
3690 gimple
*def_stmt
= SSA_NAME_DEF_STMT (lhs
);
3691 if (gimple_assign_single_p (def_stmt
)
3692 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
3693 lhs
= gimple_assign_rhs1 (def_stmt
);
3696 if (TREE_CODE (lhs
) == ADDR_EXPR
)
3698 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs
)))
3699 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs
))))
3701 tree tem
= get_addr_base_and_unit_offset (TREE_OPERAND (lhs
, 0),
3705 if (TREE_CODE (tem
) == MEM_REF
3706 && poly_int_tree_p (TREE_OPERAND (tem
, 1), &mem_offset
))
3708 lhs
= TREE_OPERAND (tem
, 0);
3709 if (TREE_CODE (lhs
) == SSA_NAME
)
3710 lhs
= vn_valueize (lhs
);
3711 lhs_offset
+= mem_offset
;
3713 else if (DECL_P (tem
))
3714 lhs
= build_fold_addr_expr (tem
);
3718 if (TREE_CODE (lhs
) != SSA_NAME
3719 && TREE_CODE (lhs
) != ADDR_EXPR
)
3722 /* Extract a pointer base and an offset for the source. */
3723 rhs
= gimple_call_arg (def_stmt
, 1);
3725 if (TREE_CODE (rhs
) == SSA_NAME
)
3726 rhs
= vn_valueize (rhs
);
3727 if (TREE_CODE (rhs
) == ADDR_EXPR
)
3729 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs
)))
3730 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs
))))
3732 tree tem
= get_addr_base_and_unit_offset (TREE_OPERAND (rhs
, 0),
3736 if (TREE_CODE (tem
) == MEM_REF
3737 && poly_int_tree_p (TREE_OPERAND (tem
, 1), &mem_offset
))
3739 rhs
= TREE_OPERAND (tem
, 0);
3740 rhs_offset
+= mem_offset
;
3742 else if (DECL_P (tem
)
3743 || TREE_CODE (tem
) == STRING_CST
)
3744 rhs
= build_fold_addr_expr (tem
);
3748 if (TREE_CODE (rhs
) == SSA_NAME
)
3749 rhs
= SSA_VAL (rhs
);
3750 else if (TREE_CODE (rhs
) != ADDR_EXPR
)
3753 /* The bases of the destination and the references have to agree. */
3754 if (TREE_CODE (base
) == MEM_REF
)
3756 if (TREE_OPERAND (base
, 0) != lhs
3757 || !poly_int_tree_p (TREE_OPERAND (base
, 1), &mem_offset
))
3761 else if (!DECL_P (base
)
3762 || TREE_CODE (lhs
) != ADDR_EXPR
3763 || TREE_OPERAND (lhs
, 0) != base
)
3766 /* If the access is completely outside of the memcpy destination
3767 area there is no aliasing. */
3768 if (!ranges_maybe_overlap_p (lhs_offset
, copy_size
, at
, byte_maxsize
))
3770 /* And the access has to be contained within the memcpy destination. */
3771 if (!known_subrange_p (at
, byte_maxsize
, lhs_offset
, copy_size
))
3774 /* Save the operands since we need to use the original ones for
3775 the hash entry we use. */
3776 if (!data
->saved_operands
.exists ())
3777 data
->saved_operands
= vr
->operands
.copy ();
3779 /* Make room for 2 operands in the new reference. */
3780 if (vr
->operands
.length () < 2)
3782 vec
<vn_reference_op_s
> old
= vr
->operands
;
3783 vr
->operands
.safe_grow_cleared (2, true);
3784 if (old
== shared_lookup_references
)
3785 shared_lookup_references
= vr
->operands
;
3788 vr
->operands
.truncate (2);
3790 /* The looked-through reference is a simple MEM_REF. */
3791 memset (&op
, 0, sizeof (op
));
3793 op
.opcode
= MEM_REF
;
3794 op
.op0
= build_int_cst (ptr_type_node
, at
- lhs_offset
+ rhs_offset
);
3795 op
.off
= at
- lhs_offset
+ rhs_offset
;
3796 vr
->operands
[0] = op
;
3797 op
.type
= TREE_TYPE (rhs
);
3798 op
.opcode
= TREE_CODE (rhs
);
3801 vr
->operands
[1] = op
;
3802 vr
->hashcode
= vn_reference_compute_hash (vr
);
3804 /* Try folding the new reference to a constant. */
3805 tree val
= fully_constant_vn_reference_p (vr
);
3807 return data
->finish (0, 0, val
);
3809 /* Adjust *ref from the new operands. */
3810 if (!ao_ref_init_from_vn_reference (&r
, 0, 0, vr
->type
, vr
->operands
))
3812 /* This can happen with bitfields. */
3813 if (maybe_ne (ref
->size
, r
.size
))
3816 vr
->offset
= r
.offset
;
3817 vr
->max_size
= r
.max_size
;
3819 /* Do not update last seen VUSE after translating. */
3820 data
->last_vuse_ptr
= NULL
;
3821 /* Invalidate the original access path since it now contains
3823 data
->orig_ref
.ref
= NULL_TREE
;
3824 /* Use the alias-set of this stmt for recording an eventual result. */
3825 if (data
->first_set
== -2)
3827 data
->first_set
= 0;
3828 data
->first_base_set
= 0;
3831 /* Keep looking for the adjusted *REF / VR pair. */
3835 /* Bail out and stop walking. */
3839 /* Return a reference op vector from OP that can be used for
3840 vn_reference_lookup_pieces. The caller is responsible for releasing
3843 vec
<vn_reference_op_s
>
3844 vn_reference_operands_for_lookup (tree op
)
3847 return valueize_shared_reference_ops_from_ref (op
, &valueized
).copy ();
3850 /* Lookup a reference operation by it's parts, in the current hash table.
3851 Returns the resulting value number if it exists in the hash table,
3852 NULL_TREE otherwise. VNRESULT will be filled in with the actual
3853 vn_reference_t stored in the hashtable if something is found. */
3856 vn_reference_lookup_pieces (tree vuse
, alias_set_type set
,
3857 alias_set_type base_set
, tree type
,
3858 vec
<vn_reference_op_s
> operands
,
3859 vn_reference_t
*vnresult
, vn_lookup_kind kind
)
3861 struct vn_reference_s vr1
;
3869 vr1
.vuse
= vuse_ssa_val (vuse
);
3870 shared_lookup_references
.truncate (0);
3871 shared_lookup_references
.safe_grow (operands
.length (), true);
3872 memcpy (shared_lookup_references
.address (),
3873 operands
.address (),
3874 sizeof (vn_reference_op_s
)
3875 * operands
.length ());
3877 valueize_refs_1 (&shared_lookup_references
, &valueized_p
);
3878 vr1
.operands
= shared_lookup_references
;
3881 vr1
.base_set
= base_set
;
3882 /* We can pretend there's no extra info fed in since the ao_refs offset
3883 and max_size are computed only from the VN reference ops. */
3886 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
3887 if ((cst
= fully_constant_vn_reference_p (&vr1
)))
3890 vn_reference_lookup_1 (&vr1
, vnresult
);
3892 && kind
!= VN_NOWALK
3896 unsigned limit
= param_sccvn_max_alias_queries_per_access
;
3897 vn_walk_cb_data
data (&vr1
, NULL_TREE
, NULL
, kind
, true, NULL_TREE
,
3899 vec
<vn_reference_op_s
> ops_for_ref
;
3901 ops_for_ref
= vr1
.operands
;
3904 /* For ao_ref_from_mem we have to ensure only available SSA names
3905 end up in base and the only convenient way to make this work
3906 for PRE is to re-valueize with that in mind. */
3907 ops_for_ref
.create (operands
.length ());
3908 ops_for_ref
.quick_grow (operands
.length ());
3909 memcpy (ops_for_ref
.address (),
3910 operands
.address (),
3911 sizeof (vn_reference_op_s
)
3912 * operands
.length ());
3913 valueize_refs_1 (&ops_for_ref
, &valueized_p
, true);
3915 if (ao_ref_init_from_vn_reference (&r
, set
, base_set
, type
,
3919 walk_non_aliased_vuses (&r
, vr1
.vuse
, true, vn_reference_lookup_2
,
3920 vn_reference_lookup_3
, vuse_valueize
,
3922 if (ops_for_ref
!= shared_lookup_references
)
3923 ops_for_ref
.release ();
3924 gcc_checking_assert (vr1
.operands
== shared_lookup_references
);
3927 && (!(*vnresult
)->result
3928 || !operand_equal_p ((*vnresult
)->result
, data
.same_val
)))
3936 return (*vnresult
)->result
;
3941 /* Lookup OP in the current hash table, and return the resulting value
3942 number if it exists in the hash table. Return NULL_TREE if it does
3943 not exist in the hash table or if the result field of the structure
3944 was NULL.. VNRESULT will be filled in with the vn_reference_t
3945 stored in the hashtable if one exists. When TBAA_P is false assume
3946 we are looking up a store and treat it as having alias-set zero.
3947 *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
3948 MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
3949 load is bitwise anded with MASK and so we are only interested in a subset
3950 of the bits and can ignore if the other bits are uninitialized or
3951 not initialized with constants. When doing redundant store removal
3952 the caller has to set REDUNDANT_STORE_REMOVAL_P. */
3955 vn_reference_lookup (tree op
, tree vuse
, vn_lookup_kind kind
,
3956 vn_reference_t
*vnresult
, bool tbaa_p
,
3957 tree
*last_vuse_ptr
, tree mask
,
3958 bool redundant_store_removal_p
)
3960 vec
<vn_reference_op_s
> operands
;
3961 struct vn_reference_s vr1
;
3962 bool valueized_anything
;
3967 vr1
.vuse
= vuse_ssa_val (vuse
);
3968 vr1
.operands
= operands
3969 = valueize_shared_reference_ops_from_ref (op
, &valueized_anything
);
3971 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
3972 this before the pass folding __builtin_object_size had a chance to run. */
3973 if ((cfun
->curr_properties
& PROP_objsz
)
3974 && operands
[0].opcode
== ADDR_EXPR
3975 && operands
.last ().opcode
== SSA_NAME
)
3978 vn_reference_op_t vro
;
3980 for (i
= 1; operands
.iterate (i
, &vro
); ++i
)
3982 if (vro
->opcode
== SSA_NAME
)
3984 else if (known_eq (vro
->off
, -1))
3988 if (i
== operands
.length () - 1
3989 /* Make sure we the offset we accumulated in a 64bit int
3990 fits the address computation carried out in target
3991 offset precision. */
3993 == sext_hwi (off
.coeffs
[0], TYPE_PRECISION (sizetype
))))
3995 gcc_assert (operands
[i
-1].opcode
== MEM_REF
);
3997 ops
[0] = operands
[i
].op0
;
3998 ops
[1] = wide_int_to_tree (sizetype
, off
);
3999 tree res
= vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR
,
4000 TREE_TYPE (op
), ops
, NULL
);
4007 vr1
.type
= TREE_TYPE (op
);
4009 ao_ref_init (&op_ref
, op
);
4010 vr1
.set
= ao_ref_alias_set (&op_ref
);
4011 vr1
.base_set
= ao_ref_base_alias_set (&op_ref
);
4014 vr1
.hashcode
= vn_reference_compute_hash (&vr1
);
4015 if (mask
== NULL_TREE
)
4016 if (tree cst
= fully_constant_vn_reference_p (&vr1
))
4019 if (kind
!= VN_NOWALK
&& vr1
.vuse
)
4021 vn_reference_t wvnresult
;
4023 unsigned limit
= param_sccvn_max_alias_queries_per_access
;
4024 auto_vec
<vn_reference_op_s
> ops_for_ref
;
4025 if (valueized_anything
)
4027 copy_reference_ops_from_ref (op
, &ops_for_ref
);
4029 valueize_refs_1 (&ops_for_ref
, &tem
, true);
4031 /* Make sure to use a valueized reference if we valueized anything.
4032 Otherwise preserve the full reference for advanced TBAA. */
4033 if (!valueized_anything
4034 || !ao_ref_init_from_vn_reference (&r
, vr1
.set
, vr1
.base_set
,
4035 vr1
.type
, ops_for_ref
))
4037 ao_ref_init (&r
, op
);
4038 /* Record the extra info we're getting from the full ref. */
4040 vr1
.offset
= r
.offset
;
4041 vr1
.max_size
= r
.max_size
;
4043 vn_walk_cb_data
data (&vr1
, r
.ref
? NULL_TREE
: op
,
4044 last_vuse_ptr
, kind
, tbaa_p
, mask
,
4045 redundant_store_removal_p
);
4049 walk_non_aliased_vuses (&r
, vr1
.vuse
, tbaa_p
, vn_reference_lookup_2
,
4050 vn_reference_lookup_3
, vuse_valueize
, limit
,
4052 gcc_checking_assert (vr1
.operands
== shared_lookup_references
);
4055 gcc_assert (mask
== NULL_TREE
);
4057 && (!wvnresult
->result
4058 || !operand_equal_p (wvnresult
->result
, data
.same_val
)))
4061 *vnresult
= wvnresult
;
4062 return wvnresult
->result
;
4065 return data
.masked_result
;
4071 *last_vuse_ptr
= vr1
.vuse
;
4074 return vn_reference_lookup_1 (&vr1
, vnresult
);
4077 /* Lookup CALL in the current hash table and return the entry in
4078 *VNRESULT if found. Populates *VR for the hashtable lookup. */
4081 vn_reference_lookup_call (gcall
*call
, vn_reference_t
*vnresult
,
4087 tree vuse
= gimple_vuse (call
);
4089 vr
->vuse
= vuse
? SSA_VAL (vuse
) : NULL_TREE
;
4090 vr
->operands
= valueize_shared_reference_ops_from_call (call
);
4091 tree lhs
= gimple_call_lhs (call
);
4092 /* For non-SSA return values the referece ops contain the LHS. */
4093 vr
->type
= ((lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
4094 ? TREE_TYPE (lhs
) : NULL_TREE
);
4100 vr
->hashcode
= vn_reference_compute_hash (vr
);
4101 vn_reference_lookup_1 (vr
, vnresult
);
4104 /* Insert OP into the current hash table with a value number of RESULT. */
4107 vn_reference_insert (tree op
, tree result
, tree vuse
, tree vdef
)
4109 vn_reference_s
**slot
;
4113 vec
<vn_reference_op_s
> operands
4114 = valueize_shared_reference_ops_from_ref (op
, &tem
);
4115 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4116 before the pass folding __builtin_object_size had a chance to run. */
4117 if ((cfun
->curr_properties
& PROP_objsz
)
4118 && operands
[0].opcode
== ADDR_EXPR
4119 && operands
.last ().opcode
== SSA_NAME
)
4122 vn_reference_op_t vro
;
4124 for (i
= 1; operands
.iterate (i
, &vro
); ++i
)
4126 if (vro
->opcode
== SSA_NAME
)
4128 else if (known_eq (vro
->off
, -1))
4132 if (i
== operands
.length () - 1
4133 /* Make sure we the offset we accumulated in a 64bit int
4134 fits the address computation carried out in target
4135 offset precision. */
4137 == sext_hwi (off
.coeffs
[0], TYPE_PRECISION (sizetype
))))
4139 gcc_assert (operands
[i
-1].opcode
== MEM_REF
);
4141 ops
[0] = operands
[i
].op0
;
4142 ops
[1] = wide_int_to_tree (sizetype
, off
);
4143 vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR
,
4144 TREE_TYPE (op
), ops
, result
,
4145 VN_INFO (result
)->value_id
);
4150 vr1
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
4151 if (TREE_CODE (result
) == SSA_NAME
)
4152 vr1
->value_id
= VN_INFO (result
)->value_id
;
4154 vr1
->value_id
= get_or_alloc_constant_value_id (result
);
4155 vr1
->vuse
= vuse_ssa_val (vuse
);
4156 vr1
->operands
= operands
.copy ();
4157 vr1
->type
= TREE_TYPE (op
);
4158 vr1
->punned
= false;
4160 ao_ref_init (&op_ref
, op
);
4161 vr1
->set
= ao_ref_alias_set (&op_ref
);
4162 vr1
->base_set
= ao_ref_base_alias_set (&op_ref
);
4163 /* Specifically use an unknown extent here, we're not doing any lookup
4164 and assume the caller didn't either (or it went VARYING). */
4167 vr1
->hashcode
= vn_reference_compute_hash (vr1
);
4168 vr1
->result
= TREE_CODE (result
) == SSA_NAME
? SSA_VAL (result
) : result
;
4169 vr1
->result_vdef
= vdef
;
4171 slot
= valid_info
->references
->find_slot_with_hash (vr1
, vr1
->hashcode
,
4174 /* Because IL walking on reference lookup can end up visiting
4175 a def that is only to be visited later in iteration order
4176 when we are about to make an irreducible region reducible
4177 the def can be effectively processed and its ref being inserted
4178 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4179 but save a lookup if we deal with already inserted refs here. */
4182 /* We cannot assert that we have the same value either because
4183 when disentangling an irreducible region we may end up visiting
4184 a use before the corresponding def. That's a missed optimization
4185 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4186 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
4187 && !operand_equal_p ((*slot
)->result
, vr1
->result
, 0))
4189 fprintf (dump_file
, "Keeping old value ");
4190 print_generic_expr (dump_file
, (*slot
)->result
);
4191 fprintf (dump_file
, " because of collision\n");
4193 free_reference (vr1
);
4194 obstack_free (&vn_tables_obstack
, vr1
);
4199 vr1
->next
= last_inserted_ref
;
4200 last_inserted_ref
= vr1
;
4203 /* Insert a reference by it's pieces into the current hash table with
4204 a value number of RESULT. Return the resulting reference
4205 structure we created. */
4208 vn_reference_insert_pieces (tree vuse
, alias_set_type set
,
4209 alias_set_type base_set
,
4210 poly_int64 offset
, poly_int64 max_size
, tree type
,
4211 vec
<vn_reference_op_s
> operands
,
4212 tree result
, unsigned int value_id
)
4215 vn_reference_s
**slot
;
4218 vr1
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
4219 vr1
->value_id
= value_id
;
4220 vr1
->vuse
= vuse_ssa_val (vuse
);
4221 vr1
->operands
= operands
;
4222 valueize_refs (&vr1
->operands
);
4224 vr1
->punned
= false;
4226 vr1
->base_set
= base_set
;
4227 vr1
->offset
= offset
;
4228 vr1
->max_size
= max_size
;
4229 vr1
->hashcode
= vn_reference_compute_hash (vr1
);
4230 if (result
&& TREE_CODE (result
) == SSA_NAME
)
4231 result
= SSA_VAL (result
);
4232 vr1
->result
= result
;
4233 vr1
->result_vdef
= NULL_TREE
;
4235 slot
= valid_info
->references
->find_slot_with_hash (vr1
, vr1
->hashcode
,
4238 /* At this point we should have all the things inserted that we have
4239 seen before, and we should never try inserting something that
4241 gcc_assert (!*slot
);
4244 vr1
->next
= last_inserted_ref
;
4245 last_inserted_ref
= vr1
;
4249 /* Compute and return the hash value for nary operation VBO1. */
4252 vn_nary_op_compute_hash (const vn_nary_op_t vno1
)
4254 inchash::hash hstate
;
4257 if (((vno1
->length
== 2
4258 && commutative_tree_code (vno1
->opcode
))
4259 || (vno1
->length
== 3
4260 && commutative_ternary_tree_code (vno1
->opcode
)))
4261 && tree_swap_operands_p (vno1
->op
[0], vno1
->op
[1]))
4262 std::swap (vno1
->op
[0], vno1
->op
[1]);
4263 else if (TREE_CODE_CLASS (vno1
->opcode
) == tcc_comparison
4264 && tree_swap_operands_p (vno1
->op
[0], vno1
->op
[1]))
4266 std::swap (vno1
->op
[0], vno1
->op
[1]);
4267 vno1
->opcode
= swap_tree_comparison (vno1
->opcode
);
4270 hstate
.add_int (vno1
->opcode
);
4271 for (i
= 0; i
< vno1
->length
; ++i
)
4272 inchash::add_expr (vno1
->op
[i
], hstate
);
4274 return hstate
.end ();
4277 /* Compare nary operations VNO1 and VNO2 and return true if they are
4281 vn_nary_op_eq (const_vn_nary_op_t
const vno1
, const_vn_nary_op_t
const vno2
)
4285 if (vno1
->hashcode
!= vno2
->hashcode
)
4288 if (vno1
->length
!= vno2
->length
)
4291 if (vno1
->opcode
!= vno2
->opcode
4292 || !types_compatible_p (vno1
->type
, vno2
->type
))
4295 for (i
= 0; i
< vno1
->length
; ++i
)
4296 if (!expressions_equal_p (vno1
->op
[i
], vno2
->op
[i
]))
4299 /* BIT_INSERT_EXPR has an implict operand as the type precision
4300 of op1. Need to check to make sure they are the same. */
4301 if (vno1
->opcode
== BIT_INSERT_EXPR
4302 && TREE_CODE (vno1
->op
[1]) == INTEGER_CST
4303 && TYPE_PRECISION (TREE_TYPE (vno1
->op
[1]))
4304 != TYPE_PRECISION (TREE_TYPE (vno2
->op
[1])))
4310 /* Initialize VNO from the pieces provided. */
4313 init_vn_nary_op_from_pieces (vn_nary_op_t vno
, unsigned int length
,
4314 enum tree_code code
, tree type
, tree
*ops
)
4317 vno
->length
= length
;
4319 memcpy (&vno
->op
[0], ops
, sizeof (tree
) * length
);
4322 /* Return the number of operands for a vn_nary ops structure from STMT. */
4325 vn_nary_length_from_stmt (gimple
*stmt
)
4327 switch (gimple_assign_rhs_code (stmt
))
4331 case VIEW_CONVERT_EXPR
:
4338 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt
));
4341 return gimple_num_ops (stmt
) - 1;
4345 /* Initialize VNO from STMT. */
4348 init_vn_nary_op_from_stmt (vn_nary_op_t vno
, gassign
*stmt
)
4352 vno
->opcode
= gimple_assign_rhs_code (stmt
);
4353 vno
->type
= TREE_TYPE (gimple_assign_lhs (stmt
));
4354 switch (vno
->opcode
)
4358 case VIEW_CONVERT_EXPR
:
4360 vno
->op
[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
4365 vno
->op
[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
4366 vno
->op
[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1);
4367 vno
->op
[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2);
4371 vno
->length
= CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt
));
4372 for (i
= 0; i
< vno
->length
; ++i
)
4373 vno
->op
[i
] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt
), i
)->value
;
4377 gcc_checking_assert (!gimple_assign_single_p (stmt
));
4378 vno
->length
= gimple_num_ops (stmt
) - 1;
4379 for (i
= 0; i
< vno
->length
; ++i
)
4380 vno
->op
[i
] = gimple_op (stmt
, i
+ 1);
4384 /* Compute the hashcode for VNO and look for it in the hash table;
4385 return the resulting value number if it exists in the hash table.
4386 Return NULL_TREE if it does not exist in the hash table or if the
4387 result field of the operation is NULL. VNRESULT will contain the
4388 vn_nary_op_t from the hashtable if it exists. */
4391 vn_nary_op_lookup_1 (vn_nary_op_t vno
, vn_nary_op_t
*vnresult
)
4393 vn_nary_op_s
**slot
;
4398 for (unsigned i
= 0; i
< vno
->length
; ++i
)
4399 if (TREE_CODE (vno
->op
[i
]) == SSA_NAME
)
4400 vno
->op
[i
] = SSA_VAL (vno
->op
[i
]);
4402 vno
->hashcode
= vn_nary_op_compute_hash (vno
);
4403 slot
= valid_info
->nary
->find_slot_with_hash (vno
, vno
->hashcode
, NO_INSERT
);
4408 return (*slot
)->predicated_values
? NULL_TREE
: (*slot
)->u
.result
;
4411 /* Lookup a n-ary operation by its pieces and return the resulting value
4412 number if it exists in the hash table. Return NULL_TREE if it does
4413 not exist in the hash table or if the result field of the operation
4414 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4418 vn_nary_op_lookup_pieces (unsigned int length
, enum tree_code code
,
4419 tree type
, tree
*ops
, vn_nary_op_t
*vnresult
)
4421 vn_nary_op_t vno1
= XALLOCAVAR (struct vn_nary_op_s
,
4422 sizeof_vn_nary_op (length
));
4423 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4424 return vn_nary_op_lookup_1 (vno1
, vnresult
);
4427 /* Lookup the rhs of STMT in the current hash table, and return the resulting
4428 value number if it exists in the hash table. Return NULL_TREE if
4429 it does not exist in the hash table. VNRESULT will contain the
4430 vn_nary_op_t from the hashtable if it exists. */
4433 vn_nary_op_lookup_stmt (gimple
*stmt
, vn_nary_op_t
*vnresult
)
4436 = XALLOCAVAR (struct vn_nary_op_s
,
4437 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt
)));
4438 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (stmt
));
4439 return vn_nary_op_lookup_1 (vno1
, vnresult
);
4442 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4445 alloc_vn_nary_op_noinit (unsigned int length
, struct obstack
*stack
)
4447 return (vn_nary_op_t
) obstack_alloc (stack
, sizeof_vn_nary_op (length
));
4450 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4454 alloc_vn_nary_op (unsigned int length
, tree result
, unsigned int value_id
)
4456 vn_nary_op_t vno1
= alloc_vn_nary_op_noinit (length
, &vn_tables_obstack
);
4458 vno1
->value_id
= value_id
;
4459 vno1
->length
= length
;
4460 vno1
->predicated_values
= 0;
4461 vno1
->u
.result
= result
;
4466 /* Insert VNO into TABLE. */
4469 vn_nary_op_insert_into (vn_nary_op_t vno
, vn_nary_op_table_type
*table
)
4471 vn_nary_op_s
**slot
;
4473 gcc_assert (! vno
->predicated_values
4474 || (! vno
->u
.values
->next
4475 && vno
->u
.values
->n
== 1));
4477 for (unsigned i
= 0; i
< vno
->length
; ++i
)
4478 if (TREE_CODE (vno
->op
[i
]) == SSA_NAME
)
4479 vno
->op
[i
] = SSA_VAL (vno
->op
[i
]);
4481 vno
->hashcode
= vn_nary_op_compute_hash (vno
);
4482 slot
= table
->find_slot_with_hash (vno
, vno
->hashcode
, INSERT
);
4483 vno
->unwind_to
= *slot
;
4486 /* Prefer non-predicated values.
4487 ??? Only if those are constant, otherwise, with constant predicated
4488 value, turn them into predicated values with entry-block validity
4489 (??? but we always find the first valid result currently). */
4490 if ((*slot
)->predicated_values
4491 && ! vno
->predicated_values
)
4493 /* ??? We cannot remove *slot from the unwind stack list.
4494 For the moment we deal with this by skipping not found
4495 entries but this isn't ideal ... */
4497 /* ??? Maintain a stack of states we can unwind in
4498 vn_nary_op_s? But how far do we unwind? In reality
4499 we need to push change records somewhere... Or not
4500 unwind vn_nary_op_s and linking them but instead
4501 unwind the results "list", linking that, which also
4502 doesn't move on hashtable resize. */
4503 /* We can also have a ->unwind_to recording *slot there.
4504 That way we can make u.values a fixed size array with
4505 recording the number of entries but of course we then
4506 have always N copies for each unwind_to-state. Or we
4507 make sure to only ever append and each unwinding will
4508 pop off one entry (but how to deal with predicated
4509 replaced with non-predicated here?) */
4510 vno
->next
= last_inserted_nary
;
4511 last_inserted_nary
= vno
;
4514 else if (vno
->predicated_values
4515 && ! (*slot
)->predicated_values
)
4517 else if (vno
->predicated_values
4518 && (*slot
)->predicated_values
)
4520 /* ??? Factor this all into a insert_single_predicated_value
4522 gcc_assert (!vno
->u
.values
->next
&& vno
->u
.values
->n
== 1);
4524 = BASIC_BLOCK_FOR_FN (cfun
, vno
->u
.values
->valid_dominated_by_p
[0]);
4525 vn_pval
*nval
= vno
->u
.values
;
4526 vn_pval
**next
= &vno
->u
.values
;
4528 for (vn_pval
*val
= (*slot
)->u
.values
; val
; val
= val
->next
)
4530 if (expressions_equal_p (val
->result
, nval
->result
))
4533 for (unsigned i
= 0; i
< val
->n
; ++i
)
4536 = BASIC_BLOCK_FOR_FN (cfun
,
4537 val
->valid_dominated_by_p
[i
]);
4538 if (dominated_by_p (CDI_DOMINATORS
, vno_bb
, val_bb
))
4539 /* Value registered with more generic predicate. */
4541 else if (flag_checking
)
4542 /* Shouldn't happen, we insert in RPO order. */
4543 gcc_assert (!dominated_by_p (CDI_DOMINATORS
,
4547 *next
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4549 + val
->n
* sizeof (int));
4550 (*next
)->next
= NULL
;
4551 (*next
)->result
= val
->result
;
4552 (*next
)->n
= val
->n
+ 1;
4553 memcpy ((*next
)->valid_dominated_by_p
,
4554 val
->valid_dominated_by_p
,
4555 val
->n
* sizeof (int));
4556 (*next
)->valid_dominated_by_p
[val
->n
] = vno_bb
->index
;
4557 next
= &(*next
)->next
;
4558 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4559 fprintf (dump_file
, "Appending predicate to value.\n");
4562 /* Copy other predicated values. */
4563 *next
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4565 + (val
->n
-1) * sizeof (int));
4566 memcpy (*next
, val
, sizeof (vn_pval
) + (val
->n
-1) * sizeof (int));
4567 (*next
)->next
= NULL
;
4568 next
= &(*next
)->next
;
4574 vno
->next
= last_inserted_nary
;
4575 last_inserted_nary
= vno
;
4579 /* While we do not want to insert things twice it's awkward to
4580 avoid it in the case where visit_nary_op pattern-matches stuff
4581 and ends up simplifying the replacement to itself. We then
4582 get two inserts, one from visit_nary_op and one from
4583 vn_nary_build_or_lookup.
4584 So allow inserts with the same value number. */
4585 if ((*slot
)->u
.result
== vno
->u
.result
)
4589 /* ??? There's also optimistic vs. previous commited state merging
4590 that is problematic for the case of unwinding. */
4592 /* ??? We should return NULL if we do not use 'vno' and have the
4593 caller release it. */
4594 gcc_assert (!*slot
);
4597 vno
->next
= last_inserted_nary
;
4598 last_inserted_nary
= vno
;
4602 /* Insert a n-ary operation into the current hash table using it's
4603 pieces. Return the vn_nary_op_t structure we created and put in
4607 vn_nary_op_insert_pieces (unsigned int length
, enum tree_code code
,
4608 tree type
, tree
*ops
,
4609 tree result
, unsigned int value_id
)
4611 vn_nary_op_t vno1
= alloc_vn_nary_op (length
, result
, value_id
);
4612 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4613 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4616 /* Return whether we can track a predicate valid when PRED_E is executed. */
4619 can_track_predicate_on_edge (edge pred_e
)
4621 /* ??? As we are currently recording the destination basic-block index in
4622 vn_pval.valid_dominated_by_p and using dominance for the
4623 validity check we cannot track predicates on all edges. */
4624 if (single_pred_p (pred_e
->dest
))
4626 /* Never record for backedges. */
4627 if (pred_e
->flags
& EDGE_DFS_BACK
)
4629 /* When there's more than one predecessor we cannot track
4630 predicate validity based on the destination block. The
4631 exception is when all other incoming edges sources are
4632 dominated by the destination block. */
4635 FOR_EACH_EDGE (e
, ei
, pred_e
->dest
->preds
)
4636 if (e
!= pred_e
&& ! dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
4642 vn_nary_op_insert_pieces_predicated (unsigned int length
, enum tree_code code
,
4643 tree type
, tree
*ops
,
4644 tree result
, unsigned int value_id
,
4647 gcc_assert (can_track_predicate_on_edge (pred_e
));
4649 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
4650 /* ??? Fix dumping, but currently we only get comparisons. */
4651 && TREE_CODE_CLASS (code
) == tcc_comparison
)
4653 fprintf (dump_file
, "Recording on edge %d->%d ", pred_e
->src
->index
,
4654 pred_e
->dest
->index
);
4655 print_generic_expr (dump_file
, ops
[0], TDF_SLIM
);
4656 fprintf (dump_file
, " %s ", get_tree_code_name (code
));
4657 print_generic_expr (dump_file
, ops
[1], TDF_SLIM
);
4658 fprintf (dump_file
, " == %s\n",
4659 integer_zerop (result
) ? "false" : "true");
4661 vn_nary_op_t vno1
= alloc_vn_nary_op (length
, NULL_TREE
, value_id
);
4662 init_vn_nary_op_from_pieces (vno1
, length
, code
, type
, ops
);
4663 vno1
->predicated_values
= 1;
4664 vno1
->u
.values
= (vn_pval
*) obstack_alloc (&vn_tables_obstack
,
4666 vno1
->u
.values
->next
= NULL
;
4667 vno1
->u
.values
->result
= result
;
4668 vno1
->u
.values
->n
= 1;
4669 vno1
->u
.values
->valid_dominated_by_p
[0] = pred_e
->dest
->index
;
4670 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4674 dominated_by_p_w_unex (basic_block bb1
, basic_block bb2
, bool);
4677 vn_nary_op_get_predicated_value (vn_nary_op_t vno
, basic_block bb
,
4680 if (! vno
->predicated_values
)
4681 return vno
->u
.result
;
4682 for (vn_pval
*val
= vno
->u
.values
; val
; val
= val
->next
)
4683 for (unsigned i
= 0; i
< val
->n
; ++i
)
4686 = BASIC_BLOCK_FOR_FN (cfun
, val
->valid_dominated_by_p
[i
]);
4687 /* Do not handle backedge executability optimistically since
4688 when figuring out whether to iterate we do not consider
4689 changed predication.
4690 When asking for predicated values on an edge avoid looking
4691 at edge executability for edges forward in our iteration
4693 if (e
&& (e
->flags
& EDGE_DFS_BACK
))
4695 if (dominated_by_p (CDI_DOMINATORS
, bb
, cand
))
4698 else if (dominated_by_p_w_unex (bb
, cand
, false))
4705 vn_nary_op_get_predicated_value (vn_nary_op_t vno
, edge e
)
4707 return vn_nary_op_get_predicated_value (vno
, e
->src
, e
);
4710 /* Insert the rhs of STMT into the current hash table with a value number of
4714 vn_nary_op_insert_stmt (gimple
*stmt
, tree result
)
4717 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt
),
4718 result
, VN_INFO (result
)->value_id
);
4719 init_vn_nary_op_from_stmt (vno1
, as_a
<gassign
*> (stmt
));
4720 return vn_nary_op_insert_into (vno1
, valid_info
->nary
);
4723 /* Compute a hashcode for PHI operation VP1 and return it. */
4725 static inline hashval_t
4726 vn_phi_compute_hash (vn_phi_t vp1
)
4728 inchash::hash hstate
;
4734 hstate
.add_int (EDGE_COUNT (vp1
->block
->preds
));
4735 switch (EDGE_COUNT (vp1
->block
->preds
))
4740 /* When this is a PHI node subject to CSE for different blocks
4741 avoid hashing the block index. */
4746 hstate
.add_int (vp1
->block
->index
);
4749 /* If all PHI arguments are constants we need to distinguish
4750 the PHI node via its type. */
4752 hstate
.merge_hash (vn_hash_type (type
));
4754 FOR_EACH_EDGE (e
, ei
, vp1
->block
->preds
)
4756 /* Don't hash backedge values they need to be handled as VN_TOP
4757 for optimistic value-numbering. */
4758 if (e
->flags
& EDGE_DFS_BACK
)
4761 phi1op
= vp1
->phiargs
[e
->dest_idx
];
4762 if (phi1op
== VN_TOP
)
4764 inchash::add_expr (phi1op
, hstate
);
4767 return hstate
.end ();
4771 /* Return true if COND1 and COND2 represent the same condition, set
4772 *INVERTED_P if one needs to be inverted to make it the same as
4776 cond_stmts_equal_p (gcond
*cond1
, tree lhs1
, tree rhs1
,
4777 gcond
*cond2
, tree lhs2
, tree rhs2
, bool *inverted_p
)
4779 enum tree_code code1
= gimple_cond_code (cond1
);
4780 enum tree_code code2
= gimple_cond_code (cond2
);
4782 *inverted_p
= false;
4785 else if (code1
== swap_tree_comparison (code2
))
4786 std::swap (lhs2
, rhs2
);
4787 else if (code1
== invert_tree_comparison (code2
, HONOR_NANS (lhs2
)))
4789 else if (code1
== invert_tree_comparison
4790 (swap_tree_comparison (code2
), HONOR_NANS (lhs2
)))
4792 std::swap (lhs2
, rhs2
);
4798 return ((expressions_equal_p (lhs1
, lhs2
)
4799 && expressions_equal_p (rhs1
, rhs2
))
4800 || (commutative_tree_code (code1
)
4801 && expressions_equal_p (lhs1
, rhs2
)
4802 && expressions_equal_p (rhs1
, lhs2
)));
4805 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4808 vn_phi_eq (const_vn_phi_t
const vp1
, const_vn_phi_t
const vp2
)
4810 if (vp1
->hashcode
!= vp2
->hashcode
)
4813 if (vp1
->block
!= vp2
->block
)
4815 if (EDGE_COUNT (vp1
->block
->preds
) != EDGE_COUNT (vp2
->block
->preds
))
4818 switch (EDGE_COUNT (vp1
->block
->preds
))
4821 /* Single-arg PHIs are just copies. */
4826 /* Make sure both PHIs are classified as CSEable. */
4827 if (! vp1
->cclhs
|| ! vp2
->cclhs
)
4830 /* Rule out backedges into the PHI. */
4832 (vp1
->block
->loop_father
->header
!= vp1
->block
4833 && vp2
->block
->loop_father
->header
!= vp2
->block
);
4835 /* If the PHI nodes do not have compatible types
4836 they are not the same. */
4837 if (!types_compatible_p (vp1
->type
, vp2
->type
))
4840 /* If the immediate dominator end in switch stmts multiple
4841 values may end up in the same PHI arg via intermediate
4844 = get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4846 = get_immediate_dominator (CDI_DOMINATORS
, vp2
->block
);
4847 gcc_checking_assert (EDGE_COUNT (idom1
->succs
) == 2
4848 && EDGE_COUNT (idom2
->succs
) == 2);
4850 /* Verify the controlling stmt is the same. */
4851 gcond
*last1
= as_a
<gcond
*> (*gsi_last_bb (idom1
));
4852 gcond
*last2
= as_a
<gcond
*> (*gsi_last_bb (idom2
));
4854 if (! cond_stmts_equal_p (last1
, vp1
->cclhs
, vp1
->ccrhs
,
4855 last2
, vp2
->cclhs
, vp2
->ccrhs
,
4859 /* Get at true/false controlled edges into the PHI. */
4860 edge te1
, te2
, fe1
, fe2
;
4861 if (! extract_true_false_controlled_edges (idom1
, vp1
->block
,
4863 || ! extract_true_false_controlled_edges (idom2
, vp2
->block
,
4867 /* Swap edges if the second condition is the inverted of the
4870 std::swap (te2
, fe2
);
4872 /* Since we do not know which edge will be executed we have
4873 to be careful when matching VN_TOP. Be conservative and
4874 only match VN_TOP == VN_TOP for now, we could allow
4875 VN_TOP on the not prevailing PHI though. See for example
4877 if (! expressions_equal_p (vp1
->phiargs
[te1
->dest_idx
],
4878 vp2
->phiargs
[te2
->dest_idx
], false)
4879 || ! expressions_equal_p (vp1
->phiargs
[fe1
->dest_idx
],
4880 vp2
->phiargs
[fe2
->dest_idx
], false))
4891 /* If the PHI nodes do not have compatible types
4892 they are not the same. */
4893 if (!types_compatible_p (vp1
->type
, vp2
->type
))
4896 /* Any phi in the same block will have it's arguments in the
4897 same edge order, because of how we store phi nodes. */
4898 unsigned nargs
= EDGE_COUNT (vp1
->block
->preds
);
4899 for (unsigned i
= 0; i
< nargs
; ++i
)
4901 tree phi1op
= vp1
->phiargs
[i
];
4902 tree phi2op
= vp2
->phiargs
[i
];
4903 if (phi1op
== phi2op
)
4905 if (!expressions_equal_p (phi1op
, phi2op
, false))
4912 /* Lookup PHI in the current hash table, and return the resulting
4913 value number if it exists in the hash table. Return NULL_TREE if
4914 it does not exist in the hash table. */
4917 vn_phi_lookup (gimple
*phi
, bool backedges_varying_p
)
4920 struct vn_phi_s
*vp1
;
4924 vp1
= XALLOCAVAR (struct vn_phi_s
,
4925 sizeof (struct vn_phi_s
)
4926 + (gimple_phi_num_args (phi
) - 1) * sizeof (tree
));
4928 /* Canonicalize the SSA_NAME's to their value number. */
4929 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
4931 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4932 if (TREE_CODE (def
) == SSA_NAME
4933 && (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
)))
4935 if (!virtual_operand_p (def
)
4936 && ssa_undefined_value_p (def
, false))
4939 def
= SSA_VAL (def
);
4941 vp1
->phiargs
[e
->dest_idx
] = def
;
4943 vp1
->type
= TREE_TYPE (gimple_phi_result (phi
));
4944 vp1
->block
= gimple_bb (phi
);
4945 /* Extract values of the controlling condition. */
4946 vp1
->cclhs
= NULL_TREE
;
4947 vp1
->ccrhs
= NULL_TREE
;
4948 if (EDGE_COUNT (vp1
->block
->preds
) == 2
4949 && vp1
->block
->loop_father
->header
!= vp1
->block
)
4951 basic_block idom1
= get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
4952 if (EDGE_COUNT (idom1
->succs
) == 2)
4953 if (gcond
*last1
= safe_dyn_cast
<gcond
*> (*gsi_last_bb (idom1
)))
4955 /* ??? We want to use SSA_VAL here. But possibly not
4957 vp1
->cclhs
= vn_valueize (gimple_cond_lhs (last1
));
4958 vp1
->ccrhs
= vn_valueize (gimple_cond_rhs (last1
));
4961 vp1
->hashcode
= vn_phi_compute_hash (vp1
);
4962 slot
= valid_info
->phis
->find_slot_with_hash (vp1
, vp1
->hashcode
, NO_INSERT
);
4965 return (*slot
)->result
;
4968 /* Insert PHI into the current hash table with a value number of
4972 vn_phi_insert (gimple
*phi
, tree result
, bool backedges_varying_p
)
4975 vn_phi_t vp1
= (vn_phi_t
) obstack_alloc (&vn_tables_obstack
,
4977 + ((gimple_phi_num_args (phi
) - 1)
4982 /* Canonicalize the SSA_NAME's to their value number. */
4983 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
4985 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4986 if (TREE_CODE (def
) == SSA_NAME
4987 && (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
)))
4989 if (!virtual_operand_p (def
)
4990 && ssa_undefined_value_p (def
, false))
4993 def
= SSA_VAL (def
);
4995 vp1
->phiargs
[e
->dest_idx
] = def
;
4997 vp1
->value_id
= VN_INFO (result
)->value_id
;
4998 vp1
->type
= TREE_TYPE (gimple_phi_result (phi
));
4999 vp1
->block
= gimple_bb (phi
);
5000 /* Extract values of the controlling condition. */
5001 vp1
->cclhs
= NULL_TREE
;
5002 vp1
->ccrhs
= NULL_TREE
;
5003 if (EDGE_COUNT (vp1
->block
->preds
) == 2
5004 && vp1
->block
->loop_father
->header
!= vp1
->block
)
5006 basic_block idom1
= get_immediate_dominator (CDI_DOMINATORS
, vp1
->block
);
5007 if (EDGE_COUNT (idom1
->succs
) == 2)
5008 if (gcond
*last1
= safe_dyn_cast
<gcond
*> (*gsi_last_bb (idom1
)))
5010 /* ??? We want to use SSA_VAL here. But possibly not
5012 vp1
->cclhs
= vn_valueize (gimple_cond_lhs (last1
));
5013 vp1
->ccrhs
= vn_valueize (gimple_cond_rhs (last1
));
5016 vp1
->result
= result
;
5017 vp1
->hashcode
= vn_phi_compute_hash (vp1
);
5019 slot
= valid_info
->phis
->find_slot_with_hash (vp1
, vp1
->hashcode
, INSERT
);
5020 gcc_assert (!*slot
);
5023 vp1
->next
= last_inserted_phi
;
5024 last_inserted_phi
= vp1
;
5029 /* Return true if BB1 is dominated by BB2 taking into account edges
5030 that are not executable. When ALLOW_BACK is false consider not
5031 executable backedges as executable. */
5034 dominated_by_p_w_unex (basic_block bb1
, basic_block bb2
, bool allow_back
)
5039 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
5042 /* Before iterating we'd like to know if there exists a
5043 (executable) path from bb2 to bb1 at all, if not we can
5044 directly return false. For now simply iterate once. */
5046 /* Iterate to the single executable bb1 predecessor. */
5047 if (EDGE_COUNT (bb1
->preds
) > 1)
5050 FOR_EACH_EDGE (e
, ei
, bb1
->preds
)
5051 if ((e
->flags
& EDGE_EXECUTABLE
)
5052 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
)))
5065 /* Re-do the dominance check with changed bb1. */
5066 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
5071 /* Iterate to the single executable bb2 successor. */
5072 if (EDGE_COUNT (bb2
->succs
) > 1)
5075 FOR_EACH_EDGE (e
, ei
, bb2
->succs
)
5076 if ((e
->flags
& EDGE_EXECUTABLE
)
5077 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
)))
5088 /* Verify the reached block is only reached through succe.
5089 If there is only one edge we can spare us the dominator
5090 check and iterate directly. */
5091 if (EDGE_COUNT (succe
->dest
->preds
) > 1)
5093 FOR_EACH_EDGE (e
, ei
, succe
->dest
->preds
)
5095 && ((e
->flags
& EDGE_EXECUTABLE
)
5096 || (!allow_back
&& (e
->flags
& EDGE_DFS_BACK
))))
5106 /* Re-do the dominance check with changed bb2. */
5107 if (dominated_by_p (CDI_DOMINATORS
, bb1
, bb2
))
5113 /* We could now iterate updating bb1 / bb2. */
5117 /* Set the value number of FROM to TO, return true if it has changed
5121 set_ssa_val_to (tree from
, tree to
)
5123 vn_ssa_aux_t from_info
= VN_INFO (from
);
5124 tree currval
= from_info
->valnum
; // SSA_VAL (from)
5125 poly_int64 toff
, coff
;
5126 bool curr_undefined
= false;
5127 bool curr_invariant
= false;
5129 /* The only thing we allow as value numbers are ssa_names
5130 and invariants. So assert that here. We don't allow VN_TOP
5131 as visiting a stmt should produce a value-number other than
5133 ??? Still VN_TOP can happen for unreachable code, so force
5134 it to varying in that case. Not all code is prepared to
5135 get VN_TOP on valueization. */
5138 /* ??? When iterating and visiting PHI <undef, backedge-value>
5139 for the first time we rightfully get VN_TOP and we need to
5140 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5141 With SCCVN we were simply lucky we iterated the other PHI
5142 cycles first and thus visited the backedge-value DEF. */
5143 if (currval
== VN_TOP
)
5145 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5146 fprintf (dump_file
, "Forcing value number to varying on "
5147 "receiving VN_TOP\n");
5151 gcc_checking_assert (to
!= NULL_TREE
5152 && ((TREE_CODE (to
) == SSA_NAME
5153 && (to
== from
|| SSA_VAL (to
) == to
))
5154 || is_gimple_min_invariant (to
)));
5158 if (currval
== from
)
5160 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5162 fprintf (dump_file
, "Not changing value number of ");
5163 print_generic_expr (dump_file
, from
);
5164 fprintf (dump_file
, " from VARYING to ");
5165 print_generic_expr (dump_file
, to
);
5166 fprintf (dump_file
, "\n");
5170 curr_invariant
= is_gimple_min_invariant (currval
);
5171 curr_undefined
= (TREE_CODE (currval
) == SSA_NAME
5172 && !virtual_operand_p (currval
)
5173 && ssa_undefined_value_p (currval
, false));
5174 if (currval
!= VN_TOP
5177 && is_gimple_min_invariant (to
))
5179 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5181 fprintf (dump_file
, "Forcing VARYING instead of changing "
5182 "value number of ");
5183 print_generic_expr (dump_file
, from
);
5184 fprintf (dump_file
, " from ");
5185 print_generic_expr (dump_file
, currval
);
5186 fprintf (dump_file
, " (non-constant) to ");
5187 print_generic_expr (dump_file
, to
);
5188 fprintf (dump_file
, " (constant)\n");
5192 else if (currval
!= VN_TOP
5194 && TREE_CODE (to
) == SSA_NAME
5195 && !virtual_operand_p (to
)
5196 && ssa_undefined_value_p (to
, false))
5198 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5200 fprintf (dump_file
, "Forcing VARYING instead of changing "
5201 "value number of ");
5202 print_generic_expr (dump_file
, from
);
5203 fprintf (dump_file
, " from ");
5204 print_generic_expr (dump_file
, currval
);
5205 fprintf (dump_file
, " (non-undefined) to ");
5206 print_generic_expr (dump_file
, to
);
5207 fprintf (dump_file
, " (undefined)\n");
5211 else if (TREE_CODE (to
) == SSA_NAME
5212 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to
))
5217 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5219 fprintf (dump_file
, "Setting value number of ");
5220 print_generic_expr (dump_file
, from
);
5221 fprintf (dump_file
, " to ");
5222 print_generic_expr (dump_file
, to
);
5226 && !operand_equal_p (currval
, to
, 0)
5227 /* Different undefined SSA names are not actually different. See
5228 PR82320 for a testcase were we'd otherwise not terminate iteration. */
5230 && TREE_CODE (to
) == SSA_NAME
5231 && !virtual_operand_p (to
)
5232 && ssa_undefined_value_p (to
, false))
5233 /* ??? For addresses involving volatile objects or types operand_equal_p
5234 does not reliably detect ADDR_EXPRs as equal. We know we are only
5235 getting invariant gimple addresses here, so can use
5236 get_addr_base_and_unit_offset to do this comparison. */
5237 && !(TREE_CODE (currval
) == ADDR_EXPR
5238 && TREE_CODE (to
) == ADDR_EXPR
5239 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval
, 0), &coff
)
5240 == get_addr_base_and_unit_offset (TREE_OPERAND (to
, 0), &toff
))
5241 && known_eq (coff
, toff
)))
5244 && currval
!= VN_TOP
5246 /* We do not want to allow lattice transitions from one value
5247 to another since that may lead to not terminating iteration
5248 (see PR95049). Since there's no convenient way to check
5249 for the allowed transition of VAL -> PHI (loop entry value,
5250 same on two PHIs, to same PHI result) we restrict the check
5253 && is_gimple_min_invariant (to
))
5255 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5256 fprintf (dump_file
, " forced VARYING");
5259 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5260 fprintf (dump_file
, " (changed)\n");
5261 from_info
->valnum
= to
;
5264 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5265 fprintf (dump_file
, "\n");
5269 /* Set all definitions in STMT to value number to themselves.
5270 Return true if a value number changed. */
5273 defs_to_varying (gimple
*stmt
)
5275 bool changed
= false;
5279 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_ALL_DEFS
)
5281 tree def
= DEF_FROM_PTR (defp
);
5282 changed
|= set_ssa_val_to (def
, def
);
5287 /* Visit a copy between LHS and RHS, return true if the value number
5291 visit_copy (tree lhs
, tree rhs
)
5294 rhs
= SSA_VAL (rhs
);
5296 return set_ssa_val_to (lhs
, rhs
);
5299 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5303 valueized_wider_op (tree wide_type
, tree op
, bool allow_truncate
)
5305 if (TREE_CODE (op
) == SSA_NAME
)
5306 op
= vn_valueize (op
);
5308 /* Either we have the op widened available. */
5311 tree tem
= vn_nary_op_lookup_pieces (1, NOP_EXPR
,
5312 wide_type
, ops
, NULL
);
5316 /* Or the op is truncated from some existing value. */
5317 if (allow_truncate
&& TREE_CODE (op
) == SSA_NAME
)
5319 gimple
*def
= SSA_NAME_DEF_STMT (op
);
5320 if (is_gimple_assign (def
)
5321 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def
)))
5323 tem
= gimple_assign_rhs1 (def
);
5324 if (useless_type_conversion_p (wide_type
, TREE_TYPE (tem
)))
5326 if (TREE_CODE (tem
) == SSA_NAME
)
5327 tem
= vn_valueize (tem
);
5333 /* For constants simply extend it. */
5334 if (TREE_CODE (op
) == INTEGER_CST
)
5335 return wide_int_to_tree (wide_type
, wi::to_widest (op
));
5340 /* Visit a nary operator RHS, value number it, and return true if the
5341 value number of LHS has changed as a result. */
5344 visit_nary_op (tree lhs
, gassign
*stmt
)
5346 vn_nary_op_t vnresult
;
5347 tree result
= vn_nary_op_lookup_stmt (stmt
, &vnresult
);
5348 if (! result
&& vnresult
)
5349 result
= vn_nary_op_get_predicated_value (vnresult
, gimple_bb (stmt
));
5351 return set_ssa_val_to (lhs
, result
);
5353 /* Do some special pattern matching for redundancies of operations
5354 in different types. */
5355 enum tree_code code
= gimple_assign_rhs_code (stmt
);
5356 tree type
= TREE_TYPE (lhs
);
5357 tree rhs1
= gimple_assign_rhs1 (stmt
);
5361 /* Match arithmetic done in a different type where we can easily
5362 substitute the result from some earlier sign-changed or widened
5364 if (INTEGRAL_TYPE_P (type
)
5365 && TREE_CODE (rhs1
) == SSA_NAME
5366 /* We only handle sign-changes, zero-extension -> & mask or
5367 sign-extension if we know the inner operation doesn't
5369 && (((TYPE_UNSIGNED (TREE_TYPE (rhs1
))
5370 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
5371 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1
))))
5372 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
5373 || TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (rhs1
))))
5375 gassign
*def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (rhs1
));
5377 && (gimple_assign_rhs_code (def
) == PLUS_EXPR
5378 || gimple_assign_rhs_code (def
) == MINUS_EXPR
5379 || gimple_assign_rhs_code (def
) == MULT_EXPR
))
5382 /* When requiring a sign-extension we cannot model a
5383 previous truncation with a single op so don't bother. */
5384 bool allow_truncate
= TYPE_UNSIGNED (TREE_TYPE (rhs1
));
5385 /* Either we have the op widened available. */
5386 ops
[0] = valueized_wider_op (type
, gimple_assign_rhs1 (def
),
5389 ops
[1] = valueized_wider_op (type
, gimple_assign_rhs2 (def
),
5391 if (ops
[0] && ops
[1])
5393 ops
[0] = vn_nary_op_lookup_pieces
5394 (2, gimple_assign_rhs_code (def
), type
, ops
, NULL
);
5395 /* We have wider operation available. */
5397 /* If the leader is a wrapping operation we can
5398 insert it for code hoisting w/o introducing
5399 undefined overflow. If it is not it has to
5400 be available. See PR86554. */
5401 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops
[0]))
5402 || (rpo_avail
&& vn_context_bb
5403 && rpo_avail
->eliminate_avail (vn_context_bb
,
5406 unsigned lhs_prec
= TYPE_PRECISION (type
);
5407 unsigned rhs_prec
= TYPE_PRECISION (TREE_TYPE (rhs1
));
5408 if (lhs_prec
== rhs_prec
5409 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
5410 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1
))))
5412 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5413 NOP_EXPR
, type
, ops
[0]);
5414 result
= vn_nary_build_or_lookup (&match_op
);
5417 bool changed
= set_ssa_val_to (lhs
, result
);
5418 vn_nary_op_insert_stmt (stmt
, result
);
5424 tree mask
= wide_int_to_tree
5425 (type
, wi::mask (rhs_prec
, false, lhs_prec
));
5426 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5430 result
= vn_nary_build_or_lookup (&match_op
);
5433 bool changed
= set_ssa_val_to (lhs
, result
);
5434 vn_nary_op_insert_stmt (stmt
, result
);
5444 if (INTEGRAL_TYPE_P (type
)
5445 && TREE_CODE (rhs1
) == SSA_NAME
5446 && TREE_CODE (gimple_assign_rhs2 (stmt
)) == INTEGER_CST
5447 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
)
5448 && default_vn_walk_kind
!= VN_NOWALK
5450 && BITS_PER_UNIT
== 8
5451 && BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
5452 && TYPE_PRECISION (type
) <= vn_walk_cb_data::bufsize
* BITS_PER_UNIT
5453 && !integer_all_onesp (gimple_assign_rhs2 (stmt
))
5454 && !integer_zerop (gimple_assign_rhs2 (stmt
)))
5456 gassign
*ass
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (rhs1
));
5458 && !gimple_has_volatile_ops (ass
)
5459 && vn_get_stmt_kind (ass
) == VN_REFERENCE
)
5461 tree last_vuse
= gimple_vuse (ass
);
5462 tree op
= gimple_assign_rhs1 (ass
);
5463 tree result
= vn_reference_lookup (op
, gimple_vuse (ass
),
5464 default_vn_walk_kind
,
5465 NULL
, true, &last_vuse
,
5466 gimple_assign_rhs2 (stmt
));
5468 && useless_type_conversion_p (TREE_TYPE (result
),
5470 return set_ssa_val_to (lhs
, result
);
5474 case TRUNC_DIV_EXPR
:
5475 if (TYPE_UNSIGNED (type
))
5480 /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5481 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type
))
5485 rhs
[1] = gimple_assign_rhs2 (stmt
);
5486 for (unsigned i
= 0; i
<= 1; ++i
)
5488 unsigned j
= i
== 0 ? 1 : 0;
5490 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5491 NEGATE_EXPR
, type
, rhs
[i
]);
5492 ops
[i
] = vn_nary_build_or_lookup_1 (&match_op
, false, true);
5495 && (ops
[0] = vn_nary_op_lookup_pieces (2, code
,
5498 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5499 NEGATE_EXPR
, type
, ops
[0]);
5500 result
= vn_nary_build_or_lookup_1 (&match_op
, true, false);
5503 bool changed
= set_ssa_val_to (lhs
, result
);
5504 vn_nary_op_insert_stmt (stmt
, result
);
5512 /* For X << C, use the value number of X * (1 << C). */
5513 if (INTEGRAL_TYPE_P (type
)
5514 && TYPE_OVERFLOW_WRAPS (type
)
5515 && !TYPE_SATURATING (type
))
5517 tree rhs2
= gimple_assign_rhs2 (stmt
);
5518 if (TREE_CODE (rhs2
) == INTEGER_CST
5519 && tree_fits_uhwi_p (rhs2
)
5520 && tree_to_uhwi (rhs2
) < TYPE_PRECISION (type
))
5522 wide_int w
= wi::set_bit_in_zero (tree_to_uhwi (rhs2
),
5523 TYPE_PRECISION (type
));
5524 gimple_match_op
match_op (gimple_match_cond::UNCOND
,
5525 MULT_EXPR
, type
, rhs1
,
5526 wide_int_to_tree (type
, w
));
5527 result
= vn_nary_build_or_lookup (&match_op
);
5530 bool changed
= set_ssa_val_to (lhs
, result
);
5531 if (TREE_CODE (result
) == SSA_NAME
)
5532 vn_nary_op_insert_stmt (stmt
, result
);
5542 bool changed
= set_ssa_val_to (lhs
, lhs
);
5543 vn_nary_op_insert_stmt (stmt
, lhs
);
5547 /* Visit a call STMT storing into LHS. Return true if the value number
5548 of the LHS has changed as a result. */
5551 visit_reference_op_call (tree lhs
, gcall
*stmt
)
5553 bool changed
= false;
5554 struct vn_reference_s vr1
;
5555 vn_reference_t vnresult
= NULL
;
5556 tree vdef
= gimple_vdef (stmt
);
5557 modref_summary
*summary
;
5559 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5560 if (lhs
&& TREE_CODE (lhs
) != SSA_NAME
)
5563 vn_reference_lookup_call (stmt
, &vnresult
, &vr1
);
5565 /* If the lookup did not succeed for pure functions try to use
5566 modref info to find a candidate to CSE to. */
5567 const unsigned accesses_limit
= 8;
5571 && gimple_vuse (stmt
)
5572 && (((summary
= get_modref_function_summary (stmt
, NULL
))
5573 && !summary
->global_memory_read
5574 && summary
->load_accesses
< accesses_limit
)
5575 || gimple_call_flags (stmt
) & ECF_CONST
))
5577 /* First search if we can do someting useful and build a
5578 vector of all loads we have to check. */
5579 bool unknown_memory_access
= false;
5580 auto_vec
<ao_ref
, accesses_limit
> accesses
;
5581 unsigned load_accesses
= summary
? summary
->load_accesses
: 0;
5582 if (!unknown_memory_access
)
5583 /* Add loads done as part of setting up the call arguments.
5584 That's also necessary for CONST functions which will
5585 not have a modref summary. */
5586 for (unsigned i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
5588 tree arg
= gimple_call_arg (stmt
, i
);
5589 if (TREE_CODE (arg
) != SSA_NAME
5590 && !is_gimple_min_invariant (arg
))
5592 if (accesses
.length () >= accesses_limit
- load_accesses
)
5594 unknown_memory_access
= true;
5597 accesses
.quick_grow (accesses
.length () + 1);
5598 ao_ref_init (&accesses
.last (), arg
);
5601 if (summary
&& !unknown_memory_access
)
5603 /* Add loads as analyzed by IPA modref. */
5604 for (auto base_node
: summary
->loads
->bases
)
5605 if (unknown_memory_access
)
5607 else for (auto ref_node
: base_node
->refs
)
5608 if (unknown_memory_access
)
5610 else for (auto access_node
: ref_node
->accesses
)
5612 accesses
.quick_grow (accesses
.length () + 1);
5613 ao_ref
*r
= &accesses
.last ();
5614 if (!access_node
.get_ao_ref (stmt
, r
))
5616 /* Initialize a ref based on the argument and
5617 unknown offset if possible. */
5618 tree arg
= access_node
.get_call_arg (stmt
);
5619 if (arg
&& TREE_CODE (arg
) == SSA_NAME
)
5620 arg
= SSA_VAL (arg
);
5622 && TREE_CODE (arg
) == ADDR_EXPR
5623 && (arg
= get_base_address (arg
))
5626 ao_ref_init (r
, arg
);
5632 unknown_memory_access
= true;
5636 r
->base_alias_set
= base_node
->base
;
5637 r
->ref_alias_set
= ref_node
->ref
;
5641 /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5642 for the call in the hashtable. */
5643 unsigned limit
= (unknown_memory_access
5645 : (param_sccvn_max_alias_queries_per_access
5646 / (accesses
.length () + 1)));
5647 tree saved_vuse
= vr1
.vuse
;
5648 hashval_t saved_hashcode
= vr1
.hashcode
;
5649 while (limit
> 0 && !vnresult
&& !SSA_NAME_IS_DEFAULT_DEF (vr1
.vuse
))
5651 vr1
.hashcode
= vr1
.hashcode
- SSA_NAME_VERSION (vr1
.vuse
);
5652 gimple
*def
= SSA_NAME_DEF_STMT (vr1
.vuse
);
5653 /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5654 do not bother for now. */
5655 if (is_a
<gphi
*> (def
))
5657 vr1
.vuse
= vuse_ssa_val (gimple_vuse (def
));
5658 vr1
.hashcode
= vr1
.hashcode
+ SSA_NAME_VERSION (vr1
.vuse
);
5659 vn_reference_lookup_1 (&vr1
, &vnresult
);
5663 /* If we found a candidate to CSE to verify it is valid. */
5664 if (vnresult
&& !accesses
.is_empty ())
5666 tree vuse
= vuse_ssa_val (gimple_vuse (stmt
));
5667 while (vnresult
&& vuse
!= vr1
.vuse
)
5669 gimple
*def
= SSA_NAME_DEF_STMT (vuse
);
5670 for (auto &ref
: accesses
)
5672 /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5673 analysis overhead that we might be able to cache. */
5674 if (stmt_may_clobber_ref_p_1 (def
, &ref
, true))
5680 vuse
= vuse_ssa_val (gimple_vuse (def
));
5683 vr1
.vuse
= saved_vuse
;
5684 vr1
.hashcode
= saved_hashcode
;
5691 if (vnresult
->result_vdef
)
5692 changed
|= set_ssa_val_to (vdef
, vnresult
->result_vdef
);
5693 else if (!lhs
&& gimple_call_lhs (stmt
))
5694 /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5695 as the call still acts as a lhs store. */
5696 changed
|= set_ssa_val_to (vdef
, vdef
);
5698 /* If the call was discovered to be pure or const reflect
5699 that as far as possible. */
5700 changed
|= set_ssa_val_to (vdef
,
5701 vuse_ssa_val (gimple_vuse (stmt
)));
5704 if (!vnresult
->result
&& lhs
)
5705 vnresult
->result
= lhs
;
5707 if (vnresult
->result
&& lhs
)
5708 changed
|= set_ssa_val_to (lhs
, vnresult
->result
);
5713 vn_reference_s
**slot
;
5714 tree vdef_val
= vdef
;
5717 /* If we value numbered an indirect functions function to
5718 one not clobbering memory value number its VDEF to its
5720 tree fn
= gimple_call_fn (stmt
);
5721 if (fn
&& TREE_CODE (fn
) == SSA_NAME
)
5724 if (TREE_CODE (fn
) == ADDR_EXPR
5725 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
5726 && (flags_from_decl_or_type (TREE_OPERAND (fn
, 0))
5727 & (ECF_CONST
| ECF_PURE
))
5728 /* If stmt has non-SSA_NAME lhs, value number the
5729 vdef to itself, as the call still acts as a lhs
5731 && (lhs
|| gimple_call_lhs (stmt
) == NULL_TREE
))
5732 vdef_val
= vuse_ssa_val (gimple_vuse (stmt
));
5734 changed
|= set_ssa_val_to (vdef
, vdef_val
);
5737 changed
|= set_ssa_val_to (lhs
, lhs
);
5738 vr2
= XOBNEW (&vn_tables_obstack
, vn_reference_s
);
5739 vr2
->vuse
= vr1
.vuse
;
5740 /* As we are not walking the virtual operand chain we know the
5741 shared_lookup_references are still original so we can re-use
5743 vr2
->operands
= vr1
.operands
.copy ();
5744 vr2
->type
= vr1
.type
;
5745 vr2
->punned
= vr1
.punned
;
5747 vr2
->offset
= vr1
.offset
;
5748 vr2
->max_size
= vr1
.max_size
;
5749 vr2
->base_set
= vr1
.base_set
;
5750 vr2
->hashcode
= vr1
.hashcode
;
5752 vr2
->result_vdef
= vdef_val
;
5754 slot
= valid_info
->references
->find_slot_with_hash (vr2
, vr2
->hashcode
,
5756 gcc_assert (!*slot
);
5758 vr2
->next
= last_inserted_ref
;
5759 last_inserted_ref
= vr2
;
5765 /* Visit a load from a reference operator RHS, part of STMT, value number it,
5766 and return true if the value number of the LHS has changed as a result. */
5769 visit_reference_op_load (tree lhs
, tree op
, gimple
*stmt
)
5771 bool changed
= false;
5775 tree vuse
= gimple_vuse (stmt
);
5776 tree last_vuse
= vuse
;
5777 result
= vn_reference_lookup (op
, vuse
, default_vn_walk_kind
, &res
, true, &last_vuse
);
5779 /* We handle type-punning through unions by value-numbering based
5780 on offset and size of the access. Be prepared to handle a
5781 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5783 && !useless_type_conversion_p (TREE_TYPE (result
), TREE_TYPE (op
)))
5785 if (CONSTANT_CLASS_P (result
))
5786 result
= const_unop (VIEW_CONVERT_EXPR
, TREE_TYPE (op
), result
);
5789 /* We will be setting the value number of lhs to the value number
5790 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5791 So first simplify and lookup this expression to see if it
5792 is already available. */
5793 gimple_match_op
res_op (gimple_match_cond::UNCOND
,
5794 VIEW_CONVERT_EXPR
, TREE_TYPE (op
), result
);
5795 result
= vn_nary_build_or_lookup (&res_op
);
5797 && TREE_CODE (result
) == SSA_NAME
5798 && VN_INFO (result
)->needs_insertion
)
5799 /* Track whether this is the canonical expression for different
5800 typed loads. We use that as a stopgap measure for code
5801 hoisting when dealing with floating point loads. */
5805 /* When building the conversion fails avoid inserting the reference
5808 return set_ssa_val_to (lhs
, lhs
);
5812 changed
= set_ssa_val_to (lhs
, result
);
5815 changed
= set_ssa_val_to (lhs
, lhs
);
5816 vn_reference_insert (op
, lhs
, last_vuse
, NULL_TREE
);
5817 if (vuse
&& SSA_VAL (last_vuse
) != SSA_VAL (vuse
))
5819 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5821 fprintf (dump_file
, "Using extra use virtual operand ");
5822 print_generic_expr (dump_file
, last_vuse
);
5823 fprintf (dump_file
, "\n");
5825 vn_reference_insert (op
, lhs
, vuse
, NULL_TREE
);
5833 /* Visit a store to a reference operator LHS, part of STMT, value number it,
5834 and return true if the value number of the LHS has changed as a result. */
5837 visit_reference_op_store (tree lhs
, tree op
, gimple
*stmt
)
5839 bool changed
= false;
5840 vn_reference_t vnresult
= NULL
;
5842 bool resultsame
= false;
5843 tree vuse
= gimple_vuse (stmt
);
5844 tree vdef
= gimple_vdef (stmt
);
5846 if (TREE_CODE (op
) == SSA_NAME
)
5849 /* First we want to lookup using the *vuses* from the store and see
5850 if there the last store to this location with the same address
5853 The vuses represent the memory state before the store. If the
5854 memory state, address, and value of the store is the same as the
5855 last store to this location, then this store will produce the
5856 same memory state as that store.
5858 In this case the vdef versions for this store are value numbered to those
5859 vuse versions, since they represent the same memory state after
5862 Otherwise, the vdefs for the store are used when inserting into
5863 the table, since the store generates a new memory state. */
5865 vn_reference_lookup (lhs
, vuse
, VN_NOWALK
, &vnresult
, false);
5867 && vnresult
->result
)
5869 tree result
= vnresult
->result
;
5870 gcc_checking_assert (TREE_CODE (result
) != SSA_NAME
5871 || result
== SSA_VAL (result
));
5872 resultsame
= expressions_equal_p (result
, op
);
5875 /* If the TBAA state isn't compatible for downstream reads
5876 we cannot value-number the VDEFs the same. */
5878 ao_ref_init (&lhs_ref
, lhs
);
5879 alias_set_type set
= ao_ref_alias_set (&lhs_ref
);
5880 alias_set_type base_set
= ao_ref_base_alias_set (&lhs_ref
);
5881 if ((vnresult
->set
!= set
5882 && ! alias_set_subset_of (set
, vnresult
->set
))
5883 || (vnresult
->base_set
!= base_set
5884 && ! alias_set_subset_of (base_set
, vnresult
->base_set
)))
5891 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5893 fprintf (dump_file
, "No store match\n");
5894 fprintf (dump_file
, "Value numbering store ");
5895 print_generic_expr (dump_file
, lhs
);
5896 fprintf (dump_file
, " to ");
5897 print_generic_expr (dump_file
, op
);
5898 fprintf (dump_file
, "\n");
5900 /* Have to set value numbers before insert, since insert is
5901 going to valueize the references in-place. */
5903 changed
|= set_ssa_val_to (vdef
, vdef
);
5905 /* Do not insert structure copies into the tables. */
5906 if (is_gimple_min_invariant (op
)
5907 || is_gimple_reg (op
))
5908 vn_reference_insert (lhs
, op
, vdef
, NULL
);
5910 /* Only perform the following when being called from PRE
5911 which embeds tail merging. */
5912 if (default_vn_walk_kind
== VN_WALK
)
5914 assign
= build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, op
);
5915 vn_reference_lookup (assign
, vuse
, VN_NOWALK
, &vnresult
, false);
5917 vn_reference_insert (assign
, lhs
, vuse
, vdef
);
5922 /* We had a match, so value number the vdef to have the value
5923 number of the vuse it came from. */
5925 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5926 fprintf (dump_file
, "Store matched earlier value, "
5927 "value numbering store vdefs to matching vuses.\n");
5929 changed
|= set_ssa_val_to (vdef
, SSA_VAL (vuse
));
5935 /* Visit and value number PHI, return true if the value number
5936 changed. When BACKEDGES_VARYING_P is true then assume all
5937 backedge values are varying. When INSERTED is not NULL then
5938 this is just a ahead query for a possible iteration, set INSERTED
5939 to true if we'd insert into the hashtable. */
5942 visit_phi (gimple
*phi
, bool *inserted
, bool backedges_varying_p
)
5944 tree result
, sameval
= VN_TOP
, seen_undef
= NULL_TREE
;
5945 bool seen_undef_visited
= false;
5946 tree backedge_val
= NULL_TREE
;
5947 bool seen_non_backedge
= false;
5948 tree sameval_base
= NULL_TREE
;
5949 poly_int64 soff
, doff
;
5950 unsigned n_executable
= 0;
5952 edge e
, sameval_e
= NULL
;
5954 /* TODO: We could check for this in initialization, and replace this
5955 with a gcc_assert. */
5956 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi
)))
5957 return set_ssa_val_to (PHI_RESULT (phi
), PHI_RESULT (phi
));
5959 /* We track whether a PHI was CSEd to avoid excessive iterations
5960 that would be necessary only because the PHI changed arguments
5963 gimple_set_plf (phi
, GF_PLF_1
, false);
5965 /* See if all non-TOP arguments have the same value. TOP is
5966 equivalent to everything, so we can ignore it. */
5967 basic_block bb
= gimple_bb (phi
);
5968 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5969 if (e
->flags
& EDGE_EXECUTABLE
)
5971 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
5973 if (def
== PHI_RESULT (phi
))
5976 bool visited
= true;
5977 if (TREE_CODE (def
) == SSA_NAME
)
5979 tree val
= SSA_VAL (def
, &visited
);
5980 if (SSA_NAME_IS_DEFAULT_DEF (def
))
5982 if (!backedges_varying_p
|| !(e
->flags
& EDGE_DFS_BACK
))
5984 if (e
->flags
& EDGE_DFS_BACK
)
5987 if (!(e
->flags
& EDGE_DFS_BACK
))
5988 seen_non_backedge
= true;
5991 /* Ignore undefined defs for sameval but record one. */
5992 else if (TREE_CODE (def
) == SSA_NAME
5993 && ! virtual_operand_p (def
)
5994 && ssa_undefined_value_p (def
, false))
5997 /* Avoid having not visited undefined defs if we also have
5999 || (!seen_undef_visited
&& visited
))
6002 seen_undef_visited
= visited
;
6005 else if (sameval
== VN_TOP
)
6010 else if (expressions_equal_p (def
, sameval
))
6012 else if (virtual_operand_p (def
))
6014 sameval
= NULL_TREE
;
6019 /* We know we're arriving only with invariant addresses here,
6020 try harder comparing them. We can do some caching here
6021 which we cannot do in expressions_equal_p. */
6022 if (TREE_CODE (def
) == ADDR_EXPR
6023 && TREE_CODE (sameval
) == ADDR_EXPR
6024 && sameval_base
!= (void *)-1)
6027 sameval_base
= get_addr_base_and_unit_offset
6028 (TREE_OPERAND (sameval
, 0), &soff
);
6030 sameval_base
= (tree
)(void *)-1;
6031 else if ((get_addr_base_and_unit_offset
6032 (TREE_OPERAND (def
, 0), &doff
) == sameval_base
)
6033 && known_eq (soff
, doff
))
6036 /* There's also the possibility to use equivalences. */
6037 if (!FLOAT_TYPE_P (TREE_TYPE (def
))
6038 /* But only do this if we didn't force any of sameval or
6039 val to VARYING because of backedge processing rules. */
6040 && (TREE_CODE (sameval
) != SSA_NAME
6041 || SSA_VAL (sameval
) == sameval
)
6042 && (TREE_CODE (def
) != SSA_NAME
|| SSA_VAL (def
) == def
))
6044 vn_nary_op_t vnresult
;
6048 tree val
= vn_nary_op_lookup_pieces (2, EQ_EXPR
,
6051 if (! val
&& vnresult
&& vnresult
->predicated_values
)
6053 val
= vn_nary_op_get_predicated_value (vnresult
, e
);
6054 if (val
&& integer_truep (val
)
6055 && !(sameval_e
&& (sameval_e
->flags
& EDGE_DFS_BACK
)))
6057 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6059 fprintf (dump_file
, "Predication says ");
6060 print_generic_expr (dump_file
, def
, TDF_NONE
);
6061 fprintf (dump_file
, " and ");
6062 print_generic_expr (dump_file
, sameval
, TDF_NONE
);
6063 fprintf (dump_file
, " are equal on edge %d -> %d\n",
6064 e
->src
->index
, e
->dest
->index
);
6068 /* If on all previous edges the value was equal to def
6069 we can change sameval to def. */
6070 if (EDGE_COUNT (bb
->preds
) == 2
6071 && (val
= vn_nary_op_get_predicated_value
6072 (vnresult
, EDGE_PRED (bb
, 0)))
6073 && integer_truep (val
)
6074 && !(e
->flags
& EDGE_DFS_BACK
))
6076 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6078 fprintf (dump_file
, "Predication says ");
6079 print_generic_expr (dump_file
, def
, TDF_NONE
);
6080 fprintf (dump_file
, " and ");
6081 print_generic_expr (dump_file
, sameval
, TDF_NONE
);
6082 fprintf (dump_file
, " are equal on edge %d -> %d\n",
6083 EDGE_PRED (bb
, 0)->src
->index
,
6084 EDGE_PRED (bb
, 0)->dest
->index
);
6091 sameval
= NULL_TREE
;
6096 /* If the value we want to use is flowing over the backedge and we
6097 should take it as VARYING but it has a non-VARYING value drop to
6099 If we value-number a virtual operand never value-number to the
6100 value from the backedge as that confuses the alias-walking code.
6101 See gcc.dg/torture/pr87176.c. If the value is the same on a
6102 non-backedge everything is OK though. */
6105 && !seen_non_backedge
6106 && TREE_CODE (backedge_val
) == SSA_NAME
6107 && sameval
== backedge_val
6108 && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val
)
6109 || SSA_VAL (backedge_val
) != backedge_val
))
6110 /* Do not value-number a virtual operand to sth not visited though
6111 given that allows us to escape a region in alias walking. */
6113 && TREE_CODE (sameval
) == SSA_NAME
6114 && !SSA_NAME_IS_DEFAULT_DEF (sameval
)
6115 && SSA_NAME_IS_VIRTUAL_OPERAND (sameval
)
6116 && (SSA_VAL (sameval
, &visited_p
), !visited_p
)))
6117 /* Note this just drops to VARYING without inserting the PHI into
6119 result
= PHI_RESULT (phi
);
6120 /* If none of the edges was executable keep the value-number at VN_TOP,
6121 if only a single edge is exectuable use its value. */
6122 else if (n_executable
<= 1)
6123 result
= seen_undef
? seen_undef
: sameval
;
6124 /* If we saw only undefined values and VN_TOP use one of the
6125 undefined values. */
6126 else if (sameval
== VN_TOP
)
6127 result
= (seen_undef
&& seen_undef_visited
) ? seen_undef
: sameval
;
6128 /* First see if it is equivalent to a phi node in this block. We prefer
6129 this as it allows IV elimination - see PRs 66502 and 67167. */
6130 else if ((result
= vn_phi_lookup (phi
, backedges_varying_p
)))
6133 && TREE_CODE (result
) == SSA_NAME
6134 && gimple_code (SSA_NAME_DEF_STMT (result
)) == GIMPLE_PHI
)
6136 gimple_set_plf (SSA_NAME_DEF_STMT (result
), GF_PLF_1
, true);
6137 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6139 fprintf (dump_file
, "Marking CSEd to PHI node ");
6140 print_gimple_expr (dump_file
, SSA_NAME_DEF_STMT (result
),
6142 fprintf (dump_file
, "\n");
6146 /* If all values are the same use that, unless we've seen undefined
6147 values as well and the value isn't constant.
6148 CCP/copyprop have the same restriction to not remove uninit warnings. */
6150 && (! seen_undef
|| is_gimple_min_invariant (sameval
)))
6154 result
= PHI_RESULT (phi
);
6155 /* Only insert PHIs that are varying, for constant value numbers
6156 we mess up equivalences otherwise as we are only comparing
6157 the immediate controlling predicates. */
6158 vn_phi_insert (phi
, result
, backedges_varying_p
);
6163 return set_ssa_val_to (PHI_RESULT (phi
), result
);
6166 /* Try to simplify RHS using equivalences and constant folding. */
6169 try_to_simplify (gassign
*stmt
)
6171 enum tree_code code
= gimple_assign_rhs_code (stmt
);
6174 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6175 in this case, there is no point in doing extra work. */
6176 if (code
== SSA_NAME
)
6179 /* First try constant folding based on our current lattice. */
6180 mprts_hook
= vn_lookup_simplify_result
;
6181 tem
= gimple_fold_stmt_to_constant_1 (stmt
, vn_valueize
, vn_valueize
);
6184 && (TREE_CODE (tem
) == SSA_NAME
6185 || is_gimple_min_invariant (tem
)))
6191 /* Visit and value number STMT, return true if the value number
6195 visit_stmt (gimple
*stmt
, bool backedges_varying_p
= false)
6197 bool changed
= false;
6199 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6201 fprintf (dump_file
, "Value numbering stmt = ");
6202 print_gimple_stmt (dump_file
, stmt
, 0);
6205 if (gimple_code (stmt
) == GIMPLE_PHI
)
6206 changed
= visit_phi (stmt
, NULL
, backedges_varying_p
);
6207 else if (gimple_has_volatile_ops (stmt
))
6208 changed
= defs_to_varying (stmt
);
6209 else if (gassign
*ass
= dyn_cast
<gassign
*> (stmt
))
6211 enum tree_code code
= gimple_assign_rhs_code (ass
);
6212 tree lhs
= gimple_assign_lhs (ass
);
6213 tree rhs1
= gimple_assign_rhs1 (ass
);
6216 /* Shortcut for copies. Simplifying copies is pointless,
6217 since we copy the expression and value they represent. */
6218 if (code
== SSA_NAME
6219 && TREE_CODE (lhs
) == SSA_NAME
)
6221 changed
= visit_copy (lhs
, rhs1
);
6224 simplified
= try_to_simplify (ass
);
6227 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6229 fprintf (dump_file
, "RHS ");
6230 print_gimple_expr (dump_file
, ass
, 0);
6231 fprintf (dump_file
, " simplified to ");
6232 print_generic_expr (dump_file
, simplified
);
6233 fprintf (dump_file
, "\n");
6236 /* Setting value numbers to constants will occasionally
6237 screw up phi congruence because constants are not
6238 uniquely associated with a single ssa name that can be
6241 && is_gimple_min_invariant (simplified
)
6242 && TREE_CODE (lhs
) == SSA_NAME
)
6244 changed
= set_ssa_val_to (lhs
, simplified
);
6248 && TREE_CODE (simplified
) == SSA_NAME
6249 && TREE_CODE (lhs
) == SSA_NAME
)
6251 changed
= visit_copy (lhs
, simplified
);
6255 if ((TREE_CODE (lhs
) == SSA_NAME
6256 /* We can substitute SSA_NAMEs that are live over
6257 abnormal edges with their constant value. */
6258 && !(gimple_assign_copy_p (ass
)
6259 && is_gimple_min_invariant (rhs1
))
6261 && is_gimple_min_invariant (simplified
))
6262 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
6263 /* Stores or copies from SSA_NAMEs that are live over
6264 abnormal edges are a problem. */
6265 || (code
== SSA_NAME
6266 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
)))
6267 changed
= defs_to_varying (ass
);
6268 else if (REFERENCE_CLASS_P (lhs
)
6270 changed
= visit_reference_op_store (lhs
, rhs1
, ass
);
6271 else if (TREE_CODE (lhs
) == SSA_NAME
)
6273 if ((gimple_assign_copy_p (ass
)
6274 && is_gimple_min_invariant (rhs1
))
6276 && is_gimple_min_invariant (simplified
)))
6279 changed
= set_ssa_val_to (lhs
, simplified
);
6281 changed
= set_ssa_val_to (lhs
, rhs1
);
6285 /* Visit the original statement. */
6286 switch (vn_get_stmt_kind (ass
))
6289 changed
= visit_nary_op (lhs
, ass
);
6292 changed
= visit_reference_op_load (lhs
, rhs1
, ass
);
6295 changed
= defs_to_varying (ass
);
6301 changed
= defs_to_varying (ass
);
6303 else if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
6305 tree lhs
= gimple_call_lhs (call_stmt
);
6306 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6308 /* Try constant folding based on our current lattice. */
6309 tree simplified
= gimple_fold_stmt_to_constant_1 (call_stmt
,
6313 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6315 fprintf (dump_file
, "call ");
6316 print_gimple_expr (dump_file
, call_stmt
, 0);
6317 fprintf (dump_file
, " simplified to ");
6318 print_generic_expr (dump_file
, simplified
);
6319 fprintf (dump_file
, "\n");
6322 /* Setting value numbers to constants will occasionally
6323 screw up phi congruence because constants are not
6324 uniquely associated with a single ssa name that can be
6327 && is_gimple_min_invariant (simplified
))
6329 changed
= set_ssa_val_to (lhs
, simplified
);
6330 if (gimple_vdef (call_stmt
))
6331 changed
|= set_ssa_val_to (gimple_vdef (call_stmt
),
6332 SSA_VAL (gimple_vuse (call_stmt
)));
6336 && TREE_CODE (simplified
) == SSA_NAME
)
6338 changed
= visit_copy (lhs
, simplified
);
6339 if (gimple_vdef (call_stmt
))
6340 changed
|= set_ssa_val_to (gimple_vdef (call_stmt
),
6341 SSA_VAL (gimple_vuse (call_stmt
)));
6344 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
6346 changed
= defs_to_varying (call_stmt
);
6351 /* Pick up flags from a devirtualization target. */
6352 tree fn
= gimple_call_fn (stmt
);
6353 int extra_fnflags
= 0;
6354 if (fn
&& TREE_CODE (fn
) == SSA_NAME
)
6357 if (TREE_CODE (fn
) == ADDR_EXPR
6358 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
)
6359 extra_fnflags
= flags_from_decl_or_type (TREE_OPERAND (fn
, 0));
6361 if ((/* Calls to the same function with the same vuse
6362 and the same operands do not necessarily return the same
6363 value, unless they're pure or const. */
6364 ((gimple_call_flags (call_stmt
) | extra_fnflags
)
6365 & (ECF_PURE
| ECF_CONST
))
6366 /* If calls have a vdef, subsequent calls won't have
6367 the same incoming vuse. So, if 2 calls with vdef have the
6368 same vuse, we know they're not subsequent.
6369 We can value number 2 calls to the same function with the
6370 same vuse and the same operands which are not subsequent
6371 the same, because there is no code in the program that can
6372 compare the 2 values... */
6373 || (gimple_vdef (call_stmt
)
6374 /* ... unless the call returns a pointer which does
6375 not alias with anything else. In which case the
6376 information that the values are distinct are encoded
6378 && !(gimple_call_return_flags (call_stmt
) & ERF_NOALIAS
)
6379 /* Only perform the following when being called from PRE
6380 which embeds tail merging. */
6381 && default_vn_walk_kind
== VN_WALK
))
6382 /* Do not process .DEFERRED_INIT since that confuses uninit
6384 && !gimple_call_internal_p (call_stmt
, IFN_DEFERRED_INIT
))
6385 changed
= visit_reference_op_call (lhs
, call_stmt
);
6387 changed
= defs_to_varying (call_stmt
);
6390 changed
= defs_to_varying (stmt
);
6396 /* Allocate a value number table. */
6399 allocate_vn_table (vn_tables_t table
, unsigned size
)
6401 table
->phis
= new vn_phi_table_type (size
);
6402 table
->nary
= new vn_nary_op_table_type (size
);
6403 table
->references
= new vn_reference_table_type (size
);
6406 /* Free a value number table. */
6409 free_vn_table (vn_tables_t table
)
6411 /* Walk over elements and release vectors. */
6412 vn_reference_iterator_type hir
;
6414 FOR_EACH_HASH_TABLE_ELEMENT (*table
->references
, vr
, vn_reference_t
, hir
)
6415 vr
->operands
.release ();
6420 delete table
->references
;
6421 table
->references
= NULL
;
6424 /* Set *ID according to RESULT. */
6427 set_value_id_for_result (tree result
, unsigned int *id
)
6429 if (result
&& TREE_CODE (result
) == SSA_NAME
)
6430 *id
= VN_INFO (result
)->value_id
;
6431 else if (result
&& is_gimple_min_invariant (result
))
6432 *id
= get_or_alloc_constant_value_id (result
);
6434 *id
= get_next_value_id ();
6437 /* Set the value ids in the valid hash tables. */
6440 set_hashtable_value_ids (void)
6442 vn_nary_op_iterator_type hin
;
6443 vn_phi_iterator_type hip
;
6444 vn_reference_iterator_type hir
;
6449 /* Now set the value ids of the things we had put in the hash
6452 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->nary
, vno
, vn_nary_op_t
, hin
)
6453 if (! vno
->predicated_values
)
6454 set_value_id_for_result (vno
->u
.result
, &vno
->value_id
);
6456 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->phis
, vp
, vn_phi_t
, hip
)
6457 set_value_id_for_result (vp
->result
, &vp
->value_id
);
6459 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info
->references
, vr
, vn_reference_t
,
6461 set_value_id_for_result (vr
->result
, &vr
->value_id
);
6464 /* Return the maximum value id we have ever seen. */
6467 get_max_value_id (void)
6469 return next_value_id
;
6472 /* Return the maximum constant value id we have ever seen. */
6475 get_max_constant_value_id (void)
6477 return -next_constant_value_id
;
6480 /* Return the next unique value id. */
6483 get_next_value_id (void)
6485 gcc_checking_assert ((int)next_value_id
> 0);
6486 return next_value_id
++;
6489 /* Return the next unique value id for constants. */
6492 get_next_constant_value_id (void)
6494 gcc_checking_assert (next_constant_value_id
< 0);
6495 return next_constant_value_id
--;
6499 /* Compare two expressions E1 and E2 and return true if they are equal.
6500 If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6501 otherwise VN_TOP only matches VN_TOP. */
6504 expressions_equal_p (tree e1
, tree e2
, bool match_vn_top_optimistically
)
6506 /* The obvious case. */
6510 /* If either one is VN_TOP consider them equal. */
6511 if (match_vn_top_optimistically
6512 && (e1
== VN_TOP
|| e2
== VN_TOP
))
6515 /* If only one of them is null, they cannot be equal. While in general
6516 this should not happen for operations like TARGET_MEM_REF some
6517 operands are optional and an identity value we could substitute
6518 has differing semantics. */
6522 /* SSA_NAME compare pointer equal. */
6523 if (TREE_CODE (e1
) == SSA_NAME
|| TREE_CODE (e2
) == SSA_NAME
)
6526 /* Now perform the actual comparison. */
6527 if (TREE_CODE (e1
) == TREE_CODE (e2
)
6528 && operand_equal_p (e1
, e2
, OEP_PURE_SAME
))
6535 /* Return true if the nary operation NARY may trap. This is a copy
6536 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6539 vn_nary_may_trap (vn_nary_op_t nary
)
6542 tree rhs2
= NULL_TREE
;
6543 bool honor_nans
= false;
6544 bool honor_snans
= false;
6545 bool fp_operation
= false;
6546 bool honor_trapv
= false;
6550 if (TREE_CODE_CLASS (nary
->opcode
) == tcc_comparison
6551 || TREE_CODE_CLASS (nary
->opcode
) == tcc_unary
6552 || TREE_CODE_CLASS (nary
->opcode
) == tcc_binary
)
6555 fp_operation
= FLOAT_TYPE_P (type
);
6558 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
6559 honor_snans
= flag_signaling_nans
!= 0;
6561 else if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_TRAPS (type
))
6564 if (nary
->length
>= 2)
6566 ret
= operation_could_trap_helper_p (nary
->opcode
, fp_operation
,
6567 honor_trapv
, honor_nans
, honor_snans
,
6572 for (i
= 0; i
< nary
->length
; ++i
)
6573 if (tree_could_trap_p (nary
->op
[i
]))
6579 /* Return true if the reference operation REF may trap. */
6582 vn_reference_may_trap (vn_reference_t ref
)
6584 switch (ref
->operands
[0].opcode
)
6588 /* We do not handle calls. */
6591 /* And toplevel address computations never trap. */
6596 vn_reference_op_t op
;
6598 FOR_EACH_VEC_ELT (ref
->operands
, i
, op
)
6602 case WITH_SIZE_EXPR
:
6603 case TARGET_MEM_REF
:
6604 /* Always variable. */
6607 if (op
->op1
&& TREE_CODE (op
->op1
) == SSA_NAME
)
6610 case ARRAY_RANGE_REF
:
6611 if (TREE_CODE (op
->op0
) == SSA_NAME
)
6616 if (TREE_CODE (op
->op0
) != INTEGER_CST
)
6619 /* !in_array_bounds */
6620 tree domain_type
= TYPE_DOMAIN (ref
->operands
[i
+1].type
);
6625 tree max
= TYPE_MAX_VALUE (domain_type
);
6628 || TREE_CODE (min
) != INTEGER_CST
6629 || TREE_CODE (max
) != INTEGER_CST
)
6632 if (tree_int_cst_lt (op
->op0
, min
)
6633 || tree_int_cst_lt (max
, op
->op0
))
6639 /* Nothing interesting in itself, the base is separate. */
6641 /* The following are the address bases. */
6646 return tree_could_trap_p (TREE_OPERAND (op
->op0
, 0));
6654 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction
,
6655 bitmap inserted_exprs_
)
6656 : dom_walker (direction
), do_pre (inserted_exprs_
!= NULL
),
6657 el_todo (0), eliminations (0), insertions (0),
6658 inserted_exprs (inserted_exprs_
)
6660 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
6661 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
6664 eliminate_dom_walker::~eliminate_dom_walker ()
6666 BITMAP_FREE (need_eh_cleanup
);
6667 BITMAP_FREE (need_ab_cleanup
);
6670 /* Return a leader for OP that is available at the current point of the
6671 eliminate domwalk. */
6674 eliminate_dom_walker::eliminate_avail (basic_block
, tree op
)
6676 tree valnum
= VN_INFO (op
)->valnum
;
6677 if (TREE_CODE (valnum
) == SSA_NAME
)
6679 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
6681 if (avail
.length () > SSA_NAME_VERSION (valnum
))
6683 tree av
= avail
[SSA_NAME_VERSION (valnum
)];
6684 /* When PRE discovers a new redundancy there's no way to unite
6685 the value classes so it instead inserts a copy old-val = new-val.
6686 Look through such copies here, providing one more level of
6687 simplification at elimination time. */
6689 if (av
&& (ass
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (av
))))
6690 if (gimple_assign_rhs_class (ass
) == GIMPLE_SINGLE_RHS
)
6692 tree rhs1
= gimple_assign_rhs1 (ass
);
6693 if (CONSTANT_CLASS_P (rhs1
)
6694 || (TREE_CODE (rhs1
) == SSA_NAME
6695 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1
)))
6701 else if (is_gimple_min_invariant (valnum
))
6706 /* At the current point of the eliminate domwalk make OP available. */
6709 eliminate_dom_walker::eliminate_push_avail (basic_block
, tree op
)
6711 tree valnum
= VN_INFO (op
)->valnum
;
6712 if (TREE_CODE (valnum
) == SSA_NAME
)
6714 if (avail
.length () <= SSA_NAME_VERSION (valnum
))
6715 avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1, true);
6717 if (avail
[SSA_NAME_VERSION (valnum
)])
6718 pushop
= avail
[SSA_NAME_VERSION (valnum
)];
6719 avail_stack
.safe_push (pushop
);
6720 avail
[SSA_NAME_VERSION (valnum
)] = op
;
6724 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
6725 the leader for the expression if insertion was successful. */
6728 eliminate_dom_walker::eliminate_insert (basic_block bb
,
6729 gimple_stmt_iterator
*gsi
, tree val
)
6731 /* We can insert a sequence with a single assignment only. */
6732 gimple_seq stmts
= VN_INFO (val
)->expr
;
6733 if (!gimple_seq_singleton_p (stmts
))
6735 gassign
*stmt
= dyn_cast
<gassign
*> (gimple_seq_first_stmt (stmts
));
6737 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
6738 && gimple_assign_rhs_code (stmt
) != VIEW_CONVERT_EXPR
6739 && gimple_assign_rhs_code (stmt
) != NEGATE_EXPR
6740 && gimple_assign_rhs_code (stmt
) != BIT_FIELD_REF
6741 && (gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6742 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)))
6745 tree op
= gimple_assign_rhs1 (stmt
);
6746 if (gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
6747 || gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
6748 op
= TREE_OPERAND (op
, 0);
6749 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (bb
, op
) : op
;
6755 if (gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
6756 res
= gimple_build (&stmts
, BIT_FIELD_REF
,
6757 TREE_TYPE (val
), leader
,
6758 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1),
6759 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2));
6760 else if (gimple_assign_rhs_code (stmt
) == BIT_AND_EXPR
)
6761 res
= gimple_build (&stmts
, BIT_AND_EXPR
,
6762 TREE_TYPE (val
), leader
, gimple_assign_rhs2 (stmt
));
6764 res
= gimple_build (&stmts
, gimple_assign_rhs_code (stmt
),
6765 TREE_TYPE (val
), leader
);
6766 if (TREE_CODE (res
) != SSA_NAME
6767 || SSA_NAME_IS_DEFAULT_DEF (res
)
6768 || gimple_bb (SSA_NAME_DEF_STMT (res
)))
6770 gimple_seq_discard (stmts
);
6772 /* During propagation we have to treat SSA info conservatively
6773 and thus we can end up simplifying the inserted expression
6774 at elimination time to sth not defined in stmts. */
6775 /* But then this is a redundancy we failed to detect. Which means
6776 res now has two values. That doesn't play well with how
6777 we track availability here, so give up. */
6778 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6780 if (TREE_CODE (res
) == SSA_NAME
)
6781 res
= eliminate_avail (bb
, res
);
6784 fprintf (dump_file
, "Failed to insert expression for value ");
6785 print_generic_expr (dump_file
, val
);
6786 fprintf (dump_file
, " which is really fully redundant to ");
6787 print_generic_expr (dump_file
, res
);
6788 fprintf (dump_file
, "\n");
6796 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
6797 vn_ssa_aux_t vn_info
= VN_INFO (res
);
6798 vn_info
->valnum
= val
;
6799 vn_info
->visited
= true;
6803 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6805 fprintf (dump_file
, "Inserted ");
6806 print_gimple_stmt (dump_file
, SSA_NAME_DEF_STMT (res
), 0);
6813 eliminate_dom_walker::eliminate_stmt (basic_block b
, gimple_stmt_iterator
*gsi
)
6815 tree sprime
= NULL_TREE
;
6816 gimple
*stmt
= gsi_stmt (*gsi
);
6817 tree lhs
= gimple_get_lhs (stmt
);
6818 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6819 && !gimple_has_volatile_ops (stmt
)
6820 /* See PR43491. Do not replace a global register variable when
6821 it is a the RHS of an assignment. Do replace local register
6822 variables since gcc does not guarantee a local variable will
6823 be allocated in register.
6824 ??? The fix isn't effective here. This should instead
6825 be ensured by not value-numbering them the same but treating
6826 them like volatiles? */
6827 && !(gimple_assign_single_p (stmt
)
6828 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
6829 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
6830 && is_global_var (gimple_assign_rhs1 (stmt
)))))
6832 sprime
= eliminate_avail (b
, lhs
);
6835 /* If there is no existing usable leader but SCCVN thinks
6836 it has an expression it wants to use as replacement,
6838 tree val
= VN_INFO (lhs
)->valnum
;
6839 vn_ssa_aux_t vn_info
;
6841 && TREE_CODE (val
) == SSA_NAME
6842 && (vn_info
= VN_INFO (val
), true)
6843 && vn_info
->needs_insertion
6844 && vn_info
->expr
!= NULL
6845 && (sprime
= eliminate_insert (b
, gsi
, val
)) != NULL_TREE
)
6846 eliminate_push_avail (b
, sprime
);
6849 /* If this now constitutes a copy duplicate points-to
6850 and range info appropriately. This is especially
6851 important for inserted code. */
6853 && TREE_CODE (sprime
) == SSA_NAME
)
6854 maybe_duplicate_ssa_info_at_copy (lhs
, sprime
);
6856 /* Inhibit the use of an inserted PHI on a loop header when
6857 the address of the memory reference is a simple induction
6858 variable. In other cases the vectorizer won't do anything
6859 anyway (either it's loop invariant or a complicated
6862 && TREE_CODE (sprime
) == SSA_NAME
6864 && (flag_tree_loop_vectorize
|| flag_tree_parallelize_loops
> 1)
6865 && loop_outer (b
->loop_father
)
6866 && has_zero_uses (sprime
)
6867 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
6868 && gimple_assign_load_p (stmt
))
6870 gimple
*def_stmt
= SSA_NAME_DEF_STMT (sprime
);
6871 basic_block def_bb
= gimple_bb (def_stmt
);
6872 if (gimple_code (def_stmt
) == GIMPLE_PHI
6873 && def_bb
->loop_father
->header
== def_bb
)
6875 loop_p loop
= def_bb
->loop_father
;
6879 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
6882 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
6884 && flow_bb_inside_loop_p (loop
, def_bb
)
6885 && simple_iv (loop
, loop
, op
, &iv
, true))
6893 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6895 fprintf (dump_file
, "Not replacing ");
6896 print_gimple_expr (dump_file
, stmt
, 0);
6897 fprintf (dump_file
, " with ");
6898 print_generic_expr (dump_file
, sprime
);
6899 fprintf (dump_file
, " which would add a loop"
6900 " carried dependence to loop %d\n",
6903 /* Don't keep sprime available. */
6911 /* If we can propagate the value computed for LHS into
6912 all uses don't bother doing anything with this stmt. */
6913 if (may_propagate_copy (lhs
, sprime
))
6915 /* Mark it for removal. */
6916 to_remove
.safe_push (stmt
);
6918 /* ??? Don't count copy/constant propagations. */
6919 if (gimple_assign_single_p (stmt
)
6920 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
6921 || gimple_assign_rhs1 (stmt
) == sprime
))
6924 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6926 fprintf (dump_file
, "Replaced ");
6927 print_gimple_expr (dump_file
, stmt
, 0);
6928 fprintf (dump_file
, " with ");
6929 print_generic_expr (dump_file
, sprime
);
6930 fprintf (dump_file
, " in all uses of ");
6931 print_gimple_stmt (dump_file
, stmt
, 0);
6938 /* If this is an assignment from our leader (which
6939 happens in the case the value-number is a constant)
6940 then there is nothing to do. Likewise if we run into
6941 inserted code that needed a conversion because of
6942 our type-agnostic value-numbering of loads. */
6943 if ((gimple_assign_single_p (stmt
)
6944 || (is_gimple_assign (stmt
)
6945 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
6946 || gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
)))
6947 && sprime
== gimple_assign_rhs1 (stmt
))
6950 /* Else replace its RHS. */
6951 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6953 fprintf (dump_file
, "Replaced ");
6954 print_gimple_expr (dump_file
, stmt
, 0);
6955 fprintf (dump_file
, " with ");
6956 print_generic_expr (dump_file
, sprime
);
6957 fprintf (dump_file
, " in ");
6958 print_gimple_stmt (dump_file
, stmt
, 0);
6962 bool can_make_abnormal_goto
= (is_gimple_call (stmt
)
6963 && stmt_can_make_abnormal_goto (stmt
));
6964 gimple
*orig_stmt
= stmt
;
6965 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
6966 TREE_TYPE (sprime
)))
6968 /* We preserve conversions to but not from function or method
6969 types. This asymmetry makes it necessary to re-instantiate
6970 conversions here. */
6971 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
6972 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs
))))
6973 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
6977 tree vdef
= gimple_vdef (stmt
);
6978 tree vuse
= gimple_vuse (stmt
);
6979 propagate_tree_value_into_stmt (gsi
, sprime
);
6980 stmt
= gsi_stmt (*gsi
);
6982 /* In case the VDEF on the original stmt was released, value-number
6983 it to the VUSE. This is to make vuse_ssa_val able to skip
6984 released virtual operands. */
6985 if (vdef
!= gimple_vdef (stmt
))
6987 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef
));
6988 VN_INFO (vdef
)->valnum
= vuse
;
6991 /* If we removed EH side-effects from the statement, clean
6992 its EH information. */
6993 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
6995 bitmap_set_bit (need_eh_cleanup
,
6996 gimple_bb (stmt
)->index
);
6997 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6998 fprintf (dump_file
, " Removed EH side-effects.\n");
7001 /* Likewise for AB side-effects. */
7002 if (can_make_abnormal_goto
7003 && !stmt_can_make_abnormal_goto (stmt
))
7005 bitmap_set_bit (need_ab_cleanup
,
7006 gimple_bb (stmt
)->index
);
7007 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7008 fprintf (dump_file
, " Removed AB side-effects.\n");
7015 /* If the statement is a scalar store, see if the expression
7016 has the same value number as its rhs. If so, the store is
7018 if (gimple_assign_single_p (stmt
)
7019 && !gimple_has_volatile_ops (stmt
)
7020 && !is_gimple_reg (gimple_assign_lhs (stmt
))
7021 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
7022 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
7024 tree rhs
= gimple_assign_rhs1 (stmt
);
7025 vn_reference_t vnresult
;
7026 /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7027 typed load of a byte known to be 0x11 as 1 so a store of
7028 a boolean 1 is detected as redundant. Because of this we
7029 have to make sure to lookup with a ref where its size
7030 matches the precision. */
7031 tree lookup_lhs
= lhs
;
7032 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7033 && (TREE_CODE (lhs
) != COMPONENT_REF
7034 || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs
, 1)))
7035 && !type_has_mode_precision_p (TREE_TYPE (lhs
)))
7037 if (TREE_CODE (TREE_TYPE (lhs
)) == BITINT_TYPE
7038 && TYPE_PRECISION (TREE_TYPE (lhs
)) > MAX_FIXED_MODE_SIZE
)
7039 lookup_lhs
= NULL_TREE
;
7040 else if (TREE_CODE (lhs
) == COMPONENT_REF
7041 || TREE_CODE (lhs
) == MEM_REF
)
7043 tree ltype
= build_nonstandard_integer_type
7044 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs
))),
7045 TYPE_UNSIGNED (TREE_TYPE (lhs
)));
7046 if (TREE_CODE (lhs
) == COMPONENT_REF
)
7048 tree foff
= component_ref_field_offset (lhs
);
7049 tree f
= TREE_OPERAND (lhs
, 1);
7050 if (!poly_int_tree_p (foff
))
7051 lookup_lhs
= NULL_TREE
;
7053 lookup_lhs
= build3 (BIT_FIELD_REF
, ltype
,
7054 TREE_OPERAND (lhs
, 0),
7055 TYPE_SIZE (TREE_TYPE (lhs
)),
7057 (foff
, DECL_FIELD_BIT_OFFSET (f
)));
7060 lookup_lhs
= build2 (MEM_REF
, ltype
,
7061 TREE_OPERAND (lhs
, 0),
7062 TREE_OPERAND (lhs
, 1));
7065 lookup_lhs
= NULL_TREE
;
7067 tree val
= NULL_TREE
;
7069 val
= vn_reference_lookup (lookup_lhs
, gimple_vuse (stmt
),
7070 VN_WALKREWRITE
, &vnresult
, false,
7071 NULL
, NULL_TREE
, true);
7072 if (TREE_CODE (rhs
) == SSA_NAME
)
7073 rhs
= VN_INFO (rhs
)->valnum
;
7075 && (operand_equal_p (val
, rhs
, 0)
7076 /* Due to the bitfield lookups above we can get bit
7077 interpretations of the same RHS as values here. Those
7078 are redundant as well. */
7079 || (TREE_CODE (val
) == SSA_NAME
7080 && gimple_assign_single_p (SSA_NAME_DEF_STMT (val
))
7081 && (val
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val
)))
7082 && TREE_CODE (val
) == VIEW_CONVERT_EXPR
7083 && TREE_OPERAND (val
, 0) == rhs
)))
7085 /* We can only remove the later store if the former aliases
7086 at least all accesses the later one does or if the store
7087 was to readonly memory storing the same value. */
7089 ao_ref_init (&lhs_ref
, lhs
);
7090 alias_set_type set
= ao_ref_alias_set (&lhs_ref
);
7091 alias_set_type base_set
= ao_ref_base_alias_set (&lhs_ref
);
7093 || ((vnresult
->set
== set
7094 || alias_set_subset_of (set
, vnresult
->set
))
7095 && (vnresult
->base_set
== base_set
7096 || alias_set_subset_of (base_set
, vnresult
->base_set
))))
7098 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7100 fprintf (dump_file
, "Deleted redundant store ");
7101 print_gimple_stmt (dump_file
, stmt
, 0);
7104 /* Queue stmt for removal. */
7105 to_remove
.safe_push (stmt
);
7111 /* If this is a control statement value numbering left edges
7112 unexecuted on force the condition in a way consistent with
7114 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
7116 if ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
)
7117 ^ (EDGE_SUCC (b
, 1)->flags
& EDGE_EXECUTABLE
))
7119 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7121 fprintf (dump_file
, "Removing unexecutable edge from ");
7122 print_gimple_stmt (dump_file
, stmt
, 0);
7124 if (((EDGE_SUCC (b
, 0)->flags
& EDGE_TRUE_VALUE
) != 0)
7125 == ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
) != 0))
7126 gimple_cond_make_true (cond
);
7128 gimple_cond_make_false (cond
);
7130 el_todo
|= TODO_cleanup_cfg
;
7135 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
7136 bool was_noreturn
= (is_gimple_call (stmt
)
7137 && gimple_call_noreturn_p (stmt
));
7138 tree vdef
= gimple_vdef (stmt
);
7139 tree vuse
= gimple_vuse (stmt
);
7141 /* If we didn't replace the whole stmt (or propagate the result
7142 into all uses), replace all uses on this stmt with their
7144 bool modified
= false;
7145 use_operand_p use_p
;
7147 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
7149 tree use
= USE_FROM_PTR (use_p
);
7150 /* ??? The call code above leaves stmt operands un-updated. */
7151 if (TREE_CODE (use
) != SSA_NAME
)
7154 if (SSA_NAME_IS_DEFAULT_DEF (use
))
7155 /* ??? For default defs BB shouldn't matter, but we have to
7156 solve the inconsistency between rpo eliminate and
7157 dom eliminate avail valueization first. */
7158 sprime
= eliminate_avail (b
, use
);
7160 /* Look for sth available at the definition block of the argument.
7161 This avoids inconsistencies between availability there which
7162 decides if the stmt can be removed and availability at the
7163 use site. The SSA property ensures that things available
7164 at the definition are also available at uses. */
7165 sprime
= eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use
)), use
);
7166 if (sprime
&& sprime
!= use
7167 && may_propagate_copy (use
, sprime
, true)
7168 /* We substitute into debug stmts to avoid excessive
7169 debug temporaries created by removed stmts, but we need
7170 to avoid doing so for inserted sprimes as we never want
7171 to create debug temporaries for them. */
7173 || TREE_CODE (sprime
) != SSA_NAME
7174 || !is_gimple_debug (stmt
)
7175 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
7177 propagate_value (use_p
, sprime
);
7182 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7183 into which is a requirement for the IPA devirt machinery. */
7184 gimple
*old_stmt
= stmt
;
7187 /* If a formerly non-invariant ADDR_EXPR is turned into an
7188 invariant one it was on a separate stmt. */
7189 if (gimple_assign_single_p (stmt
)
7190 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
7191 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
7192 gimple_stmt_iterator prev
= *gsi
;
7194 if (fold_stmt (gsi
, follow_all_ssa_edges
))
7196 /* fold_stmt may have created new stmts inbetween
7197 the previous stmt and the folded stmt. Mark
7198 all defs created there as varying to not confuse
7199 the SCCVN machinery as we're using that even during
7201 if (gsi_end_p (prev
))
7202 prev
= gsi_start_bb (b
);
7205 if (gsi_stmt (prev
) != gsi_stmt (*gsi
))
7210 FOR_EACH_SSA_TREE_OPERAND (def
, gsi_stmt (prev
),
7211 dit
, SSA_OP_ALL_DEFS
)
7212 /* As existing DEFs may move between stmts
7213 only process new ones. */
7214 if (! has_VN_INFO (def
))
7216 vn_ssa_aux_t vn_info
= VN_INFO (def
);
7217 vn_info
->valnum
= def
;
7218 vn_info
->visited
= true;
7220 if (gsi_stmt (prev
) == gsi_stmt (*gsi
))
7226 stmt
= gsi_stmt (*gsi
);
7227 /* In case we folded the stmt away schedule the NOP for removal. */
7228 if (gimple_nop_p (stmt
))
7229 to_remove
.safe_push (stmt
);
7232 /* Visit indirect calls and turn them into direct calls if
7233 possible using the devirtualization machinery. Do this before
7234 checking for required EH/abnormal/noreturn cleanup as devird
7235 may expose more of those. */
7236 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
7238 tree fn
= gimple_call_fn (call_stmt
);
7240 && flag_devirtualize
7241 && virtual_method_call_p (fn
))
7243 tree otr_type
= obj_type_ref_class (fn
);
7244 unsigned HOST_WIDE_INT otr_tok
7245 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn
));
7247 ipa_polymorphic_call_context
context (current_function_decl
,
7248 fn
, stmt
, &instance
);
7249 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
),
7250 otr_type
, stmt
, NULL
);
7252 vec
<cgraph_node
*> targets
7253 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
7254 otr_tok
, context
, &final
);
7256 dump_possible_polymorphic_call_targets (dump_file
,
7257 obj_type_ref_class (fn
),
7259 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
7262 if (targets
.length () == 1)
7263 fn
= targets
[0]->decl
;
7265 fn
= builtin_decl_unreachable ();
7266 if (dump_enabled_p ())
7268 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
,
7269 "converting indirect call to "
7271 lang_hooks
.decl_printable_name (fn
, 2));
7273 gimple_call_set_fndecl (call_stmt
, fn
);
7274 /* If changing the call to __builtin_unreachable
7275 or similar noreturn function, adjust gimple_call_fntype
7277 if (gimple_call_noreturn_p (call_stmt
)
7278 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn
)))
7279 && TYPE_ARG_TYPES (TREE_TYPE (fn
))
7280 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn
)))
7282 gimple_call_set_fntype (call_stmt
, TREE_TYPE (fn
));
7283 maybe_remove_unused_call_args (cfun
, call_stmt
);
7291 /* When changing a call into a noreturn call, cfg cleanup
7292 is needed to fix up the noreturn call. */
7294 && is_gimple_call (stmt
) && gimple_call_noreturn_p (stmt
))
7295 to_fixup
.safe_push (stmt
);
7296 /* When changing a condition or switch into one we know what
7297 edge will be executed, schedule a cfg cleanup. */
7298 if ((gimple_code (stmt
) == GIMPLE_COND
7299 && (gimple_cond_true_p (as_a
<gcond
*> (stmt
))
7300 || gimple_cond_false_p (as_a
<gcond
*> (stmt
))))
7301 || (gimple_code (stmt
) == GIMPLE_SWITCH
7302 && TREE_CODE (gimple_switch_index
7303 (as_a
<gswitch
*> (stmt
))) == INTEGER_CST
))
7304 el_todo
|= TODO_cleanup_cfg
;
7305 /* If we removed EH side-effects from the statement, clean
7306 its EH information. */
7307 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
7309 bitmap_set_bit (need_eh_cleanup
,
7310 gimple_bb (stmt
)->index
);
7311 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7312 fprintf (dump_file
, " Removed EH side-effects.\n");
7314 /* Likewise for AB side-effects. */
7315 if (can_make_abnormal_goto
7316 && !stmt_can_make_abnormal_goto (stmt
))
7318 bitmap_set_bit (need_ab_cleanup
,
7319 gimple_bb (stmt
)->index
);
7320 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7321 fprintf (dump_file
, " Removed AB side-effects.\n");
7324 /* In case the VDEF on the original stmt was released, value-number
7325 it to the VUSE. This is to make vuse_ssa_val able to skip
7326 released virtual operands. */
7327 if (vdef
&& SSA_NAME_IN_FREE_LIST (vdef
))
7328 VN_INFO (vdef
)->valnum
= vuse
;
7331 /* Make new values available - for fully redundant LHS we
7332 continue with the next stmt above and skip this.
7333 But avoid picking up dead defs. */
7335 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7336 if (! has_zero_uses (def
)
7338 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (def
))))
7339 eliminate_push_avail (b
, def
);
7342 /* Perform elimination for the basic-block B during the domwalk. */
7345 eliminate_dom_walker::before_dom_children (basic_block b
)
7348 avail_stack
.safe_push (NULL_TREE
);
7350 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7351 if (!(b
->flags
& BB_EXECUTABLE
))
7356 for (gphi_iterator gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
7358 gphi
*phi
= gsi
.phi ();
7359 tree res
= PHI_RESULT (phi
);
7361 if (virtual_operand_p (res
))
7367 tree sprime
= eliminate_avail (b
, res
);
7371 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7373 fprintf (dump_file
, "Replaced redundant PHI node defining ");
7374 print_generic_expr (dump_file
, res
);
7375 fprintf (dump_file
, " with ");
7376 print_generic_expr (dump_file
, sprime
);
7377 fprintf (dump_file
, "\n");
7380 /* If we inserted this PHI node ourself, it's not an elimination. */
7381 if (! inserted_exprs
7382 || ! bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
7385 /* If we will propagate into all uses don't bother to do
7387 if (may_propagate_copy (res
, sprime
))
7389 /* Mark the PHI for removal. */
7390 to_remove
.safe_push (phi
);
7395 remove_phi_node (&gsi
, false);
7397 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
7398 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
7399 gimple
*stmt
= gimple_build_assign (res
, sprime
);
7400 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
7401 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
7405 eliminate_push_avail (b
, res
);
7409 for (gimple_stmt_iterator gsi
= gsi_start_bb (b
);
7412 eliminate_stmt (b
, &gsi
);
7414 /* Replace destination PHI arguments. */
7417 FOR_EACH_EDGE (e
, ei
, b
->succs
)
7418 if (e
->flags
& EDGE_EXECUTABLE
)
7419 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
7423 gphi
*phi
= gsi
.phi ();
7424 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
7425 tree arg
= USE_FROM_PTR (use_p
);
7426 if (TREE_CODE (arg
) != SSA_NAME
7427 || virtual_operand_p (arg
))
7429 tree sprime
= eliminate_avail (b
, arg
);
7430 if (sprime
&& may_propagate_copy (arg
, sprime
,
7431 !(e
->flags
& EDGE_ABNORMAL
)))
7432 propagate_value (use_p
, sprime
);
7435 vn_context_bb
= NULL
;
7440 /* Make no longer available leaders no longer available. */
7443 eliminate_dom_walker::after_dom_children (basic_block
)
7446 while ((entry
= avail_stack
.pop ()) != NULL_TREE
)
7448 tree valnum
= VN_INFO (entry
)->valnum
;
7449 tree old
= avail
[SSA_NAME_VERSION (valnum
)];
7451 avail
[SSA_NAME_VERSION (valnum
)] = NULL_TREE
;
7453 avail
[SSA_NAME_VERSION (valnum
)] = entry
;
7457 /* Remove queued stmts and perform delayed cleanups. */
7460 eliminate_dom_walker::eliminate_cleanup (bool region_p
)
7462 statistics_counter_event (cfun
, "Eliminated", eliminations
);
7463 statistics_counter_event (cfun
, "Insertions", insertions
);
7465 /* We cannot remove stmts during BB walk, especially not release SSA
7466 names there as this confuses the VN machinery. The stmts ending
7467 up in to_remove are either stores or simple copies.
7468 Remove stmts in reverse order to make debug stmt creation possible. */
7469 while (!to_remove
.is_empty ())
7471 bool do_release_defs
= true;
7472 gimple
*stmt
= to_remove
.pop ();
7474 /* When we are value-numbering a region we do not require exit PHIs to
7475 be present so we have to make sure to deal with uses outside of the
7476 region of stmts that we thought are eliminated.
7477 ??? Note we may be confused by uses in dead regions we didn't run
7478 elimination on. Rather than checking individual uses we accept
7479 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7480 contains such example). */
7483 if (gphi
*phi
= dyn_cast
<gphi
*> (stmt
))
7485 tree lhs
= gimple_phi_result (phi
);
7486 if (!has_zero_uses (lhs
))
7488 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7489 fprintf (dump_file
, "Keeping eliminated stmt live "
7490 "as copy because of out-of-region uses\n");
7491 tree sprime
= eliminate_avail (gimple_bb (stmt
), lhs
);
7492 gimple
*copy
= gimple_build_assign (lhs
, sprime
);
7493 gimple_stmt_iterator gsi
7494 = gsi_after_labels (gimple_bb (stmt
));
7495 gsi_insert_before (&gsi
, copy
, GSI_SAME_STMT
);
7496 do_release_defs
= false;
7499 else if (tree lhs
= gimple_get_lhs (stmt
))
7500 if (TREE_CODE (lhs
) == SSA_NAME
7501 && !has_zero_uses (lhs
))
7503 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7504 fprintf (dump_file
, "Keeping eliminated stmt live "
7505 "as copy because of out-of-region uses\n");
7506 tree sprime
= eliminate_avail (gimple_bb (stmt
), lhs
);
7507 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
7508 if (is_gimple_assign (stmt
))
7510 gimple_assign_set_rhs_from_tree (&gsi
, sprime
);
7511 stmt
= gsi_stmt (gsi
);
7513 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
))
7514 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
7519 gimple
*copy
= gimple_build_assign (lhs
, sprime
);
7520 gsi_insert_before (&gsi
, copy
, GSI_SAME_STMT
);
7521 do_release_defs
= false;
7526 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7528 fprintf (dump_file
, "Removing dead stmt ");
7529 print_gimple_stmt (dump_file
, stmt
, 0, TDF_NONE
);
7532 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
7533 if (gimple_code (stmt
) == GIMPLE_PHI
)
7534 remove_phi_node (&gsi
, do_release_defs
);
7537 basic_block bb
= gimple_bb (stmt
);
7538 unlink_stmt_vdef (stmt
);
7539 if (gsi_remove (&gsi
, true))
7540 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
7541 if (is_gimple_call (stmt
) && stmt_can_make_abnormal_goto (stmt
))
7542 bitmap_set_bit (need_ab_cleanup
, bb
->index
);
7543 if (do_release_defs
)
7544 release_defs (stmt
);
7547 /* Removing a stmt may expose a forwarder block. */
7548 el_todo
|= TODO_cleanup_cfg
;
7551 /* Fixup stmts that became noreturn calls. This may require splitting
7552 blocks and thus isn't possible during the dominator walk. Do this
7553 in reverse order so we don't inadvertedly remove a stmt we want to
7554 fixup by visiting a dominating now noreturn call first. */
7555 while (!to_fixup
.is_empty ())
7557 gimple
*stmt
= to_fixup
.pop ();
7559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7561 fprintf (dump_file
, "Fixing up noreturn call ");
7562 print_gimple_stmt (dump_file
, stmt
, 0);
7565 if (fixup_noreturn_call (stmt
))
7566 el_todo
|= TODO_cleanup_cfg
;
7569 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
7570 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
7573 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
7576 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
7578 if (do_eh_cleanup
|| do_ab_cleanup
)
7579 el_todo
|= TODO_cleanup_cfg
;
7584 /* Eliminate fully redundant computations. */
7587 eliminate_with_rpo_vn (bitmap inserted_exprs
)
7589 eliminate_dom_walker
walker (CDI_DOMINATORS
, inserted_exprs
);
7591 eliminate_dom_walker
*saved_rpo_avail
= rpo_avail
;
7592 rpo_avail
= &walker
;
7593 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
7594 rpo_avail
= saved_rpo_avail
;
7596 return walker
.eliminate_cleanup ();
7600 do_rpo_vn_1 (function
*fn
, edge entry
, bitmap exit_bbs
,
7601 bool iterate
, bool eliminate
, bool skip_entry_phis
,
7602 vn_lookup_kind kind
);
7605 run_rpo_vn (vn_lookup_kind kind
)
7607 do_rpo_vn_1 (cfun
, NULL
, NULL
, true, false, false, kind
);
7609 /* ??? Prune requirement of these. */
7610 constant_to_value_id
= new hash_table
<vn_constant_hasher
> (23);
7612 /* Initialize the value ids and prune out remaining VN_TOPs
7616 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7618 vn_ssa_aux_t info
= VN_INFO (name
);
7620 || info
->valnum
== VN_TOP
)
7621 info
->valnum
= name
;
7622 if (info
->valnum
== name
)
7623 info
->value_id
= get_next_value_id ();
7624 else if (is_gimple_min_invariant (info
->valnum
))
7625 info
->value_id
= get_or_alloc_constant_value_id (info
->valnum
);
7629 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7631 vn_ssa_aux_t info
= VN_INFO (name
);
7632 if (TREE_CODE (info
->valnum
) == SSA_NAME
7633 && info
->valnum
!= name
7634 && info
->value_id
!= VN_INFO (info
->valnum
)->value_id
)
7635 info
->value_id
= VN_INFO (info
->valnum
)->value_id
;
7638 set_hashtable_value_ids ();
7640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7642 fprintf (dump_file
, "Value numbers:\n");
7643 FOR_EACH_SSA_NAME (i
, name
, cfun
)
7645 if (VN_INFO (name
)->visited
7646 && SSA_VAL (name
) != name
)
7648 print_generic_expr (dump_file
, name
);
7649 fprintf (dump_file
, " = ");
7650 print_generic_expr (dump_file
, SSA_VAL (name
));
7651 fprintf (dump_file
, " (%04d)\n", VN_INFO (name
)->value_id
);
7657 /* Free VN associated data structures. */
7662 free_vn_table (valid_info
);
7663 XDELETE (valid_info
);
7664 obstack_free (&vn_tables_obstack
, NULL
);
7665 obstack_free (&vn_tables_insert_obstack
, NULL
);
7667 vn_ssa_aux_iterator_type it
;
7669 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash
, info
, vn_ssa_aux_t
, it
)
7670 if (info
->needs_insertion
)
7671 release_ssa_name (info
->name
);
7672 obstack_free (&vn_ssa_aux_obstack
, NULL
);
7673 delete vn_ssa_aux_hash
;
7675 delete constant_to_value_id
;
7676 constant_to_value_id
= NULL
;
7679 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7682 vn_lookup_simplify_result (gimple_match_op
*res_op
)
7684 if (!res_op
->code
.is_tree_code ())
7686 tree
*ops
= res_op
->ops
;
7687 unsigned int length
= res_op
->num_ops
;
7688 if (res_op
->code
== CONSTRUCTOR
7689 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7690 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7691 && TREE_CODE (res_op
->ops
[0]) == CONSTRUCTOR
)
7693 length
= CONSTRUCTOR_NELTS (res_op
->ops
[0]);
7694 ops
= XALLOCAVEC (tree
, length
);
7695 for (unsigned i
= 0; i
< length
; ++i
)
7696 ops
[i
] = CONSTRUCTOR_ELT (res_op
->ops
[0], i
)->value
;
7698 vn_nary_op_t vnresult
= NULL
;
7699 tree res
= vn_nary_op_lookup_pieces (length
, (tree_code
) res_op
->code
,
7700 res_op
->type
, ops
, &vnresult
);
7701 /* If this is used from expression simplification make sure to
7702 return an available expression. */
7703 if (res
&& TREE_CODE (res
) == SSA_NAME
&& mprts_hook
&& rpo_avail
)
7704 res
= rpo_avail
->eliminate_avail (vn_context_bb
, res
);
7708 /* Return a leader for OPs value that is valid at BB. */
7711 rpo_elim::eliminate_avail (basic_block bb
, tree op
)
7714 tree valnum
= SSA_VAL (op
, &visited
);
7715 /* If we didn't visit OP then it must be defined outside of the
7716 region we process and also dominate it. So it is available. */
7719 if (TREE_CODE (valnum
) == SSA_NAME
)
7721 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
7723 vn_ssa_aux_t valnum_info
= VN_INFO (valnum
);
7724 vn_avail
*av
= valnum_info
->avail
;
7727 /* See above. But when there's availability info prefer
7728 what we recorded there for example to preserve LC SSA. */
7729 if (!valnum_info
->visited
)
7733 if (av
->location
== bb
->index
)
7734 /* On tramp3d 90% of the cases are here. */
7735 return ssa_name (av
->leader
);
7738 basic_block abb
= BASIC_BLOCK_FOR_FN (cfun
, av
->location
);
7739 /* ??? During elimination we have to use availability at the
7740 definition site of a use we try to replace. This
7741 is required to not run into inconsistencies because
7742 of dominated_by_p_w_unex behavior and removing a definition
7743 while not replacing all uses.
7744 ??? We could try to consistently walk dominators
7745 ignoring non-executable regions. The nearest common
7746 dominator of bb and abb is where we can stop walking. We
7747 may also be able to "pre-compute" (bits of) the next immediate
7748 (non-)dominator during the RPO walk when marking edges as
7750 if (dominated_by_p_w_unex (bb
, abb
, true))
7752 tree leader
= ssa_name (av
->leader
);
7753 /* Prevent eliminations that break loop-closed SSA. */
7754 if (loops_state_satisfies_p (LOOP_CLOSED_SSA
)
7755 && ! SSA_NAME_IS_DEFAULT_DEF (leader
)
7756 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7757 (leader
))->loop_father
,
7760 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7762 print_generic_expr (dump_file
, leader
);
7763 fprintf (dump_file
, " is available for ");
7764 print_generic_expr (dump_file
, valnum
);
7765 fprintf (dump_file
, "\n");
7767 /* On tramp3d 99% of the _remaining_ cases succeed at
7771 /* ??? Can we somehow skip to the immediate dominator
7772 RPO index (bb_to_rpo)? Again, maybe not worth, on
7773 tramp3d the worst number of elements in the vector is 9. */
7777 /* While we prefer avail we have to fallback to using the value
7778 directly if defined outside of the region when none of the
7779 available defs suit. */
7780 if (!valnum_info
->visited
)
7783 else if (valnum
!= VN_TOP
)
7784 /* valnum is is_gimple_min_invariant. */
7789 /* Make LEADER a leader for its value at BB. */
7792 rpo_elim::eliminate_push_avail (basic_block bb
, tree leader
)
7794 tree valnum
= VN_INFO (leader
)->valnum
;
7795 if (valnum
== VN_TOP
7796 || is_gimple_min_invariant (valnum
))
7798 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7800 fprintf (dump_file
, "Making available beyond BB%d ", bb
->index
);
7801 print_generic_expr (dump_file
, leader
);
7802 fprintf (dump_file
, " for value ");
7803 print_generic_expr (dump_file
, valnum
);
7804 fprintf (dump_file
, "\n");
7806 vn_ssa_aux_t value
= VN_INFO (valnum
);
7808 if (m_avail_freelist
)
7810 av
= m_avail_freelist
;
7811 m_avail_freelist
= m_avail_freelist
->next
;
7814 av
= XOBNEW (&vn_ssa_aux_obstack
, vn_avail
);
7815 av
->location
= bb
->index
;
7816 av
->leader
= SSA_NAME_VERSION (leader
);
7817 av
->next
= value
->avail
;
7818 av
->next_undo
= last_pushed_avail
;
7819 last_pushed_avail
= value
;
7823 /* Valueization hook for RPO VN plus required state. */
7826 rpo_vn_valueize (tree name
)
7828 if (TREE_CODE (name
) == SSA_NAME
)
7830 vn_ssa_aux_t val
= VN_INFO (name
);
7833 tree tem
= val
->valnum
;
7834 if (tem
!= VN_TOP
&& tem
!= name
)
7836 if (TREE_CODE (tem
) != SSA_NAME
)
7838 /* For all values we only valueize to an available leader
7839 which means we can use SSA name info without restriction. */
7840 tem
= rpo_avail
->eliminate_avail (vn_context_bb
, tem
);
7849 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7850 inverted condition. */
7853 insert_related_predicates_on_edge (enum tree_code code
, tree
*ops
, edge pred_e
)
7858 /* a < b -> a {!,<}= b */
7859 vn_nary_op_insert_pieces_predicated (2, NE_EXPR
, boolean_type_node
,
7860 ops
, boolean_true_node
, 0, pred_e
);
7861 vn_nary_op_insert_pieces_predicated (2, LE_EXPR
, boolean_type_node
,
7862 ops
, boolean_true_node
, 0, pred_e
);
7863 /* a < b -> ! a {>,=} b */
7864 vn_nary_op_insert_pieces_predicated (2, GT_EXPR
, boolean_type_node
,
7865 ops
, boolean_false_node
, 0, pred_e
);
7866 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR
, boolean_type_node
,
7867 ops
, boolean_false_node
, 0, pred_e
);
7870 /* a > b -> a {!,>}= b */
7871 vn_nary_op_insert_pieces_predicated (2, NE_EXPR
, boolean_type_node
,
7872 ops
, boolean_true_node
, 0, pred_e
);
7873 vn_nary_op_insert_pieces_predicated (2, GE_EXPR
, boolean_type_node
,
7874 ops
, boolean_true_node
, 0, pred_e
);
7875 /* a > b -> ! a {<,=} b */
7876 vn_nary_op_insert_pieces_predicated (2, LT_EXPR
, boolean_type_node
,
7877 ops
, boolean_false_node
, 0, pred_e
);
7878 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR
, boolean_type_node
,
7879 ops
, boolean_false_node
, 0, pred_e
);
7882 /* a == b -> ! a {<,>} b */
7883 vn_nary_op_insert_pieces_predicated (2, LT_EXPR
, boolean_type_node
,
7884 ops
, boolean_false_node
, 0, pred_e
);
7885 vn_nary_op_insert_pieces_predicated (2, GT_EXPR
, boolean_type_node
,
7886 ops
, boolean_false_node
, 0, pred_e
);
7891 /* Nothing besides inverted condition. */
7897 /* Main stmt worker for RPO VN, process BB. */
7900 process_bb (rpo_elim
&avail
, basic_block bb
,
7901 bool bb_visited
, bool iterate_phis
, bool iterate
, bool eliminate
,
7902 bool do_region
, bitmap exit_bbs
, bool skip_phis
)
7910 /* If we are in loop-closed SSA preserve this state. This is
7911 relevant when called on regions from outside of FRE/PRE. */
7912 bool lc_phi_nodes
= false;
7914 && loops_state_satisfies_p (LOOP_CLOSED_SSA
))
7915 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
7916 if (e
->src
->loop_father
!= e
->dest
->loop_father
7917 && flow_loop_nested_p (e
->dest
->loop_father
,
7918 e
->src
->loop_father
))
7920 lc_phi_nodes
= true;
7924 /* When we visit a loop header substitute into loop info. */
7925 if (!iterate
&& eliminate
&& bb
->loop_father
->header
== bb
)
7927 /* Keep fields in sync with substitute_in_loop_info. */
7928 if (bb
->loop_father
->nb_iterations
)
7929 bb
->loop_father
->nb_iterations
7930 = simplify_replace_tree (bb
->loop_father
->nb_iterations
,
7931 NULL_TREE
, NULL_TREE
, &vn_valueize_for_srt
);
7934 /* Value-number all defs in the basic-block. */
7936 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7939 gphi
*phi
= gsi
.phi ();
7940 tree res
= PHI_RESULT (phi
);
7941 vn_ssa_aux_t res_info
= VN_INFO (res
);
7944 gcc_assert (!res_info
->visited
);
7945 res_info
->valnum
= VN_TOP
;
7946 res_info
->visited
= true;
7949 /* When not iterating force backedge values to varying. */
7950 visit_stmt (phi
, !iterate_phis
);
7951 if (virtual_operand_p (res
))
7955 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
7956 how we handle backedges and availability.
7957 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
7958 tree val
= res_info
->valnum
;
7959 if (res
!= val
&& !iterate
&& eliminate
)
7961 if (tree leader
= avail
.eliminate_avail (bb
, res
))
7964 /* Preserve loop-closed SSA form. */
7966 || is_gimple_min_invariant (leader
)))
7968 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7970 fprintf (dump_file
, "Replaced redundant PHI node "
7972 print_generic_expr (dump_file
, res
);
7973 fprintf (dump_file
, " with ");
7974 print_generic_expr (dump_file
, leader
);
7975 fprintf (dump_file
, "\n");
7977 avail
.eliminations
++;
7979 if (may_propagate_copy (res
, leader
))
7981 /* Schedule for removal. */
7982 avail
.to_remove
.safe_push (phi
);
7985 /* ??? Else generate a copy stmt. */
7989 /* Only make defs available that not already are. But make
7990 sure loop-closed SSA PHI node defs are picked up for
7994 || ! avail
.eliminate_avail (bb
, res
))
7995 avail
.eliminate_push_avail (bb
, res
);
7998 /* For empty BBs mark outgoing edges executable. For non-empty BBs
7999 we do this when processing the last stmt as we have to do this
8000 before elimination which otherwise forces GIMPLE_CONDs to
8001 if (1 != 0) style when seeing non-executable edges. */
8002 if (gsi_end_p (gsi_start_bb (bb
)))
8004 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8006 if (!(e
->flags
& EDGE_EXECUTABLE
))
8008 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8010 "marking outgoing edge %d -> %d executable\n",
8011 e
->src
->index
, e
->dest
->index
);
8012 e
->flags
|= EDGE_EXECUTABLE
;
8013 e
->dest
->flags
|= BB_EXECUTABLE
;
8015 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
8017 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8019 "marking destination block %d reachable\n",
8021 e
->dest
->flags
|= BB_EXECUTABLE
;
8025 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
8026 !gsi_end_p (gsi
); gsi_next (&gsi
))
8032 FOR_EACH_SSA_TREE_OPERAND (op
, gsi_stmt (gsi
), i
, SSA_OP_ALL_DEFS
)
8034 vn_ssa_aux_t op_info
= VN_INFO (op
);
8035 gcc_assert (!op_info
->visited
);
8036 op_info
->valnum
= VN_TOP
;
8037 op_info
->visited
= true;
8040 /* We somehow have to deal with uses that are not defined
8041 in the processed region. Forcing unvisited uses to
8042 varying here doesn't play well with def-use following during
8043 expression simplification, so we deal with this by checking
8044 the visited flag in SSA_VAL. */
8047 visit_stmt (gsi_stmt (gsi
));
8049 gimple
*last
= gsi_stmt (gsi
);
8051 switch (gimple_code (last
))
8054 e
= find_taken_edge (bb
, vn_valueize (gimple_switch_index
8055 (as_a
<gswitch
*> (last
))));
8059 tree lhs
= vn_valueize (gimple_cond_lhs (last
));
8060 tree rhs
= vn_valueize (gimple_cond_rhs (last
));
8061 tree val
= gimple_simplify (gimple_cond_code (last
),
8062 boolean_type_node
, lhs
, rhs
,
8064 /* If the condition didn't simplfy see if we have recorded
8065 an expression from sofar taken edges. */
8066 if (! val
|| TREE_CODE (val
) != INTEGER_CST
)
8068 vn_nary_op_t vnresult
;
8072 val
= vn_nary_op_lookup_pieces (2, gimple_cond_code (last
),
8073 boolean_type_node
, ops
,
8075 /* Did we get a predicated value? */
8076 if (! val
&& vnresult
&& vnresult
->predicated_values
)
8078 val
= vn_nary_op_get_predicated_value (vnresult
, bb
);
8079 if (val
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
8081 fprintf (dump_file
, "Got predicated value ");
8082 print_generic_expr (dump_file
, val
, TDF_NONE
);
8083 fprintf (dump_file
, " for ");
8084 print_gimple_stmt (dump_file
, last
, TDF_SLIM
);
8089 e
= find_taken_edge (bb
, val
);
8092 /* If we didn't manage to compute the taken edge then
8093 push predicated expressions for the condition itself
8094 and related conditions to the hashtables. This allows
8095 simplification of redundant conditions which is
8096 important as early cleanup. */
8097 edge true_e
, false_e
;
8098 extract_true_false_edges_from_block (bb
, &true_e
, &false_e
);
8099 enum tree_code code
= gimple_cond_code (last
);
8100 enum tree_code icode
8101 = invert_tree_comparison (code
, HONOR_NANS (lhs
));
8105 if ((do_region
&& bitmap_bit_p (exit_bbs
, true_e
->dest
->index
))
8106 || !can_track_predicate_on_edge (true_e
))
8108 if ((do_region
&& bitmap_bit_p (exit_bbs
, false_e
->dest
->index
))
8109 || !can_track_predicate_on_edge (false_e
))
8112 vn_nary_op_insert_pieces_predicated
8113 (2, code
, boolean_type_node
, ops
,
8114 boolean_true_node
, 0, true_e
);
8116 vn_nary_op_insert_pieces_predicated
8117 (2, code
, boolean_type_node
, ops
,
8118 boolean_false_node
, 0, false_e
);
8119 if (icode
!= ERROR_MARK
)
8122 vn_nary_op_insert_pieces_predicated
8123 (2, icode
, boolean_type_node
, ops
,
8124 boolean_false_node
, 0, true_e
);
8126 vn_nary_op_insert_pieces_predicated
8127 (2, icode
, boolean_type_node
, ops
,
8128 boolean_true_node
, 0, false_e
);
8130 /* Relax for non-integers, inverted condition handled
8132 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
8135 insert_related_predicates_on_edge (code
, ops
, true_e
);
8137 insert_related_predicates_on_edge (icode
, ops
, false_e
);
8143 e
= find_taken_edge (bb
, vn_valueize (gimple_goto_dest (last
)));
8150 todo
= TODO_cleanup_cfg
;
8151 if (!(e
->flags
& EDGE_EXECUTABLE
))
8153 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8155 "marking known outgoing %sedge %d -> %d executable\n",
8156 e
->flags
& EDGE_DFS_BACK
? "back-" : "",
8157 e
->src
->index
, e
->dest
->index
);
8158 e
->flags
|= EDGE_EXECUTABLE
;
8159 e
->dest
->flags
|= BB_EXECUTABLE
;
8161 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
8163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8165 "marking destination block %d reachable\n",
8167 e
->dest
->flags
|= BB_EXECUTABLE
;
8170 else if (gsi_one_before_end_p (gsi
))
8172 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8174 if (!(e
->flags
& EDGE_EXECUTABLE
))
8176 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8178 "marking outgoing edge %d -> %d executable\n",
8179 e
->src
->index
, e
->dest
->index
);
8180 e
->flags
|= EDGE_EXECUTABLE
;
8181 e
->dest
->flags
|= BB_EXECUTABLE
;
8183 else if (!(e
->dest
->flags
& BB_EXECUTABLE
))
8185 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8187 "marking destination block %d reachable\n",
8189 e
->dest
->flags
|= BB_EXECUTABLE
;
8194 /* Eliminate. That also pushes to avail. */
8195 if (eliminate
&& ! iterate
)
8196 avail
.eliminate_stmt (bb
, &gsi
);
8198 /* If not eliminating, make all not already available defs
8199 available. But avoid picking up dead defs. */
8200 FOR_EACH_SSA_TREE_OPERAND (op
, gsi_stmt (gsi
), i
, SSA_OP_DEF
)
8201 if (! has_zero_uses (op
)
8202 && ! avail
.eliminate_avail (bb
, op
))
8203 avail
.eliminate_push_avail (bb
, op
);
8206 /* Eliminate in destination PHI arguments. Always substitute in dest
8207 PHIs, even for non-executable edges. This handles region
8209 if (!iterate
&& eliminate
)
8210 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8211 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
8212 !gsi_end_p (gsi
); gsi_next (&gsi
))
8214 gphi
*phi
= gsi
.phi ();
8215 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
8216 tree arg
= USE_FROM_PTR (use_p
);
8217 if (TREE_CODE (arg
) != SSA_NAME
8218 || virtual_operand_p (arg
))
8221 if (SSA_NAME_IS_DEFAULT_DEF (arg
))
8223 sprime
= SSA_VAL (arg
);
8224 gcc_assert (TREE_CODE (sprime
) != SSA_NAME
8225 || SSA_NAME_IS_DEFAULT_DEF (sprime
));
8228 /* Look for sth available at the definition block of the argument.
8229 This avoids inconsistencies between availability there which
8230 decides if the stmt can be removed and availability at the
8231 use site. The SSA property ensures that things available
8232 at the definition are also available at uses. */
8233 sprime
= avail
.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg
)),
8237 && may_propagate_copy (arg
, sprime
, !(e
->flags
& EDGE_ABNORMAL
)))
8238 propagate_value (use_p
, sprime
);
8241 vn_context_bb
= NULL
;
8245 /* Unwind state per basic-block. */
8249 /* Times this block has been visited. */
8251 /* Whether to handle this as iteration point or whether to treat
8252 incoming backedge PHI values as varying. */
8254 /* Maximum RPO index this block is reachable from. */
8258 vn_reference_t ref_top
;
8260 vn_nary_op_t nary_top
;
8261 vn_avail
*avail_top
;
8264 /* Unwind the RPO VN state for iteration. */
8267 do_unwind (unwind_state
*to
, rpo_elim
&avail
)
8269 gcc_assert (to
->iterate
);
8270 for (; last_inserted_nary
!= to
->nary_top
;
8271 last_inserted_nary
= last_inserted_nary
->next
)
8274 slot
= valid_info
->nary
->find_slot_with_hash
8275 (last_inserted_nary
, last_inserted_nary
->hashcode
, NO_INSERT
);
8276 /* Predication causes the need to restore previous state. */
8277 if ((*slot
)->unwind_to
)
8278 *slot
= (*slot
)->unwind_to
;
8280 valid_info
->nary
->clear_slot (slot
);
8282 for (; last_inserted_phi
!= to
->phi_top
;
8283 last_inserted_phi
= last_inserted_phi
->next
)
8286 slot
= valid_info
->phis
->find_slot_with_hash
8287 (last_inserted_phi
, last_inserted_phi
->hashcode
, NO_INSERT
);
8288 valid_info
->phis
->clear_slot (slot
);
8290 for (; last_inserted_ref
!= to
->ref_top
;
8291 last_inserted_ref
= last_inserted_ref
->next
)
8293 vn_reference_t
*slot
;
8294 slot
= valid_info
->references
->find_slot_with_hash
8295 (last_inserted_ref
, last_inserted_ref
->hashcode
, NO_INSERT
);
8296 (*slot
)->operands
.release ();
8297 valid_info
->references
->clear_slot (slot
);
8299 obstack_free (&vn_tables_obstack
, to
->ob_top
);
8301 /* Prune [rpo_idx, ] from avail. */
8302 for (; last_pushed_avail
&& last_pushed_avail
->avail
!= to
->avail_top
;)
8304 vn_ssa_aux_t val
= last_pushed_avail
;
8305 vn_avail
*av
= val
->avail
;
8306 val
->avail
= av
->next
;
8307 last_pushed_avail
= av
->next_undo
;
8308 av
->next
= avail
.m_avail_freelist
;
8309 avail
.m_avail_freelist
= av
;
8313 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8314 If ITERATE is true then treat backedges optimistically as not
8315 executed and iterate. If ELIMINATE is true then perform
8316 elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8317 is true then force PHI nodes in ENTRY->dest to VARYING. */
8320 do_rpo_vn_1 (function
*fn
, edge entry
, bitmap exit_bbs
,
8321 bool iterate
, bool eliminate
, bool skip_entry_phis
,
8322 vn_lookup_kind kind
)
8325 default_vn_walk_kind
= kind
;
8327 /* We currently do not support region-based iteration when
8328 elimination is requested. */
8329 gcc_assert (!entry
|| !iterate
|| !eliminate
);
8330 /* When iterating we need loop info up-to-date. */
8331 gcc_assert (!iterate
|| !loops_state_satisfies_p (LOOPS_NEED_FIXUP
));
8333 bool do_region
= entry
!= NULL
;
8336 entry
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn
));
8337 exit_bbs
= BITMAP_ALLOC (NULL
);
8338 bitmap_set_bit (exit_bbs
, EXIT_BLOCK
);
8341 /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8342 re-mark those that are contained in the region. */
8345 FOR_EACH_EDGE (e
, ei
, entry
->dest
->preds
)
8346 e
->flags
&= ~EDGE_DFS_BACK
;
8348 int *rpo
= XNEWVEC (int, n_basic_blocks_for_fn (fn
) - NUM_FIXED_BLOCKS
);
8349 auto_vec
<std::pair
<int, int> > toplevel_scc_extents
;
8350 int n
= rev_post_order_and_mark_dfs_back_seme
8351 (fn
, entry
, exit_bbs
, true, rpo
, !iterate
? &toplevel_scc_extents
: NULL
);
8354 BITMAP_FREE (exit_bbs
);
8356 /* If there are any non-DFS_BACK edges into entry->dest skip
8357 processing PHI nodes for that block. This supports
8358 value-numbering loop bodies w/o the actual loop. */
8359 FOR_EACH_EDGE (e
, ei
, entry
->dest
->preds
)
8361 && !(e
->flags
& EDGE_DFS_BACK
))
8363 if (e
!= NULL
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
8364 fprintf (dump_file
, "Region does not contain all edges into "
8365 "the entry block, skipping its PHIs.\n");
8366 skip_entry_phis
|= e
!= NULL
;
8368 int *bb_to_rpo
= XNEWVEC (int, last_basic_block_for_fn (fn
));
8369 for (int i
= 0; i
< n
; ++i
)
8370 bb_to_rpo
[rpo
[i
]] = i
;
8372 unwind_state
*rpo_state
= XNEWVEC (unwind_state
, n
);
8374 rpo_elim
avail (entry
->dest
);
8377 /* Verify we have no extra entries into the region. */
8378 if (flag_checking
&& do_region
)
8380 auto_bb_flag
bb_in_region (fn
);
8381 for (int i
= 0; i
< n
; ++i
)
8383 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8384 bb
->flags
|= bb_in_region
;
8386 /* We can't merge the first two loops because we cannot rely
8387 on EDGE_DFS_BACK for edges not within the region. But if
8388 we decide to always have the bb_in_region flag we can
8389 do the checking during the RPO walk itself (but then it's
8390 also easy to handle MEME conservatively). */
8391 for (int i
= 0; i
< n
; ++i
)
8393 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8396 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8397 gcc_assert (e
== entry
8398 || (skip_entry_phis
&& bb
== entry
->dest
)
8399 || (e
->src
->flags
& bb_in_region
));
8401 for (int i
= 0; i
< n
; ++i
)
8403 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8404 bb
->flags
&= ~bb_in_region
;
8408 /* Create the VN state. For the initial size of the various hashtables
8409 use a heuristic based on region size and number of SSA names. */
8410 unsigned region_size
= (((unsigned HOST_WIDE_INT
)n
* num_ssa_names
)
8411 / (n_basic_blocks_for_fn (fn
) - NUM_FIXED_BLOCKS
));
8412 VN_TOP
= create_tmp_var_raw (void_type_node
, "vn_top");
8414 next_constant_value_id
= -1;
8416 vn_ssa_aux_hash
= new hash_table
<vn_ssa_aux_hasher
> (region_size
* 2);
8417 gcc_obstack_init (&vn_ssa_aux_obstack
);
8419 gcc_obstack_init (&vn_tables_obstack
);
8420 gcc_obstack_init (&vn_tables_insert_obstack
);
8421 valid_info
= XCNEW (struct vn_tables_s
);
8422 allocate_vn_table (valid_info
, region_size
);
8423 last_inserted_ref
= NULL
;
8424 last_inserted_phi
= NULL
;
8425 last_inserted_nary
= NULL
;
8426 last_pushed_avail
= NULL
;
8428 vn_valueize
= rpo_vn_valueize
;
8430 /* Initialize the unwind state and edge/BB executable state. */
8431 unsigned curr_scc
= 0;
8432 for (int i
= 0; i
< n
; ++i
)
8434 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8435 rpo_state
[i
].visited
= 0;
8436 rpo_state
[i
].max_rpo
= i
;
8437 if (!iterate
&& curr_scc
< toplevel_scc_extents
.length ())
8439 if (i
>= toplevel_scc_extents
[curr_scc
].first
8440 && i
<= toplevel_scc_extents
[curr_scc
].second
)
8441 rpo_state
[i
].max_rpo
= toplevel_scc_extents
[curr_scc
].second
;
8442 if (i
== toplevel_scc_extents
[curr_scc
].second
)
8445 bb
->flags
&= ~BB_EXECUTABLE
;
8446 bool has_backedges
= false;
8449 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8451 if (e
->flags
& EDGE_DFS_BACK
)
8452 has_backedges
= true;
8453 e
->flags
&= ~EDGE_EXECUTABLE
;
8454 if (iterate
|| e
== entry
|| (skip_entry_phis
&& bb
== entry
->dest
))
8457 rpo_state
[i
].iterate
= iterate
&& has_backedges
;
8459 entry
->flags
|= EDGE_EXECUTABLE
;
8460 entry
->dest
->flags
|= BB_EXECUTABLE
;
8462 /* As heuristic to improve compile-time we handle only the N innermost
8463 loops and the outermost one optimistically. */
8466 unsigned max_depth
= param_rpo_vn_max_loop_depth
;
8467 for (auto loop
: loops_list (cfun
, LI_ONLY_INNERMOST
))
8468 if (loop_depth (loop
) > max_depth
)
8469 for (unsigned i
= 2;
8470 i
< loop_depth (loop
) - max_depth
; ++i
)
8472 basic_block header
= superloop_at_depth (loop
, i
)->header
;
8473 bool non_latch_backedge
= false;
8476 FOR_EACH_EDGE (e
, ei
, header
->preds
)
8477 if (e
->flags
& EDGE_DFS_BACK
)
8479 /* There can be a non-latch backedge into the header
8480 which is part of an outer irreducible region. We
8481 cannot avoid iterating this block then. */
8482 if (!dominated_by_p (CDI_DOMINATORS
,
8485 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8486 fprintf (dump_file
, "non-latch backedge %d -> %d "
8487 "forces iteration of loop %d\n",
8488 e
->src
->index
, e
->dest
->index
, loop
->num
);
8489 non_latch_backedge
= true;
8492 e
->flags
|= EDGE_EXECUTABLE
;
8494 rpo_state
[bb_to_rpo
[header
->index
]].iterate
= non_latch_backedge
;
8501 /* Go and process all blocks, iterating as necessary. */
8504 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[idx
]);
8506 /* If the block has incoming backedges remember unwind state. This
8507 is required even for non-executable blocks since in irreducible
8508 regions we might reach them via the backedge and re-start iterating
8510 Note we can individually mark blocks with incoming backedges to
8511 not iterate where we then handle PHIs conservatively. We do that
8512 heuristically to reduce compile-time for degenerate cases. */
8513 if (rpo_state
[idx
].iterate
)
8515 rpo_state
[idx
].ob_top
= obstack_alloc (&vn_tables_obstack
, 0);
8516 rpo_state
[idx
].ref_top
= last_inserted_ref
;
8517 rpo_state
[idx
].phi_top
= last_inserted_phi
;
8518 rpo_state
[idx
].nary_top
= last_inserted_nary
;
8519 rpo_state
[idx
].avail_top
8520 = last_pushed_avail
? last_pushed_avail
->avail
: NULL
;
8523 if (!(bb
->flags
& BB_EXECUTABLE
))
8525 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8526 fprintf (dump_file
, "Block %d: BB%d found not executable\n",
8532 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8533 fprintf (dump_file
, "Processing block %d: BB%d\n", idx
, bb
->index
);
8535 todo
|= process_bb (avail
, bb
,
8536 rpo_state
[idx
].visited
!= 0,
8537 rpo_state
[idx
].iterate
,
8538 iterate
, eliminate
, do_region
, exit_bbs
, false);
8539 rpo_state
[idx
].visited
++;
8541 /* Verify if changed values flow over executable outgoing backedges
8542 and those change destination PHI values (that's the thing we
8543 can easily verify). Reduce over all such edges to the farthest
8545 int iterate_to
= -1;
8548 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8549 if ((e
->flags
& (EDGE_DFS_BACK
|EDGE_EXECUTABLE
))
8550 == (EDGE_DFS_BACK
|EDGE_EXECUTABLE
)
8551 && rpo_state
[bb_to_rpo
[e
->dest
->index
]].iterate
)
8553 int destidx
= bb_to_rpo
[e
->dest
->index
];
8554 if (!rpo_state
[destidx
].visited
)
8556 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8557 fprintf (dump_file
, "Unvisited destination %d\n",
8559 if (iterate_to
== -1 || destidx
< iterate_to
)
8560 iterate_to
= destidx
;
8563 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8564 fprintf (dump_file
, "Looking for changed values of backedge"
8565 " %d->%d destination PHIs\n",
8566 e
->src
->index
, e
->dest
->index
);
8567 vn_context_bb
= e
->dest
;
8569 for (gsi
= gsi_start_phis (e
->dest
);
8570 !gsi_end_p (gsi
); gsi_next (&gsi
))
8572 bool inserted
= false;
8573 /* While we'd ideally just iterate on value changes
8574 we CSE PHIs and do that even across basic-block
8575 boundaries. So even hashtable state changes can
8576 be important (which is roughly equivalent to
8577 PHI argument value changes). To not excessively
8578 iterate because of that we track whether a PHI
8579 was CSEd to with GF_PLF_1. */
8580 bool phival_changed
;
8581 if ((phival_changed
= visit_phi (gsi
.phi (),
8583 || (inserted
&& gimple_plf (gsi
.phi (), GF_PLF_1
)))
8586 && dump_file
&& (dump_flags
& TDF_DETAILS
))
8587 fprintf (dump_file
, "PHI was CSEd and hashtable "
8588 "state (changed)\n");
8589 if (iterate_to
== -1 || destidx
< iterate_to
)
8590 iterate_to
= destidx
;
8594 vn_context_bb
= NULL
;
8596 if (iterate_to
!= -1)
8598 do_unwind (&rpo_state
[iterate_to
], avail
);
8600 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8601 fprintf (dump_file
, "Iterating to %d BB%d\n",
8602 iterate_to
, rpo
[iterate_to
]);
8612 /* Process all blocks greedily with a worklist that enforces RPO
8613 processing of reachable blocks. */
8614 auto_bitmap worklist
;
8615 bitmap_set_bit (worklist
, 0);
8616 while (!bitmap_empty_p (worklist
))
8618 int idx
= bitmap_clear_first_set_bit (worklist
);
8619 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[idx
]);
8620 gcc_assert ((bb
->flags
& BB_EXECUTABLE
)
8621 && !rpo_state
[idx
].visited
);
8623 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8624 fprintf (dump_file
, "Processing block %d: BB%d\n", idx
, bb
->index
);
8626 /* When we run into predecessor edges where we cannot trust its
8627 executable state mark them executable so PHI processing will
8629 ??? Do we need to force arguments flowing over that edge
8630 to be varying or will they even always be? */
8633 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
8634 if (!(e
->flags
& EDGE_EXECUTABLE
)
8635 && (bb
== entry
->dest
8636 || (!rpo_state
[bb_to_rpo
[e
->src
->index
]].visited
8637 && (rpo_state
[bb_to_rpo
[e
->src
->index
]].max_rpo
8640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8641 fprintf (dump_file
, "Cannot trust state of predecessor "
8642 "edge %d -> %d, marking executable\n",
8643 e
->src
->index
, e
->dest
->index
);
8644 e
->flags
|= EDGE_EXECUTABLE
;
8648 todo
|= process_bb (avail
, bb
, false, false, false, eliminate
,
8649 do_region
, exit_bbs
,
8650 skip_entry_phis
&& bb
== entry
->dest
);
8651 rpo_state
[idx
].visited
++;
8653 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
8654 if ((e
->flags
& EDGE_EXECUTABLE
)
8655 && e
->dest
->index
!= EXIT_BLOCK
8656 && (!do_region
|| !bitmap_bit_p (exit_bbs
, e
->dest
->index
))
8657 && !rpo_state
[bb_to_rpo
[e
->dest
->index
]].visited
)
8658 bitmap_set_bit (worklist
, bb_to_rpo
[e
->dest
->index
]);
8662 /* If statistics or dump file active. */
8664 unsigned max_visited
= 1;
8665 for (int i
= 0; i
< n
; ++i
)
8667 basic_block bb
= BASIC_BLOCK_FOR_FN (fn
, rpo
[i
]);
8668 if (bb
->flags
& BB_EXECUTABLE
)
8670 statistics_histogram_event (cfun
, "RPO block visited times",
8671 rpo_state
[i
].visited
);
8672 if (rpo_state
[i
].visited
> max_visited
)
8673 max_visited
= rpo_state
[i
].visited
;
8675 unsigned nvalues
= 0, navail
= 0;
8676 for (hash_table
<vn_ssa_aux_hasher
>::iterator i
= vn_ssa_aux_hash
->begin ();
8677 i
!= vn_ssa_aux_hash
->end (); ++i
)
8680 vn_avail
*av
= (*i
)->avail
;
8687 statistics_counter_event (cfun
, "RPO blocks", n
);
8688 statistics_counter_event (cfun
, "RPO blocks visited", nblk
);
8689 statistics_counter_event (cfun
, "RPO blocks executable", nex
);
8690 statistics_histogram_event (cfun
, "RPO iterations", 10*nblk
/ nex
);
8691 statistics_histogram_event (cfun
, "RPO num values", nvalues
);
8692 statistics_histogram_event (cfun
, "RPO num avail", navail
);
8693 statistics_histogram_event (cfun
, "RPO num lattice",
8694 vn_ssa_aux_hash
->elements ());
8695 if (dump_file
&& (dump_flags
& (TDF_DETAILS
|TDF_STATS
)))
8697 fprintf (dump_file
, "RPO iteration over %d blocks visited %" PRIu64
8698 " blocks in total discovering %d executable blocks iterating "
8699 "%d.%d times, a block was visited max. %u times\n",
8701 (int)((10*nblk
/ nex
)/10), (int)((10*nblk
/ nex
)%10),
8703 fprintf (dump_file
, "RPO tracked %d values available at %d locations "
8704 "and %" PRIu64
" lattice elements\n",
8705 nvalues
, navail
, (uint64_t) vn_ssa_aux_hash
->elements ());
8710 /* When !iterate we already performed elimination during the RPO
8714 /* Elimination for region-based VN needs to be done within the
8716 gcc_assert (! do_region
);
8717 /* Note we can't use avail.walk here because that gets confused
8718 by the existing availability and it will be less efficient
8720 todo
|= eliminate_with_rpo_vn (NULL
);
8723 todo
|= avail
.eliminate_cleanup (do_region
);
8729 XDELETEVEC (bb_to_rpo
);
8731 XDELETEVEC (rpo_state
);
8736 /* Region-based entry for RPO VN. Performs value-numbering and elimination
8737 on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
8738 the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
8740 If ITERATE is true then treat backedges optimistically as not
8741 executed and iterate. If ELIMINATE is true then perform
8742 elimination, otherwise leave that to the caller.
8743 If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
8744 KIND specifies the amount of work done for handling memory operations. */
8747 do_rpo_vn (function
*fn
, edge entry
, bitmap exit_bbs
,
8748 bool iterate
, bool eliminate
, bool skip_entry_phis
,
8749 vn_lookup_kind kind
)
8751 auto_timevar
tv (TV_TREE_RPO_VN
);
8752 unsigned todo
= do_rpo_vn_1 (fn
, entry
, exit_bbs
, iterate
, eliminate
,
8753 skip_entry_phis
, kind
);
8761 const pass_data pass_data_fre
=
8763 GIMPLE_PASS
, /* type */
8765 OPTGROUP_NONE
, /* optinfo_flags */
8766 TV_TREE_FRE
, /* tv_id */
8767 ( PROP_cfg
| PROP_ssa
), /* properties_required */
8768 0, /* properties_provided */
8769 0, /* properties_destroyed */
8770 0, /* todo_flags_start */
8771 0, /* todo_flags_finish */
8774 class pass_fre
: public gimple_opt_pass
8777 pass_fre (gcc::context
*ctxt
)
8778 : gimple_opt_pass (pass_data_fre
, ctxt
), may_iterate (true)
8781 /* opt_pass methods: */
8782 opt_pass
* clone () final override
{ return new pass_fre (m_ctxt
); }
8783 void set_pass_param (unsigned int n
, bool param
) final override
8785 gcc_assert (n
== 0);
8786 may_iterate
= param
;
8788 bool gate (function
*) final override
8790 return flag_tree_fre
!= 0 && (may_iterate
|| optimize
> 1);
8792 unsigned int execute (function
*) final override
;
8796 }; // class pass_fre
8799 pass_fre::execute (function
*fun
)
8803 /* At -O[1g] use the cheap non-iterating mode. */
8804 bool iterate_p
= may_iterate
&& (optimize
> 1);
8805 calculate_dominance_info (CDI_DOMINATORS
);
8807 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
8809 todo
= do_rpo_vn_1 (fun
, NULL
, NULL
, iterate_p
, true, false, VN_WALKREWRITE
);
8813 loop_optimizer_finalize ();
8815 if (scev_initialized_p ())
8818 /* For late FRE after IVOPTs and unrolling, see if we can
8819 remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8821 todo
|= TODO_update_address_taken
;
8829 make_pass_fre (gcc::context
*ctxt
)
8831 return new pass_fre (ctxt
);
8834 #undef BB_EXECUTABLE